KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > ExpCacheDescriptor


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/ExpCacheDescriptor.java#5 $
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) 2005-2006 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.olap;
11
12 import mondrian.calc.*;
13 import mondrian.calc.impl.BetterExpCompiler;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 /**
19  * Holds information necessary to add an expression to the expression result
20  * cache (see {@link Evaluator#getCachedResult(ExpCacheDescriptor)}).
21  *
22  * @author jhyde
23  * @since Aug 16, 2005
24  * @version $Id: //open/mondrian/src/main/mondrian/olap/ExpCacheDescriptor.java#5 $
25  */

26 public class ExpCacheDescriptor {
27     private final Exp exp;
28     private int[] dependentDimensionOrdinals;
29     private final Calc calc;
30
31     public ExpCacheDescriptor(Exp exp, Calc calc, Evaluator evaluator) {
32         this.calc = calc;
33         this.exp = exp;
34         computeDepends(calc, evaluator);
35     }
36
37     /**
38      * Creates a descriptor.
39      */

40     public ExpCacheDescriptor(Exp exp, Evaluator evaluator) {
41         this(exp, new BetterExpCompiler(evaluator, null));
42     }
43
44     /**
45      * Creates a descriptor.
46      */

47     public ExpCacheDescriptor(Exp exp, ExpCompiler compiler) {
48         this.exp = exp;
49
50         // Compile expression.
51
this.calc = compiler.compile(exp);
52
53         // Compute list of dependent dimensions.
54
computeDepends(calc, compiler.getEvaluator());
55     }
56
57     private void computeDepends(Calc calc, Evaluator evaluator) {
58         final List JavaDoc<Integer JavaDoc> ordinalList = new ArrayList JavaDoc<Integer JavaDoc>();
59         final Member[] members = evaluator.getMembers();
60         for (int i = 0; i < members.length; i++) {
61             Dimension dimension = members[i].getDimension();
62             if (calc.dependsOn(dimension)) {
63                 ordinalList.add(i);
64             }
65         }
66         dependentDimensionOrdinals = new int[ordinalList.size()];
67         for (int i = 0; i < dependentDimensionOrdinals.length; i++) {
68             dependentDimensionOrdinals[i] = ordinalList.get(i);
69         }
70     }
71
72     public Exp getExp() {
73         return exp;
74     }
75
76     public Calc getCalc() {
77         return calc;
78     }
79
80     public Object JavaDoc evaluate(Evaluator evaluator) {
81         return calc.evaluate(evaluator);
82     }
83
84     /**
85      * Returns the ordinals of the dimensions which this expression is
86      * dependent upon. When the cache descriptor is used to generate a cache
87      * key, the key will consist of a member from each of these dimensions.
88      */

89     public int[] getDependentDimensionOrdinals() {
90         return dependentDimensionOrdinals;
91     }
92
93 }
94
95 // End ExpCacheDescriptor.java
96
Popular Tags