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