001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.filter;
018
019import javax.jms.JMSException;
020
021/**
022 * A filter performing a comparison of two objects
023 * 
024 * 
025 */
026public abstract class LogicExpression extends BinaryExpression implements BooleanExpression {
027
028    /**
029     * @param left
030     * @param right
031     */
032    public LogicExpression(BooleanExpression left, BooleanExpression right) {
033        super(left, right);
034    }
035
036    public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) {
037        return new LogicExpression(lvalue, rvalue) {
038
039            public Object evaluate(MessageEvaluationContext message) throws JMSException {
040
041                Boolean lv = (Boolean)left.evaluate(message);
042                if (lv != null && lv.booleanValue()) {
043                    return Boolean.TRUE;
044                }
045                Boolean rv = (Boolean)right.evaluate(message);
046                if (rv != null && rv.booleanValue()) {
047                    return Boolean.TRUE;
048                }
049                if (lv == null || rv == null) {
050                    return null;
051                }
052                return Boolean.FALSE;
053            }
054
055            public String getExpressionSymbol() {
056                return "OR";
057            }
058        };
059    }
060
061    public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) {
062        return new LogicExpression(lvalue, rvalue) {
063
064            public Object evaluate(MessageEvaluationContext message) throws JMSException {
065
066                Boolean lv = (Boolean)left.evaluate(message);
067
068                if (lv != null && !lv.booleanValue()) {
069                    return Boolean.FALSE;
070                }
071                Boolean rv = (Boolean)right.evaluate(message);
072                if (rv != null && !rv.booleanValue()) {
073                    return Boolean.FALSE;
074                }
075                if (lv == null || rv == null) {
076                    return null;
077                }
078                return Boolean.TRUE;
079            }
080
081            public String getExpressionSymbol() {
082                return "AND";
083            }
084        };
085    }
086
087    public abstract Object evaluate(MessageEvaluationContext message) throws JMSException;
088
089    public boolean matches(MessageEvaluationContext message) throws JMSException {
090        Object object = evaluate(message);
091        return object != null && object == Boolean.TRUE;
092    }
093
094}