1 18 package org.apache.activemq.filter; 19 20 import java.math.BigDecimal ; 21 import java.util.Collection ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.jms.JMSException ; 27 28 33 public abstract class UnaryExpression implements Expression { 34 35 private static final BigDecimal 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 evaluate(MessageEvaluationContext message) throws JMSException { 41 Object rvalue = right.evaluate(message); 42 if (rvalue == null) { 43 return null; 44 } 45 if (rvalue instanceof Number ) { 46 return negate((Number ) rvalue); 47 } 48 return null; 49 } 50 51 public String getExpressionSymbol() { 52 return "-"; 53 } 54 }; 55 } 56 57 public static BooleanExpression createInExpression(PropertyExpression right, List elements, final boolean not) { 58 59 Collection t; 61 if( elements.size()==0 ) 62 t=null; 63 else if( elements.size() < 5 ) 64 t = elements; 65 else { 66 t = new HashSet (elements); 67 } 68 final Collection inList = t; 69 70 return new BooleanUnaryExpression(right) { 71 public Object evaluate(MessageEvaluationContext message) throws JMSException { 72 73 Object rvalue = right.evaluate(message); 74 if (rvalue == null) { 75 return null; 76 } 77 if( rvalue.getClass()!=String .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 toString() { 89 StringBuffer answer = new StringBuffer (); 90 answer.append(right); 91 answer.append(" "); 92 answer.append(getExpressionSymbol()); 93 answer.append(" ( "); 94 95 int count=0; 96 for (Iterator i = inList.iterator(); i.hasNext();) { 97 Object o = (Object ) 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 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 { 124 Object 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 evaluate(MessageEvaluationContext message) throws JMSException { 133 Boolean lvalue = (Boolean ) right.evaluate(message); 134 if (lvalue == null) { 135 return null; 136 } 137 return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE; 138 } 139 140 public String getExpressionSymbol() { 141 return "NOT"; 142 } 143 }; 144 } 145 146 public static BooleanExpression createXPath(final String xpath) { 147 return new XPathExpression(xpath); 148 } 149 150 public static BooleanExpression createXQuery(final String xpath) { 151 return new XQueryExpression(xpath); 152 } 153 154 public static BooleanExpression createBooleanCast(Expression left) { 155 return new BooleanUnaryExpression(left) { 156 public Object evaluate(MessageEvaluationContext message) throws JMSException { 157 Object rvalue = right.evaluate(message); 158 if (rvalue == null) 159 return null; 160 if (!rvalue.getClass().equals(Boolean .class)) 161 return Boolean.FALSE; 162 return ((Boolean )rvalue).booleanValue() ? Boolean.TRUE : Boolean.FALSE; 163 } 164 165 public String toString() { 166 return right.toString(); 167 } 168 169 public String getExpressionSymbol() { 170 return ""; 171 } 172 }; 173 } 174 175 private static Number negate(Number left) { 176 Class clazz = left.getClass(); 177 if (clazz == Integer .class) { 178 return new Integer (-left.intValue()); 179 } 180 else if (clazz == Long .class) { 181 return new Long (-left.longValue()); 182 } 183 else if (clazz == Float .class) { 184 return new Float (-left.floatValue()); 185 } 186 else if (clazz == Double .class) { 187 return new Double (-left.doubleValue()); 188 } 189 else if (clazz == BigDecimal .class) { 190 BigDecimal bd = (BigDecimal )left; 195 bd = bd.negate(); 196 197 if( BD_LONG_MIN_VALUE.compareTo(bd)==0 ) { 198 return new Long (Long.MIN_VALUE); 199 } 200 return bd; 201 } 202 else { 203 throw new RuntimeException ("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 222 public String toString() { 223 return "(" + getExpressionSymbol() + " " + right.toString() + ")"; 224 } 225 226 231 public int hashCode() { 232 return toString().hashCode(); 233 } 234 235 240 public boolean equals(Object 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 255 abstract public String getExpressionSymbol(); 256 257 } 258 | Popular Tags |