LogicExpression.java
001 /* Licensed to the Apache Software Foundation (ASF) under one
002  * or more contributor license agreements.  See the NOTICE file
003  * distributed with this work for additional information
004  * regarding copyright ownership.  The ASF licenses this file
005  * to you under the Apache License, Version 2.0 (the
006  * "License"); you may not use this file except in compliance
007  * with 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,
012  * software distributed under the License is distributed on an
013  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
014  * KIND, either express or implied.  See the License for the
015  * specific language governing permissions and limitations
016  * under the License.
017  */
018 package org.apache.qpid.filter;
019 
020 import org.apache.qpid.QpidException;
021 import org.apache.qpid.client.message.AbstractJMSMessage;
022 
023 
024 /**
025  * A filter performing a comparison of two objects
026  */
027 public abstract class LogicExpression extends BinaryExpression implements BooleanExpression
028 {
029 
030     public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue)
031     {
032         return new LogicExpression(lvalue, rvalue)
033             {
034 
035                 public Object evaluate(AbstractJMSMessage messagethrows QpidException
036                 {
037 
038                     Boolean lv = (Booleanleft.evaluate(message);
039                     // Can we do an OR shortcut??
040                     if ((lv != null&& lv.booleanValue())
041                     {
042                         return Boolean.TRUE;
043                     }
044 
045                     Boolean rv = (Booleanright.evaluate(message);
046 
047                     return (rv == nullnull : rv;
048                 }
049 
050                 public String getExpressionSymbol()
051                 {
052                     return "OR";
053                 }
054             };
055     }
056 
057     public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue)
058     {
059         return new LogicExpression(lvalue, rvalue)
060             {
061 
062                 public Object evaluate(AbstractJMSMessage messagethrows QpidException
063                 {
064 
065                     Boolean lv = (Booleanleft.evaluate(message);
066 
067                     // Can we do an AND shortcut??
068                     if (lv == null)
069                     {
070                         return null;
071                     }
072 
073                     if (!lv.booleanValue())
074                     {
075                         return Boolean.FALSE;
076                     }
077 
078                     Boolean rv = (Booleanright.evaluate(message);
079 
080                     return (rv == nullnull : rv;
081                 }
082 
083                 public String getExpressionSymbol()
084                 {
085                     return "AND";
086                 }
087             };
088     }
089 
090     /**
091      @param left
092      @param right
093      */
094     public LogicExpression(BooleanExpression left, BooleanExpression right)
095     {
096         super(left, right);
097     }
098 
099     public abstract Object evaluate(AbstractJMSMessage messagethrows QpidException;
100 
101     public boolean matches(AbstractJMSMessage messagethrows QpidException
102     {
103         Object object = evaluate(message);
104 
105         return (object != null&& (object == Boolean.TRUE);
106     }
107 
108 }