1 10 package mondrian.calc; 11 12 import org.eigenbase.util.property.StringProperty; 13 import mondrian.olap.*; 14 import mondrian.util.ObjectFactory; 15 import mondrian.util.CreationException; 16 17 25 public interface ExpCompiler { 26 30 Evaluator getEvaluator(); 31 Validator getValidator(); 32 33 39 Calc compile(Exp exp); 40 41 49 Calc compile(Exp exp, ResultStyle[] preferredResultTypes); 50 51 54 MemberCalc compileMember(Exp exp); 55 56 59 LevelCalc compileLevel(Exp exp); 60 61 64 DimensionCalc compileDimension(Exp exp); 65 66 69 HierarchyCalc compileHierarchy(Exp exp); 70 71 75 IntegerCalc compileInteger(Exp exp); 76 77 81 StringCalc compileString(Exp exp); 82 83 89 ListCalc compileList(Exp exp); 90 91 102 ListCalc compileList(Exp exp, boolean mutable); 103 104 108 IterCalc compileIter(Exp exp); 109 110 113 BooleanCalc compileBoolean(Exp exp); 114 115 118 DoubleCalc compileDouble(Exp exp); 119 120 123 TupleCalc compileTuple(Exp exp); 124 125 136 Calc compileScalar(Exp exp, boolean convert); 137 138 145 ParameterSlot registerParameter(Parameter parameter); 146 147 151 ResultStyle[] getAcceptableResultStyles(); 152 153 160 enum ResultStyle { 161 164 ANY, 165 166 170 MUTABLE_LIST, 171 172 176 LIST, 177 178 182 ITERABLE, 183 184 189 VALUE 190 } 191 192 ResultStyle[] ANY_RESULT_STYLE_ARRAY = 193 new ResultStyle[] { 194 ResultStyle.ANY 195 }; 196 ResultStyle[] ITERABLE_RESULT_STYLE_ARRAY = 197 new ResultStyle[] { 198 ResultStyle.ITERABLE 199 }; 200 ResultStyle[] MUTABLE_LIST_RESULT_STYLE_ARRAY = 201 new ResultStyle[] { 202 ResultStyle.MUTABLE_LIST 203 }; 204 ResultStyle[] LIST_RESULT_STYLE_ARRAY = 205 new ResultStyle[] { 206 ResultStyle.LIST 207 }; 208 209 ResultStyle[] ITERABLE_ANY_RESULT_STYLE_ARRAY = 210 new ResultStyle[] { 211 ResultStyle.ITERABLE, 212 ResultStyle.ANY 213 }; 214 ResultStyle[] ITERABLE_LIST_RESULT_STYLE_ARRAY = 215 new ResultStyle[] { 216 ResultStyle.ITERABLE, 217 ResultStyle.LIST 218 }; 219 ResultStyle[] ITERABLE_MUTABLE_LIST_RESULT_STYLE_ARRAY = 220 new ResultStyle[] { 221 ResultStyle.ITERABLE, 222 ResultStyle.MUTABLE_LIST 223 }; 224 ResultStyle[] ITERABLE_LIST_MUTABLE_LIST_RESULT_STYLE_ARRAY = 225 new ResultStyle[] { 226 ResultStyle.ITERABLE, 227 ResultStyle.LIST, 228 ResultStyle.MUTABLE_LIST 229 }; 230 ResultStyle[] LIST_MUTABLE_LIST_RESULT_STYLE_ARRAY = 231 new ResultStyle[] { 232 ResultStyle.LIST, 233 ResultStyle.MUTABLE_LIST 234 }; 235 ResultStyle[] MUTABLE_LIST_LIST_RESULT_STYLE_ARRAY = 236 new ResultStyle[] { 237 ResultStyle.MUTABLE_LIST, 238 ResultStyle.LIST 239 }; 240 241 242 250 public class Factory extends ObjectFactory<ExpCompiler> { 251 private static final Factory factory; 252 private static final Class [] CLASS_ARRAY; 253 static { 254 factory = new Factory(); 255 CLASS_ARRAY = new Class [] { 256 Evaluator.class, 257 Validator.class, 258 ResultStyle[].class 259 }; 260 } 261 262 271 public static ExpCompiler getExpCompiler(final Evaluator evaluator, 272 final Validator validator) 273 throws CreationException { 274 return getExpCompiler(evaluator, validator, ANY_RESULT_STYLE_ARRAY); 275 } 276 277 287 public static ExpCompiler getExpCompiler(final Evaluator evaluator, 288 final Validator validator, 289 final ResultStyle[] resultStyles) 290 throws CreationException { 291 return factory.getObject(CLASS_ARRAY, 292 new Object [] { 293 evaluator, 294 validator, 295 resultStyles 296 }); 297 } 298 299 304 private static final ThreadLocal <String > ClassName = 305 new ThreadLocal <String >(); 306 307 313 public static String getThreadLocalClassName() { 314 return ClassName.get(); 315 } 316 325 public static void setThreadLocalClassName(String className) { 326 ClassName.set(className); 327 } 328 334 public static void clearThreadLocalClassName() { 335 ClassName.set(null); 336 } 337 338 343 private Factory() { 344 super(ExpCompiler.class); 345 } 346 351 protected String getClassName() { 352 return getThreadLocalClassName(); 353 } 354 355 360 protected StringProperty getStringProperty() { 361 return MondrianProperties.instance().ExpCompilerClass; 362 } 363 376 protected ExpCompiler getDefault(final Class [] parameterTypes, 377 final Object [] parameterValues) 378 throws CreationException { 379 Evaluator evaluator = (Evaluator) parameterValues[0]; 381 Validator validator = (Validator) parameterValues[1]; 382 ResultStyle[] resultStyles = (ResultStyle[]) parameterValues[2]; 383 384 return new mondrian.calc.impl.BetterExpCompiler( 389 evaluator, validator, resultStyles); 390 } 391 392 399 public static Factory getFactory() { 400 return factory; 401 } 402 403 410 public Object removeContext() { 411 return new Context(); 412 } 413 414 421 public void restoreContext(final Object context) { 422 if (context instanceof Context) { 423 ((Context) context).restore(); 424 } 425 } 426 427 436 public static class Context implements ObjectFactory.Context { 437 private final String threadLocalClassName; 438 private final String systemPropertyClassName; 439 Context() { 440 this.threadLocalClassName = 441 ExpCompiler.Factory.getThreadLocalClassName(); 442 if (this.threadLocalClassName != null) { 443 ExpCompiler.Factory.clearThreadLocalClassName(); 444 } 445 446 this.systemPropertyClassName = 447 System.getProperty(ExpCompiler.class.getName()); 448 if (this.systemPropertyClassName != null) { 449 System.getProperties().remove(ExpCompiler.class.getName()); 450 } 451 } 452 private void restore() { 453 if (this.threadLocalClassName != null) { 454 ExpCompiler.Factory.setThreadLocalClassName( 455 this.threadLocalClassName); 456 } 457 if (this.systemPropertyClassName != null) { 458 System.setProperty(ExpCompiler.class.getName(), 459 this.systemPropertyClassName); 460 } 461 } 462 } 463 } 464 } 465 466 | Popular Tags |