KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.math.BigDecimal JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.jms.JMSException JavaDoc;
27
28 /**
29  * An expression which performs an operation on two expression values
30  *
31  * @version $Revision: 1.3 $
32  */

33 public abstract class UnaryExpression implements Expression {
34
35     private static final BigDecimal JavaDoc BD_LONG_MIN_VALUE = BigDecimal.valueOf(Long.MIN_VALUE);
36     protected Expression right;
37
38     public static Expression createNegate(Expression left) {
39         return new UnaryExpression(left) {
40             public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
41                 Object JavaDoc rvalue = right.evaluate(message);
42                 if (rvalue == null) {
43                     return null;
44                 }
45                 if (rvalue instanceof Number JavaDoc) {
46                     return negate((Number JavaDoc) rvalue);
47                 }
48                 return null;
49             }
50
51             public String JavaDoc getExpressionSymbol() {
52                 return "-";
53             }
54         };
55     }
56
57     public static BooleanExpression createInExpression(PropertyExpression right, List JavaDoc elements, final boolean not) {
58         
59         // Use a HashSet if there are many elements.
60
Collection JavaDoc t;
61         if( elements.size()==0 )
62             t=null;
63         else if( elements.size() < 5 )
64             t = elements;
65         else {
66             t = new HashSet JavaDoc(elements);
67         }
68         final Collection JavaDoc inList = t;
69         
70         return new BooleanUnaryExpression(right) {
71             public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
72                 
73                 Object JavaDoc rvalue = right.evaluate(message);
74                 if (rvalue == null) {
75                     return null;
76                 }
77                 if( rvalue.getClass()!=String JavaDoc.class )
78                     return null;
79                 
80                 if( (inList!=null && inList.contains(rvalue)) ^ not ) {
81                     return Boolean.TRUE;
82                 } else {
83                     return Boolean.FALSE;
84                 }
85                 
86             }
87
88             public String JavaDoc toString() {
89                 StringBuffer JavaDoc answer = new StringBuffer JavaDoc();
90                 answer.append(right);
91                 answer.append(" ");
92                 answer.append(getExpressionSymbol());
93                 answer.append(" ( ");
94
95                 int count=0;
96                 for (Iterator JavaDoc i = inList.iterator(); i.hasNext();) {
97                     Object JavaDoc o = (Object JavaDoc) i.next();
98                     if( count!=0 ) {
99                         answer.append(", ");
100                     }
101                     answer.append(o);
102                     count++;
103                 }
104                 
105                 answer.append(" )");
106                 return answer.toString();
107             }
108             
109             public String JavaDoc getExpressionSymbol() {
110                 if( not )
111                     return "NOT IN";
112                 else
113                     return "IN";
114             }
115         };
116     }
117
118     abstract static class BooleanUnaryExpression extends UnaryExpression implements BooleanExpression {
119         public BooleanUnaryExpression(Expression left) {
120             super(left);
121         }
122
123         public boolean matches(MessageEvaluationContext message) throws JMSException JavaDoc {
124             Object JavaDoc object = evaluate(message);
125             return object!=null && object==Boolean.TRUE;
126         }
127     };
128
129         
130     public static BooleanExpression createNOT(BooleanExpression left) {
131         return new BooleanUnaryExpression(left) {
132             public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
133                 Boolean JavaDoc lvalue = (Boolean JavaDoc) right.evaluate(message);
134                 if (lvalue == null) {
135                     return null;
136                 }
137                 return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
138             }
139
140             public String JavaDoc getExpressionSymbol() {
141                 return "NOT";
142             }
143         };
144     }
145     
146     public static BooleanExpression createXPath(final String JavaDoc xpath) {
147         return new XPathExpression(xpath);
148     }
149
150     public static BooleanExpression createXQuery(final String JavaDoc xpath) {
151         return new XQueryExpression(xpath);
152     }
153
154     public static BooleanExpression createBooleanCast(Expression left) {
155         return new BooleanUnaryExpression(left) {
156             public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
157                 Object JavaDoc rvalue = right.evaluate(message);
158                 if (rvalue == null)
159                     return null;
160                 if (!rvalue.getClass().equals(Boolean JavaDoc.class))
161                     return Boolean.FALSE;
162                 return ((Boolean JavaDoc)rvalue).booleanValue() ? Boolean.TRUE : Boolean.FALSE;
163             }
164
165             public String JavaDoc toString() {
166                 return right.toString();
167             }
168
169             public String JavaDoc getExpressionSymbol() {
170                 return "";
171             }
172         };
173     }
174
175     private static Number JavaDoc negate(Number JavaDoc left) {
176         Class JavaDoc clazz = left.getClass();
177         if (clazz == Integer JavaDoc.class) {
178             return new Integer JavaDoc(-left.intValue());
179         }
180         else if (clazz == Long JavaDoc.class) {
181             return new Long JavaDoc(-left.longValue());
182         }
183         else if (clazz == Float JavaDoc.class) {
184             return new Float JavaDoc(-left.floatValue());
185         }
186         else if (clazz == Double JavaDoc.class) {
187             return new Double JavaDoc(-left.doubleValue());
188         }
189         else if (clazz == BigDecimal JavaDoc.class) {
190             // We ussually get a big deciamal when we have Long.MIN_VALUE constant in the
191
// Selector. Long.MIN_VALUE is too big to store in a Long as a positive so we store it
192
// as a Big decimal. But it gets Negated right away.. to here we try to covert it back
193
// to a Long.
194
BigDecimal JavaDoc bd = (BigDecimal JavaDoc)left;
195             bd = bd.negate();
196             
197             if( BD_LONG_MIN_VALUE.compareTo(bd)==0 ) {
198                 return new Long JavaDoc(Long.MIN_VALUE);
199             }
200             return bd;
201         }
202         else {
203             throw new RuntimeException JavaDoc("Don't know how to negate: "+left);
204         }
205     }
206
207     public UnaryExpression(Expression left) {
208         this.right = left;
209     }
210
211     public Expression getRight() {
212         return right;
213     }
214
215     public void setRight(Expression expression) {
216         right = expression;
217     }
218
219     /**
220      * @see java.lang.Object#toString()
221      */

222     public String JavaDoc toString() {
223         return "(" + getExpressionSymbol() + " " + right.toString() + ")";
224     }
225
226     /**
227      * TODO: more efficient hashCode()
228      *
229      * @see java.lang.Object#hashCode()
230      */

231     public int hashCode() {
232         return toString().hashCode();
233     }
234
235     /**
236      * TODO: more efficient hashCode()
237      *
238      * @see java.lang.Object#equals(java.lang.Object)
239      */

240     public boolean equals(Object JavaDoc o) {
241
242         if (o == null || !this.getClass().equals(o.getClass())) {
243             return false;
244         }
245         return toString().equals(o.toString());
246
247     }
248
249     /**
250      * Returns the symbol that represents this binary expression. For example, addition is
251      * represented by "+"
252      *
253      * @return
254      */

255     abstract public String JavaDoc getExpressionSymbol();
256
257 }
258
Popular Tags