KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > filter > LogicExpression


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.filter;
19
20 import javax.jms.JMSException JavaDoc;
21
22 /**
23  * A filter performing a comparison of two objects
24  *
25  * @version $Revision: 1.2 $
26  */

27 public abstract class LogicExpression extends BinaryExpression implements BooleanExpression {
28
29     public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) {
30         return new LogicExpression(lvalue, rvalue) {
31             
32             public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
33                 
34                 Boolean JavaDoc lv = (Boolean JavaDoc) left.evaluate(message);
35                 // Can we do an OR shortcut??
36
if (lv !=null && lv.booleanValue()) {
37                     return Boolean.TRUE;
38                 }
39                 
40                 Boolean JavaDoc rv = (Boolean JavaDoc) right.evaluate(message);
41                 return rv==null ? null : rv;
42             }
43
44             public String JavaDoc getExpressionSymbol() {
45                 return "OR";
46             }
47         };
48     }
49
50     public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) {
51         return new LogicExpression(lvalue, rvalue) {
52
53             public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
54
55                 Boolean JavaDoc lv = (Boolean JavaDoc) left.evaluate(message);
56
57                 // Can we do an AND shortcut??
58
if (lv == null)
59                     return null;
60                 if (!lv.booleanValue()) {
61                     return Boolean.FALSE;
62                 }
63
64                 Boolean JavaDoc rv = (Boolean JavaDoc) right.evaluate(message);
65                 return rv == null ? null : rv;
66             }
67
68             public String JavaDoc getExpressionSymbol() {
69                 return "AND";
70             }
71         };
72     }
73
74     /**
75      * @param left
76      * @param right
77      */

78     public LogicExpression(BooleanExpression left, BooleanExpression right) {
79         super(left, right);
80     }
81
82     abstract public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc;
83
84     public boolean matches(MessageEvaluationContext message) throws JMSException JavaDoc {
85         Object JavaDoc object = evaluate(message);
86         return object!=null && object==Boolean.TRUE;
87     }
88
89 }
90
Popular Tags