View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.filter;
    19
    20  import javax.jms.JMSException;
    21  import javax.jms.Message;
    22
    23  /***
    24   * A filter performing a comparison of two objects
    25   *
    26   * @version $Revision: 1.1 $
    27   */
    28  public abstract class LogicExpression extends BinaryExpression implements BooleanExpression {
    29
    30      public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) {
    31          return new LogicExpression(lvalue, rvalue) {
    32              protected Object evaluate(Boolean lv, Boolean rv) {
    33                  return lv.booleanValue() || rv.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
    34              }
    35
    36              public String getExpressionSymbol() {
    37                  return "OR";
    38              }
    39          };
    40      }
    41
    42      public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) {
    43          return new LogicExpression(lvalue, rvalue) {
    44              protected Object evaluate(Boolean lv, Boolean rv) {
    45                  return lv.booleanValue() && rv.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
    46              }
    47
    48              public String getExpressionSymbol() {
    49                  return "AND";
    50              }
    51          };
    52      }
    53
    54      /***
    55       * @param left
    56       * @param right
    57       */
    58      public LogicExpression(BooleanExpression left, BooleanExpression right) {
    59          super(left, right);
    60      }
    61
    62      public Object evaluate(Message message) throws JMSException {
    63          Boolean lv = (Boolean) left.evaluate(message);
    64          if (lv == null) {
    65              return null;
    66          }
    67          Boolean rv = (Boolean) right.evaluate(message);
    68          if (rv == null) {
    69              return null;
    70          }
    71          return evaluate(lv, rv);
    72      }
    73
    74      abstract protected Object evaluate(Boolean lv, Boolean rv);
    75
    76  }