KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/calc/impl/MemberValueCalc.java#4 $
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.type.Type;
14 import mondrian.olap.type.ScalarType;
15 import mondrian.calc.*;
16
17 /**
18  * Expression which evaluates a few member expressions,
19  * sets the dimensional context to the result of those expressions,
20  * then yields the value of the current measure in the current
21  * dimensional context.
22  *
23  * <p>The evaluator's context is preserved.
24  *
25  * <p>Note that a MemberValueCalc with 0 member expressions is equivalent to a
26  * {@link mondrian.calc.impl.ValueCalc}; see also {@link mondrian.calc.impl.TupleValueCalc}.
27  *
28  * @author jhyde
29  * @version $Id: //open/mondrian/src/main/mondrian/calc/impl/MemberValueCalc.java#4 $
30  * @since Sep 27, 2005
31  */

32 public class MemberValueCalc extends GenericCalc {
33     private final MemberCalc[] memberCalcs;
34     private final Member[] savedMembers;
35
36     public MemberValueCalc(Exp exp, MemberCalc[] memberCalcs) {
37         super(exp);
38         final Type type = exp.getType();
39         assert type instanceof ScalarType : exp;
40         this.memberCalcs = memberCalcs;
41         this.savedMembers = new Member[memberCalcs.length];
42     }
43
44     public Object JavaDoc evaluate(Evaluator evaluator) {
45         for (int i = 0; i < memberCalcs.length; i++) {
46             MemberCalc memberCalc = memberCalcs[i];
47             final Member member = memberCalc.evaluateMember(evaluator);
48             if (member == null ||
49                     member.isNull()) {
50                 // This method needs to leave the evaluator in the same state
51
// it found it.
52
for (int j = 0; j < i; j++) {
53                     evaluator.setContext(savedMembers[j]);
54                 }
55                 return null;
56             }
57             savedMembers[i] = evaluator.setContext(member);
58         }
59         final Object JavaDoc o = evaluator.evaluateCurrent();
60         evaluator.setContext(savedMembers);
61         return o;
62     }
63
64     public Calc[] getCalcs() {
65         return memberCalcs;
66     }
67
68     public boolean dependsOn(Dimension dimension) {
69         if (super.dependsOn(dimension)) {
70             return true;
71         }
72         for (MemberCalc memberCalc : memberCalcs) {
73             // If the expression
74
if (memberCalc.getType().usesDimension(dimension, true)) {
75                 return false;
76             }
77         }
78         return true;
79     }
80 }
81
82 // End MemberValueCalc.java
83
Popular Tags