1 21 package com.presumo.jms.selector; 22 23 import java.util.Hashtable ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import javax.jms.Message ; 27 28 36 public abstract class JmsOperand 37 { 38 39 protected static short evalNumber = Short.MIN_VALUE + 1; 40 protected short myEvalNumber = Short.MIN_VALUE; 41 42 protected JmsOperand lastEval = null; 43 private final boolean tracked; 44 protected int refcount; 45 46 47 50 protected Hashtable parents = null; 51 52 static private ArrayList allInstances = new ArrayList (); 53 54 static ArrayList getAllInstances() { return allInstances; }; 56 57 static final void resetStoredEvals() 58 { 59 ++evalNumber; 60 if (evalNumber == Short.MAX_VALUE) { 64 65 for (int i=0; i < allInstances.size(); i++) { 66 ( (JmsOperand) allInstances.get(i)).myEvalNumber = Short.MIN_VALUE; 67 } 68 evalNumber = Short.MIN_VALUE+1; 69 } 70 } 71 72 77 protected JmsOperand() 78 { 79 this(true); 80 } 81 82 89 protected JmsOperand(boolean tracked) 90 { 91 if (tracked) { 92 allInstances.add(this); 93 } 94 refcount = 1; 95 this.tracked = tracked; 96 97 } 98 99 void incrementRefCount() 100 { 101 refcount++; 102 } 103 104 107 void incrementAllRefCounts() 108 { 109 refcount++; 110 } 111 112 116 protected final Enumeration allParents() 117 { 118 return parents.keys(); 119 } 120 121 protected final int numberOfParents() 122 { 123 if (parents == null) 124 return 0; 125 else 126 return parents.size(); 127 } 128 129 protected final boolean hasParent(JmsOperand parent) { 130 if (parents == null) 131 return false; 132 else 133 return (parents.get(parent) != null); 134 } 135 136 protected final void addParent(JmsOperand parent) { 137 if (parents == null) { 138 parents = new Hashtable (10); 139 } 140 parents.put(parent, new Integer (1)); 141 142 } 143 144 protected final void addParentRef(JmsOperand parent) { 145 if (parents == null) 146 throw new IllegalStateException ("Filter error: addParentRef called with no parents"); 147 Integer parentRefCount = (Integer ) parents.get(parent); 148 149 if (parentRefCount == null) 150 throw new IllegalStateException ("Filter error: addParentRef called on non-existant parent: " + parent + " this: " + this); 151 152 parentRefCount = new Integer (parentRefCount.intValue() + 1); 153 parents.remove(parent); 154 parents.put(parent, parentRefCount); 155 } 156 157 protected final void removeParent(JmsOperand parent) { 158 if (parents == null || parent == null) return; 159 160 Integer references = (Integer ) parents.get(parent); 161 if (references == null) return; 162 163 int temp = references.intValue() - 1; 164 if (temp == 0) { 165 parents.remove(parent); 166 if (parents.size() == 0) 167 parents = null; 168 } 169 else { 170 parents.remove(parent); 171 parents.put(parent, references); 172 references = new Integer (references.intValue() - 1); 173 } 174 175 } 176 177 178 186 abstract JmsOperand evaluate(Message msg) throws SelectorFalseException; 187 188 197 JmsOperand evaluateOnce(Message msg) throws SelectorFalseException 198 { 199 if (myEvalNumber != evalNumber) { 200 lastEval = evaluate(msg); myEvalNumber = evalNumber; } else { 205 } 207 return lastEval; 208 } 209 210 214 abstract String unParse(); 215 216 221 void delete() 222 { 223 delete(null); 224 } 225 226 protected void delete(JmsOperand fromParent) 227 { 228 if (refcount == 0) return; 230 refcount--; 231 232 removeParent(fromParent); 239 240 if (refcount == 0) { 241 242 if (tracked) 243 allInstances.remove(this); 244 } 245 } 246 247 248 void printTree(int level) { 249 for (int i=0; i < level * 2; i++) System.out.print(" "); 250 System.out.print(this); 251 System.out.println(" Refcount[" + refcount +"]"); 252 } 253 254 abstract short getType(); 255 256 static final short JMS_EQUALS = 0; 257 static final short JMS_INTEGER_LITERAL = 1; 258 static final short JMS_IDENTIFIER = 2; 259 static final short JMS_BOOLEAN_LITERAL = 3; 260 static final short JMS_STRING_LITERAL = 4; 261 static final short JMS_FLOAT_LITERAL = 5; 262 static final short JMS_LESS = 6; 263 static final short JMS_GREATER = 7; 264 static final short JMS_NOT = 8; 265 static final short JMS_BINARY_OR = 9; 266 static final short JMS_BINARY_AND = 10; 267 static final short JMS_IS = 11; 268 static final short JMS_BINARY_MULT = 12; 269 static final short JMS_BINARY_DIV = 13; 270 static final short JMS_BINARY_ADD = 14; 271 static final short JMS_BINARY_SUB = 15; 272 static final short JMS_LIKE = 16; 273 274 } 275 276 277 278 279 280 281 282 | Popular Tags |