KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/calc/impl/TupleValueCalc.java#2 $
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.olap.fun.TupleFunDef;
14 import mondrian.olap.type.TupleType;
15 import mondrian.calc.impl.MemberValueCalc;
16 import mondrian.calc.*;
17
18 /**
19  * Expression which evaluates a tuple expression,
20  * sets the dimensional context to the result of that expression,
21  * then yields the value of the current measure in the current
22  * dimensional context.
23  *
24  * <p>The evaluator's context is preserved.
25  *
26  * @see mondrian.calc.impl.ValueCalc
27  * @see mondrian.calc.impl.MemberValueCalc
28  *
29  * @author jhyde
30  * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/TupleValueCalc.java#2 $
31  * @since Sep 27, 2005
32  */

33 public class TupleValueCalc extends GenericCalc {
34     private final TupleCalc tupleCalc;
35     private final Member[] savedMembers;
36
37     public TupleValueCalc(Exp exp, TupleCalc tupleCalc) {
38         super(exp);
39         this.tupleCalc = tupleCalc;
40         final TupleType tupleType = (TupleType) this.tupleCalc.getType();
41         this.savedMembers = new Member[tupleType.elementTypes.length];
42     }
43
44     public Object JavaDoc evaluate(Evaluator evaluator) {
45         final Member[] members = tupleCalc.evaluateTuple(evaluator);
46         if (members == null) {
47             return null;
48         }
49         for (int i = 0; i < members.length; i++) {
50             savedMembers[i] = evaluator.setContext(members[i]);
51         }
52         final Object JavaDoc o = evaluator.evaluateCurrent();
53         evaluator.setContext(savedMembers);
54         return o;
55     }
56
57     public Calc[] getCalcs() {
58         return new Calc[] {tupleCalc};
59     }
60
61     /**
62      * Optimizes the scalar evaluation of a tuple. It evaluates the members
63      * of the tuple, sets the context to these members, and evaluates the
64      * scalar result in one step, without generating a tuple.<p/>
65      *
66      * This is useful when evaluating calculated members:<blockquote><code>
67      *
68      * <pre>WITH MEMBER [Measures].[Sales last quarter]
69      * AS ' ([Measures].[Unit Sales], [Time].PreviousMember '</pre>
70      *
71      * </code></blockquote>
72      */

73     public Calc optimize() {
74         if (tupleCalc instanceof TupleFunDef.CalcImpl) {
75             TupleFunDef.CalcImpl calc = (TupleFunDef.CalcImpl) tupleCalc;
76             return new MemberValueCalc(
77                     new DummyExp(type), calc.getMemberCalcs());
78         }
79         return this;
80     }
81 }
82
83 // End TupleValueCalc.java
84
Popular Tags