1 28 29 package com.caucho.jms.selector; 30 31 import javax.jms.InvalidSelectorException ; 32 import javax.jms.JMSException ; 33 import javax.jms.Message ; 34 35 38 public class BooleanBinarySelector extends Selector { 39 private int _token; 40 private Selector _left; 41 private Selector _right; 42 43 BooleanBinarySelector(int token, Selector left, Selector right) 44 throws JMSException 45 { 46 _token = token; 47 _left = left; 48 _right = right; 49 50 switch (_token) { 51 case SelectorParser.LT: 52 case SelectorParser.LE: 53 case SelectorParser.GT: 54 case SelectorParser.GE: 55 if (! _left.isUnknown() && ! _left.isNumber()) 56 throw new InvalidSelectorException (L.l("'{0}' must have a numeric value for comparison.", 57 this)); 58 if (! _right.isUnknown() && ! _right.isNumber()) 59 throw new InvalidSelectorException (L.l("'{0}' must have a numeric value for comparison.", 60 this)); 61 break; 62 case SelectorParser.EQ: 63 case SelectorParser.NE: 64 if (_left.isUnknown() || _right.isUnknown()) { 65 } 66 else if (_left.isNumber() != _right.isNumber()) 67 throw new InvalidSelectorException (L.l("'{0}' test must have matching types.", 68 this)); 69 break; 70 71 case SelectorParser.AND: 72 case SelectorParser.OR: 73 if (! _left.isUnknown() && ! _left.isBoolean()) 74 throw new InvalidSelectorException (L.l("'{0}' must have a numeric value for comparison.", 75 this)); 76 if (! _right.isUnknown() && ! _right.isBoolean()) 77 throw new InvalidSelectorException (L.l("'{0}' must have a numeric value for comparison.", 78 this)); 79 break; 80 } 81 } 82 83 86 boolean isBoolean() 87 { 88 return true; 89 } 90 91 94 boolean isUnknown() 95 { 96 return false; 97 } 98 99 103 public Object evaluate(Message message) 104 throws JMSException 105 { 106 Object lvalue = _left.evaluate(message); 107 Object rvalue = _right.evaluate(message); 108 109 if (lvalue == null || rvalue == null) 110 return NULL; 111 112 switch (_token) { 113 case SelectorParser.EQ: 114 if (lvalue instanceof Number && rvalue instanceof Number ) 115 return toBoolean(((Number ) lvalue).doubleValue() == 116 ((Number ) rvalue).doubleValue()); 117 else 118 return toBoolean(lvalue.equals(rvalue)); 119 120 case SelectorParser.NE: 121 if (lvalue instanceof Number && rvalue instanceof Number ) 122 return toBoolean(! (((Number ) lvalue).doubleValue() == 123 ((Number ) rvalue).doubleValue())); 124 else 125 return toBoolean(! lvalue.equals(rvalue)); 126 127 case SelectorParser.LT: 128 if (lvalue instanceof Number && rvalue instanceof Number ) 129 return toBoolean(((Number ) lvalue).doubleValue() < 130 ((Number ) rvalue).doubleValue()); 131 else 132 return Boolean.FALSE; 133 134 case SelectorParser.LE: 135 if (lvalue instanceof Number && rvalue instanceof Number ) 136 return toBoolean(((Number ) lvalue).doubleValue() <= 137 ((Number ) rvalue).doubleValue()); 138 else 139 return Boolean.FALSE; 140 141 case SelectorParser.GT: 142 if (lvalue instanceof Number && rvalue instanceof Number ) 143 return toBoolean(((Number ) lvalue).doubleValue() > 144 ((Number ) rvalue).doubleValue()); 145 else 146 return Boolean.FALSE; 147 148 case SelectorParser.GE: 149 if (lvalue instanceof Number && rvalue instanceof Number ) 150 return toBoolean(((Number ) lvalue).doubleValue() >= 151 ((Number ) rvalue).doubleValue()); 152 else 153 return Boolean.FALSE; 154 155 case SelectorParser.AND: 156 if (! (lvalue instanceof Boolean )) 157 return NULL; 158 else if (! ((Boolean ) lvalue).booleanValue()) 159 return Boolean.FALSE; 160 else if (! (rvalue instanceof Boolean )) 161 return NULL; 162 else if (! ((Boolean ) rvalue).booleanValue()) 163 return Boolean.FALSE; 164 else 165 return Boolean.TRUE; 166 167 case SelectorParser.OR: 168 if ((lvalue instanceof Boolean ) && ((Boolean ) lvalue).booleanValue()) 169 return Boolean.TRUE; 170 else if ((rvalue instanceof Boolean ) && 171 ((Boolean ) rvalue).booleanValue()) 172 return Boolean.TRUE; 173 else if (lvalue instanceof Boolean || rvalue instanceof Boolean ) 174 return Boolean.FALSE; 175 else 176 return NULL; 177 178 default: 179 throw new JMSException ("NOTONE"); 180 } 181 } 182 183 public String toString() 184 { 185 switch (_token) { 186 case SelectorParser.EQ: 187 return _left + " = " + _right; 188 case SelectorParser.NE: 189 return _left + " <> " + _right; 190 case SelectorParser.LT: 191 return _left + " < " + _right; 192 case SelectorParser.LE: 193 return _left + " <= " + _right; 194 case SelectorParser.GT: 195 return _left + " > " + _right; 196 case SelectorParser.GE: 197 return _left + " >= " + _right; 198 case SelectorParser.AND: 199 return _left + " AND " + _right; 200 case SelectorParser.OR: 201 return _left + " OR " + _right; 202 default: 203 return super.toString(); 204 } 205 } 206 } 207 | Popular Tags |