1 18 package org.apache.activemq.filter; 19 20 import javax.jms.JMSException ; 21 22 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 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 evaluate(Object lvalue, Object rvalue) { 44 if (lvalue instanceof String ) { 45 String text = (String ) lvalue; 46 String answer = text + rvalue; 47 return answer; 48 } 49 else if (lvalue instanceof Number ) { 50 return plus((Number ) lvalue, asNumber(rvalue)); 51 } 52 throw new RuntimeException ("Cannot call plus operation on: " + lvalue + " and: " + rvalue); 53 } 54 55 public String 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 evaluate(Object lvalue, Object rvalue) { 64 if (lvalue instanceof Number ) { 65 return minus((Number ) lvalue, asNumber(rvalue)); 66 } 67 throw new RuntimeException ("Cannot call minus operation on: " + lvalue + " and: " + rvalue); 68 } 69 70 public String 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 evaluate(Object lvalue, Object rvalue) { 80 if (lvalue instanceof Number ) { 81 return multiply((Number ) lvalue, asNumber(rvalue)); 82 } 83 throw new RuntimeException ("Cannot call multiply operation on: " + lvalue + " and: " + rvalue); 84 } 85 86 public String 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 evaluate(Object lvalue, Object rvalue) { 96 if (lvalue instanceof Number ) { 97 return divide((Number ) lvalue, asNumber(rvalue)); 98 } 99 throw new RuntimeException ("Cannot call divide operation on: " + lvalue + " and: " + rvalue); 100 } 101 102 public String 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 evaluate(Object lvalue, Object rvalue) { 112 if (lvalue instanceof Number ) { 113 return mod((Number ) lvalue, asNumber(rvalue)); 114 } 115 throw new RuntimeException ("Cannot call mod operation on: " + lvalue + " and: " + rvalue); 116 } 117 118 public String getExpressionSymbol() { 119 return "%"; 120 } 121 }; 122 } 123 124 protected Number plus(Number left, Number right) { 125 switch (numberType(left, right)) { 126 case INTEGER: 127 return new Integer (left.intValue() + right.intValue()); 128 case LONG: 129 return new Long (left.longValue() + right.longValue()); 130 default: 131 return new Double (left.doubleValue() + right.doubleValue()); 132 } 133 } 134 135 protected Number minus(Number left, Number right) { 136 switch (numberType(left, right)) { 137 case INTEGER: 138 return new Integer (left.intValue() - right.intValue()); 139 case LONG: 140 return new Long (left.longValue() - right.longValue()); 141 default: 142 return new Double (left.doubleValue() - right.doubleValue()); 143 } 144 } 145 146 protected Number multiply(Number left, Number right) { 147 switch (numberType(left, right)) { 148 case INTEGER: 149 return new Integer (left.intValue() * right.intValue()); 150 case LONG: 151 return new Long (left.longValue() * right.longValue()); 152 default: 153 return new Double (left.doubleValue() * right.doubleValue()); 154 } 155 } 156 157 protected Number divide(Number left, Number right) { 158 return new Double (left.doubleValue() / right.doubleValue()); 159 } 160 161 protected Number mod(Number left, Number right) { 162 return new Double (left.doubleValue() % right.doubleValue()); 163 } 164 165 private int numberType(Number left, Number right) { 166 if (isDouble(left) || isDouble(right)) { 167 return DOUBLE; 168 } 169 else if (left instanceof Long || right instanceof Long ) { 170 return LONG; 171 } 172 else { 173 return INTEGER; 174 } 175 } 176 177 private boolean isDouble(Number n) { 178 return n instanceof Float || n instanceof Double ; 179 } 180 181 protected Number asNumber(Object value) { 182 if (value instanceof Number ) { 183 return (Number ) value; 184 } 185 else { 186 throw new RuntimeException ("Cannot convert value: " + value + " into a number"); 187 } 188 } 189 190 public Object evaluate(MessageEvaluationContext message) throws JMSException { 191 Object lvalue = left.evaluate(message); 192 if (lvalue == null) { 193 return null; 194 } 195 Object rvalue = right.evaluate(message); 196 if (rvalue == null) { 197 return null; 198 } 199 return evaluate(lvalue, rvalue); 200 } 201 202 203 208 abstract protected Object evaluate(Object lvalue, Object rvalue); 209 210 } 211 | Popular Tags |