1 /*** 2 * 3 * Copyright 2004 Hiram Chirino 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 **/ 18 package org.codehaus.activemq.filter; 19 20 import javax.jms.JMSException; 21 import javax.jms.Message; 22 23 /*** 24 * An expression which performs an operation on two expression values 25 * 26 * @version $Revision: 1.1 $ 27 */ 28 public abstract class UnaryExpression implements Expression { 29 30 protected Expression right; 31 32 public static Expression createNegate(Expression left) { 33 return new UnaryExpression(left) { 34 public Object evaluate(Message message) throws JMSException { 35 Object lvalue = right.evaluate(message); 36 if (lvalue == null) { 37 return null; 38 } 39 if (lvalue instanceof Number) { 40 return negate((Number) lvalue); 41 } 42 throw new RuntimeException("Cannot call negate operation on: " + lvalue); 43 } 44 45 public String getExpressionSymbol() { 46 return "-"; 47 } 48 }; 49 } 50 51 abstract static class BooleanUnaryExpression extends UnaryExpression implements BooleanExpression { 52 public BooleanUnaryExpression(Expression left) { 53 super(left); 54 } 55 }; 56 57 public static BooleanExpression createNOT(BooleanExpression left) { 58 return new BooleanUnaryExpression(left) { 59 public Object evaluate(Message message) throws JMSException { 60 Boolean lvalue = (Boolean) right.evaluate(message); 61 if (lvalue == null) { 62 return null; 63 } 64 return lvalue.booleanValue() ? Boolean.FALSE : Boolean.TRUE; 65 } 66 67 public String getExpressionSymbol() { 68 return "NOT"; 69 } 70 }; 71 } 72 73 private static Number negate(Number left) { 74 if (left instanceof Integer) { 75 return new Integer(-left.intValue()); 76 } 77 else if (left instanceof Long) { 78 return new Long(-left.longValue()); 79 } 80 else { 81 return new Double(-left.doubleValue()); 82 } 83 } 84 85 public UnaryExpression(Expression left) { 86 this.right = left; 87 } 88 89 public Expression getRight() { 90 return right; 91 } 92 93 public void setRight(Expression expression) { 94 right = expression; 95 } 96 97 /*** 98 * @see java.lang.Object#toString() 99 */ 100 public String toString() { 101 return "(" + getExpressionSymbol() + " " + right.toString() + ")"; 102 } 103 104 /*** 105 * TODO: more efficient hashCode() 106 * 107 * @see java.lang.Object#hashCode() 108 */ 109 public int hashCode() { 110 return toString().hashCode(); 111 } 112 113 /*** 114 * TODO: more efficient hashCode() 115 * 116 * @see java.lang.Object#equals(java.lang.Object) 117 */ 118 public boolean equals(Object o) { 119 120 if (o == null || !this.getClass().equals(o.getClass())) { 121 return false; 122 } 123 return toString().equals(o.toString()); 124 125 } 126 127 /*** 128 * Returns the symbol that represents this binary expression. For example, addition is 129 * represented by "+" 130 * 131 * @return 132 */ 133 abstract public String getExpressionSymbol(); 134 135 }