1 package com.calipso.reportgenerator.reportcalculator.expression; 2 3 import java.util.HashSet ; 4 import java.util.Iterator ; 5 import java.util.Set ; 6 import java.io.Serializable ; 7 8 13 14 public class Expression implements Serializable { 15 Expression[] arguments; 16 Set referencesCache; 17 18 protected void initialize() { 19 } 20 21 26 public boolean equals(Object o) { 27 if (this == o) return true; 28 if (!(o instanceof Expression)) return false; 29 30 final Expression expression = (Expression) o; 31 32 if (referencesCache != null ? !referencesCache.equals(expression.referencesCache) : expression.referencesCache != null) return false; 33 34 return true; 35 } 36 37 41 public int hashCode() { 42 return (referencesCache != null ? referencesCache.hashCode() : 0); 43 } 44 45 49 public Expression[] getArguments() { 50 return arguments; 51 } 52 53 57 public void setArguments(Expression[] arguments) { 58 invalidateCache(); 59 this.arguments = arguments; 60 } 61 62 65 private void invalidateCache() { 66 referencesCache = null; 67 } 68 69 76 public Set references() { 77 Iterator iterator; 78 79 if (referencesCache == null) { 80 referencesCache = new HashSet (); 81 iterator = variables().iterator(); 82 while (iterator.hasNext()) { 83 referencesCache.add(((VariableExp) iterator.next()).getReference()); 84 } 85 } 86 return referencesCache; 87 } 88 89 93 protected Set variables() { 94 Set variablesCache; 95 96 variablesCache = new HashSet (); 97 for (int i = 0; i < arguments.length; i++) { 98 variablesCache.addAll(arguments[i].variables()); 99 } 100 return variablesCache; 101 } 102 103 107 public void setReferencesCache(Set referencesCache) { 108 this.referencesCache = referencesCache; 109 } 110 111 116 117 public Expression newAnd(Expression expression) { 118 return expression.basicAnd(this); 119 } 120 121 126 public Expression newOr(Expression expression) { 127 return expression.basicOr(this); 128 } 129 130 131 136 137 public Expression newLessThan(Expression expression) { 138 return new LessThan(this, expression); 139 } 140 141 142 147 public Expression newLessOrEquealTo(Expression expression) { 148 return new LessOrEqualTo(this, expression); 149 } 150 151 152 157 public Expression newEqualTo(Expression expression) { 158 return new EqualTo(this, expression); 159 } 160 161 166 167 public Expression newGreaterThan(Expression expression) { 168 return new GreaterThan(this, expression); 169 } 170 171 176 177 public Expression newGreaterOrEqualTo(Expression expression) { 178 return new GreaterOrEqualTo(this, expression); 179 } 180 181 182 186 public Expression newNot() { 187 return new NotExp(this); 188 } 189 190 196 public Expression newBeginsWith(Expression expression) { 197 return new BeginsWithExp(this, expression); 198 } 199 200 206 public Expression newEndsWith(Expression expression) { 207 return new EndsWithExp(this, expression); 208 } 209 210 216 public Expression newIncludes(Expression expression) { 217 return new IncludesExp(this, expression); 218 } 219 220 225 protected Expression basicAnd(Expression expression) { 226 return new AndExp(expression, this); 227 } 228 229 230 235 protected Expression basicOr(Expression expression) { 236 return new OrExp(expression, this); 237 } 238 239 240 244 public String toString() { 245 return basicAsString(); 246 } 247 248 253 protected String asStringUnderAnd() { 254 return basicAsString(); 255 } 256 257 261 protected String asStringUnderOr() { 262 return basicAsString(); 263 } 264 265 269 protected String asStringUnderNot() { 270 return basicAsString(); 271 } 272 273 277 278 protected String basicAsString() { 279 return ""; 280 } 281 282 283 289 public Object visitedBy(ExpressionVisitor visitor) { 290 return null; 291 } 292 293 294 298 public boolean isConstant() { 299 return false; 300 } 301 302 306 public boolean isFalse() { 307 return false; 308 } 309 310 314 public boolean isMagnitude() { 315 return false; 316 } 317 318 322 public boolean isTrue() { 323 return false; 324 } 325 326 330 public boolean isVariable() { 331 return false; 332 } 333 334 340 public Expression simplifiedIn(Context context) { 341 return (Expression) ((new ExpressionSimplifier()).simplifyIn(this, context)); 342 } 343 344 351 public boolean valueIn(Context context) { 352 return simplifiedIn(context).isTrue(); 353 } 354 } | Popular Tags |