001: /**
002: *
003: * Copyright 2004 Protique Ltd
004: * Copyright 2004 Hiram Chirino
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * 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.activemq.filter.mockrunner;
019:
020: import javax.jms.JMSException;
021: import javax.jms.Message;
022:
023: /**
024: * Alwin Ibba: Changed package
025: *
026: * An expression which performs an operation on two expression values
027: *
028: * @version $Revision: 1.3 $
029: */
030: public abstract class ArithmeticExpression extends BinaryExpression {
031:
032: protected static final int INTEGER = 1;
033: protected static final int LONG = 2;
034: protected static final int DOUBLE = 3;
035:
036: /**
037: * @param left
038: * @param right
039: */
040: public ArithmeticExpression(Expression left, Expression right) {
041: super (left, right);
042: }
043:
044: public static Expression createPlus(Expression left,
045: Expression right) {
046: return new ArithmeticExpression(left, right) {
047: protected Object evaluate(Object lvalue, Object rvalue) {
048: if (lvalue instanceof String) {
049: String text = (String) lvalue;
050: String answer = text + rvalue;
051: System.out.println("lvalue: " + lvalue
052: + " rvalue: " + rvalue + " result: "
053: + answer);
054: return answer;
055: } else if (lvalue instanceof Number) {
056: return plus((Number) lvalue, asNumber(rvalue));
057: }
058: throw new RuntimeException(
059: "Cannot call plus operation on: " + lvalue
060: + " and: " + rvalue);
061: }
062:
063: public String getExpressionSymbol() {
064: return "+";
065: }
066: };
067: }
068:
069: public static Expression createMinus(Expression left,
070: Expression right) {
071: return new ArithmeticExpression(left, right) {
072: protected Object evaluate(Object lvalue, Object rvalue) {
073: if (lvalue instanceof Number) {
074: return minus((Number) lvalue, asNumber(rvalue));
075: }
076: throw new RuntimeException(
077: "Cannot call minus operation on: " + lvalue
078: + " and: " + rvalue);
079: }
080:
081: public String getExpressionSymbol() {
082: return "-";
083: }
084: };
085: }
086:
087: public static Expression createMultiply(Expression left,
088: Expression right) {
089: return new ArithmeticExpression(left, right) {
090:
091: protected Object evaluate(Object lvalue, Object rvalue) {
092: if (lvalue instanceof Number) {
093: return multiply((Number) lvalue, asNumber(rvalue));
094: }
095: throw new RuntimeException(
096: "Cannot call multiply operation on: " + lvalue
097: + " and: " + rvalue);
098: }
099:
100: public String getExpressionSymbol() {
101: return "*";
102: }
103: };
104: }
105:
106: public static Expression createDivide(Expression left,
107: Expression right) {
108: return new ArithmeticExpression(left, right) {
109:
110: protected Object evaluate(Object lvalue, Object rvalue) {
111: if (lvalue instanceof Number) {
112: return divide((Number) lvalue, asNumber(rvalue));
113: }
114: throw new RuntimeException(
115: "Cannot call divide operation on: " + lvalue
116: + " and: " + rvalue);
117: }
118:
119: public String getExpressionSymbol() {
120: return "/";
121: }
122: };
123: }
124:
125: public static Expression createMod(Expression left, Expression right) {
126: return new ArithmeticExpression(left, right) {
127:
128: protected Object evaluate(Object lvalue, Object rvalue) {
129: if (lvalue instanceof Number) {
130: return mod((Number) lvalue, asNumber(rvalue));
131: }
132: throw new RuntimeException(
133: "Cannot call mod operation on: " + lvalue
134: + " and: " + rvalue);
135: }
136:
137: public String getExpressionSymbol() {
138: return "%";
139: }
140: };
141: }
142:
143: protected Number plus(Number left, Number right) {
144: switch (numberType(left, right)) {
145: case INTEGER:
146: return new Integer(left.intValue() + right.intValue());
147: case LONG:
148: return new Long(left.longValue() + right.longValue());
149: default:
150: return new Double(left.doubleValue() + right.doubleValue());
151: }
152: }
153:
154: protected Number minus(Number left, Number right) {
155: switch (numberType(left, right)) {
156: case INTEGER:
157: return new Integer(left.intValue() - right.intValue());
158: case LONG:
159: return new Long(left.longValue() - right.longValue());
160: default:
161: return new Double(left.doubleValue() - right.doubleValue());
162: }
163: }
164:
165: protected Number multiply(Number left, Number right) {
166: switch (numberType(left, right)) {
167: case INTEGER:
168: return new Integer(left.intValue() * right.intValue());
169: case LONG:
170: return new Long(left.longValue() * right.longValue());
171: default:
172: return new Double(left.doubleValue() * right.doubleValue());
173: }
174: }
175:
176: protected Number divide(Number left, Number right) {
177: return new Double(left.doubleValue() / right.doubleValue());
178: }
179:
180: protected Number mod(Number left, Number right) {
181: return new Double(left.doubleValue() % right.doubleValue());
182: }
183:
184: private int numberType(Number left, Number right) {
185: if (isDouble(left) || isDouble(right)) {
186: return DOUBLE;
187: } else if (left instanceof Long || right instanceof Long) {
188: return LONG;
189: } else {
190: return INTEGER;
191: }
192: }
193:
194: private boolean isDouble(Number n) {
195: return n instanceof Float || n instanceof Double;
196: }
197:
198: protected Number asNumber(Object value) {
199: if (value instanceof Number) {
200: return (Number) value;
201: } else {
202: throw new RuntimeException("Cannot convert value: " + value
203: + " into a number");
204: }
205: }
206:
207: public Object evaluate(Message message) throws JMSException {
208: Object lvalue = left.evaluate(message);
209: if (lvalue == null) {
210: return null;
211: }
212: Object rvalue = right.evaluate(message);
213: if (rvalue == null) {
214: return null;
215: }
216: return evaluate(lvalue, rvalue);
217: }
218:
219: /**
220: * @param lvalue
221: * @param rvalue
222: * @return
223: */
224: abstract protected Object evaluate(Object lvalue, Object rvalue);
225:
226: }
|