KickJava   Java API By Example, From Geeks To Geeks.

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


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  * An expression which performs an operation on two expression values
24  *
25  * @version $Revision: 1.2 $
26  */

27 public abstract class ArithmeticExpression extends BinaryExpression {
28
29     protected static final int INTEGER = 1;
30     protected static final int LONG = 2;
31     protected static final int DOUBLE = 3;
32
33     /**
34      * @param left
35      * @param right
36      */

37     public ArithmeticExpression(Expression left, Expression right) {
38         super(left, right);
39     }
40
41     public static Expression createPlus(Expression left, Expression right) {
42         return new ArithmeticExpression(left, right) {
43             protected Object JavaDoc evaluate(Object JavaDoc lvalue, Object JavaDoc rvalue) {
44                 if (lvalue instanceof String JavaDoc) {
45                     String JavaDoc text = (String JavaDoc) lvalue;
46                     String JavaDoc answer = text + rvalue;
47                     return answer;
48                 }
49                 else if (lvalue instanceof Number JavaDoc) {
50                     return plus((Number JavaDoc) lvalue, asNumber(rvalue));
51                 }
52                 throw new RuntimeException JavaDoc("Cannot call plus operation on: " + lvalue + " and: " + rvalue);
53             }
54
55             public String JavaDoc getExpressionSymbol() {
56                 return "+";
57             }
58         };
59     }
60
61     public static Expression createMinus(Expression left, Expression right) {
62         return new ArithmeticExpression(left, right) {
63             protected Object JavaDoc evaluate(Object JavaDoc lvalue, Object JavaDoc rvalue) {
64                 if (lvalue instanceof Number JavaDoc) {
65                     return minus((Number JavaDoc) lvalue, asNumber(rvalue));
66                 }
67                 throw new RuntimeException JavaDoc("Cannot call minus operation on: " + lvalue + " and: " + rvalue);
68             }
69
70             public String JavaDoc getExpressionSymbol() {
71                 return "-";
72             }
73         };
74     }
75
76     public static Expression createMultiply(Expression left, Expression right) {
77         return new ArithmeticExpression(left, right) {
78
79             protected Object JavaDoc evaluate(Object JavaDoc lvalue, Object JavaDoc rvalue) {
80                 if (lvalue instanceof Number JavaDoc) {
81                     return multiply((Number JavaDoc) lvalue, asNumber(rvalue));
82                 }
83                 throw new RuntimeException JavaDoc("Cannot call multiply operation on: " + lvalue + " and: " + rvalue);
84             }
85
86             public String JavaDoc getExpressionSymbol() {
87                 return "*";
88             }
89         };
90     }
91
92     public static Expression createDivide(Expression left, Expression right) {
93         return new ArithmeticExpression(left, right) {
94
95             protected Object JavaDoc evaluate(Object JavaDoc lvalue, Object JavaDoc rvalue) {
96                 if (lvalue instanceof Number JavaDoc) {
97                     return divide((Number JavaDoc) lvalue, asNumber(rvalue));
98                 }
99                 throw new RuntimeException JavaDoc("Cannot call divide operation on: " + lvalue + " and: " + rvalue);
100             }
101
102             public String JavaDoc getExpressionSymbol() {
103                 return "/";
104             }
105         };
106     }
107
108     public static Expression createMod(Expression left, Expression right) {
109         return new ArithmeticExpression(left, right) {
110
111             protected Object JavaDoc evaluate(Object JavaDoc lvalue, Object JavaDoc rvalue) {
112                 if (lvalue instanceof Number JavaDoc) {
113                     return mod((Number JavaDoc) lvalue, asNumber(rvalue));
114                 }
115                 throw new RuntimeException JavaDoc("Cannot call mod operation on: " + lvalue + " and: " + rvalue);
116             }
117
118             public String JavaDoc getExpressionSymbol() {
119                 return "%";
120             }
121         };
122     }
123
124     protected Number JavaDoc plus(Number JavaDoc left, Number JavaDoc right) {
125         switch (numberType(left, right)) {
126             case INTEGER:
127                 return new Integer JavaDoc(left.intValue() + right.intValue());
128             case LONG:
129                 return new Long JavaDoc(left.longValue() + right.longValue());
130             default:
131                 return new Double JavaDoc(left.doubleValue() + right.doubleValue());
132         }
133     }
134
135     protected Number JavaDoc minus(Number JavaDoc left, Number JavaDoc right) {
136         switch (numberType(left, right)) {
137             case INTEGER:
138                 return new Integer JavaDoc(left.intValue() - right.intValue());
139             case LONG:
140                 return new Long JavaDoc(left.longValue() - right.longValue());
141             default:
142                 return new Double JavaDoc(left.doubleValue() - right.doubleValue());
143         }
144     }
145
146     protected Number JavaDoc multiply(Number JavaDoc left, Number JavaDoc right) {
147         switch (numberType(left, right)) {
148             case INTEGER:
149                 return new Integer JavaDoc(left.intValue() * right.intValue());
150             case LONG:
151                 return new Long JavaDoc(left.longValue() * right.longValue());
152             default:
153                 return new Double JavaDoc(left.doubleValue() * right.doubleValue());
154         }
155     }
156
157     protected Number JavaDoc divide(Number JavaDoc left, Number JavaDoc right) {
158         return new Double JavaDoc(left.doubleValue() / right.doubleValue());
159     }
160
161     protected Number JavaDoc mod(Number JavaDoc left, Number JavaDoc right) {
162         return new Double JavaDoc(left.doubleValue() % right.doubleValue());
163     }
164
165     private int numberType(Number JavaDoc left, Number JavaDoc right) {
166         if (isDouble(left) || isDouble(right)) {
167             return DOUBLE;
168         }
169         else if (left instanceof Long JavaDoc || right instanceof Long JavaDoc) {
170             return LONG;
171         }
172         else {
173             return INTEGER;
174         }
175     }
176
177     private boolean isDouble(Number JavaDoc n) {
178         return n instanceof Float JavaDoc || n instanceof Double JavaDoc;
179     }
180
181     protected Number JavaDoc asNumber(Object JavaDoc value) {
182         if (value instanceof Number JavaDoc) {
183             return (Number JavaDoc) value;
184         }
185         else {
186             throw new RuntimeException JavaDoc("Cannot convert value: " + value + " into a number");
187         }
188     }
189
190     public Object JavaDoc evaluate(MessageEvaluationContext message) throws JMSException JavaDoc {
191         Object JavaDoc lvalue = left.evaluate(message);
192         if (lvalue == null) {
193             return null;
194         }
195         Object JavaDoc rvalue = right.evaluate(message);
196         if (rvalue == null) {
197             return null;
198         }
199         return evaluate(lvalue, rvalue);
200     }
201
202
203     /**
204      * @param lvalue
205      * @param rvalue
206      * @return
207      */

208     abstract protected Object JavaDoc evaluate(Object JavaDoc lvalue, Object JavaDoc rvalue);
209
210 }
211
Popular Tags