001    /** 
002     * 
003     * Copyright 2004 Hiram Chirino
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * 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     **/
018    package org.activemq.filter;
019    
020    import javax.jms.JMSException;
021    import javax.jms.Message;
022    
023    /**
024     * A filter performing a comparison of two objects
025     * 
026     * @version $Revision: 1.1.1.1 $
027     */
028    public abstract class LogicExpression extends BinaryExpression implements BooleanExpression {
029    
030        public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) {
031            return new LogicExpression(lvalue, rvalue) {
032                    
033                public Object evaluate(Message message) throws JMSException {
034                    
035                    Boolean lv = (Boolean) left.evaluate(message);
036                    // Can we do an OR shortcut??
037                    if (lv !=null && lv.booleanValue()) {
038                        return Boolean.TRUE;
039                    }
040                    
041                    Boolean rv = (Boolean) right.evaluate(message);
042                    return rv==null ? null : rv;
043                }
044    
045                public String getExpressionSymbol() {
046                    return "OR";
047                }
048            };
049        }
050    
051        public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) {
052            return new LogicExpression(lvalue, rvalue) {
053                
054                    public Object evaluate(Message message) throws JMSException {
055                    
056                    Boolean lv = (Boolean) left.evaluate(message);
057                    
058                    // Can we do an AND shortcut??
059                    if( lv==null )
060                            return null;
061                    if (!lv.booleanValue()) {
062                        return Boolean.FALSE;
063                    }
064                    
065                    Boolean rv = (Boolean) right.evaluate(message);
066                    return rv==null ? null : rv;
067                }
068    
069                public String getExpressionSymbol() {
070                    return "AND";
071                }
072            };
073        }
074    
075        /**
076         * @param left
077         * @param right
078         */
079        public LogicExpression(BooleanExpression left, BooleanExpression right) {
080            super(left, right);
081        }
082    
083        abstract public Object evaluate(Message message) throws JMSException;
084    
085    
086    }