KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > calc > impl > BetterExpCompiler


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/calc/impl/BetterExpCompiler.java#6 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2006-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.calc.impl;
11
12 import mondrian.olap.*;
13 import mondrian.calc.*;
14
15 import java.util.List JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 /**
19  * Enhanced expression compiler. It can generate code to convert between
20  * scalar types.
21  *
22  * @author jhyde
23  * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/BetterExpCompiler.java#6 $
24  * @since Sep 29, 2005
25  */

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             // Wrap the expression in an expression which creates a mutable
72
// copy.
73
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 JavaDoc evaluateList(Evaluator evaluator) {
87             List JavaDoc list = listCalc.evaluateList(evaluator);
88             return new ArrayList JavaDoc(list);
89         }
90     }
91 }
92
93 // End BetterExpCompiler.java
94
Popular Tags