1 10 package mondrian.calc.impl; 11 12 import mondrian.olap.*; 13 import mondrian.calc.*; 14 15 import java.util.List ; 16 import java.util.ArrayList ; 17 18 26 public class BetterExpCompiler extends AbstractExpCompiler { 27 public BetterExpCompiler(Evaluator evaluator, Validator validator) { 28 super(evaluator, validator); 29 } 30 public BetterExpCompiler(Evaluator evaluator, Validator validator, 31 ResultStyle[] resultStyles) { 32 super(evaluator, validator, resultStyles); 33 } 34 35 public DoubleCalc compileDouble(Exp exp) { 36 final Calc calc = compileScalar(exp, false); 37 if (calc instanceof DoubleCalc) { 38 return (DoubleCalc) calc; 39 } else if (calc instanceof IntegerCalc) { 40 final IntegerCalc integerCalc = (IntegerCalc) calc; 41 return new AbstractDoubleCalc(exp, new Calc[] {integerCalc}) { 42 public double evaluateDouble(Evaluator evaluator) { 43 final int result = integerCalc.evaluateInteger(evaluator); 44 return (double) result; 45 } 46 }; 47 } else { 48 throw Util.newInternal("cannot cast " + exp); 49 } 50 } 51 52 public TupleCalc compileTuple(Exp exp) { 53 final Calc calc = compile(exp); 54 if (calc instanceof TupleCalc) { 55 return (TupleCalc) calc; 56 } else if (calc instanceof MemberCalc) { 57 final MemberCalc memberCalc = (MemberCalc) calc; 58 return new AbstractTupleCalc(exp, new Calc[] {memberCalc}) { 59 public Member[] evaluateTuple(Evaluator evaluator) { 60 return new Member[] {memberCalc.evaluateMember(evaluator)}; 61 } 62 }; 63 } else { 64 throw Util.newInternal("cannot cast " + exp); 65 } 66 } 67 68 public ListCalc compileList(Exp exp, boolean mutable) { 69 final ListCalc listCalc = super.compileList(exp, mutable); 70 if (mutable && listCalc.getResultStyle() == ResultStyle.LIST) { 71 return new CopyListCalc(listCalc); 74 } 75 return listCalc; 76 } 77 78 private static class CopyListCalc extends AbstractListCalc { 79 private final ListCalc listCalc; 80 81 public CopyListCalc(ListCalc listCalc) { 82 super(new DummyExp(listCalc.getType()), new Calc[]{listCalc}); 83 this.listCalc = listCalc; 84 } 85 86 public List evaluateList(Evaluator evaluator) { 87 List list = listCalc.evaluateList(evaluator); 88 return new ArrayList (list); 89 } 90 } 91 } 92 93 | Popular Tags |