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     */
017    package org.apache.activemq.filter;
018    
019    import javax.jms.JMSException;
020    
021    /**
022     * A filter performing a comparison of two objects
023     * 
024     * 
025     */
026    public 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                    // Can we do an OR shortcut??
043                    if (lv != null && lv.booleanValue()) {
044                        return Boolean.TRUE;
045                    }
046    
047                    Boolean rv = (Boolean)right.evaluate(message);
048                    return rv == null ? null : rv;
049                }
050    
051                public String getExpressionSymbol() {
052                    return "OR";
053                }
054            };
055        }
056    
057        public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) {
058            return new LogicExpression(lvalue, rvalue) {
059    
060                public Object evaluate(MessageEvaluationContext message) throws JMSException {
061    
062                    Boolean lv = (Boolean)left.evaluate(message);
063    
064                    // Can we do an AND shortcut??
065                    if (lv == null) {
066                        return null;
067                    }
068                    if (!lv.booleanValue()) {
069                        return Boolean.FALSE;
070                    }
071    
072                    Boolean rv = (Boolean)right.evaluate(message);
073                    return rv == null ? null : rv;
074                }
075    
076                public String getExpressionSymbol() {
077                    return "AND";
078                }
079            };
080        }
081    
082        public abstract Object evaluate(MessageEvaluationContext message) throws JMSException;
083    
084        public boolean matches(MessageEvaluationContext message) throws JMSException {
085            Object object = evaluate(message);
086            return object != null && object == Boolean.TRUE;
087        }
088    
089    }