KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > fun > AbstractAggregateFunDef


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

10 package mondrian.olap.fun;
11
12 import mondrian.calc.*;
13 import mondrian.olap.*;
14 import mondrian.resource.MondrianResource;
15 import mondrian.mdx.UnresolvedFunCall;
16
17 import java.util.*;
18
19 /**
20  * Abstract base class for all aggregate functions (<code>Aggregate</code>,
21  * <code>Sum</code>, <code>Avg</code>, et cetera).
22  *
23  * @author jhyde
24  * @since 2005/8/14
25  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/AbstractAggregateFunDef.java#10 $
26  */

27 public class AbstractAggregateFunDef extends FunDefBase {
28     public AbstractAggregateFunDef(FunDef dummyFunDef) {
29         super(dummyFunDef);
30     }
31
32     protected Exp validateArg(
33             Validator validator, Exp[] args, int i, int category) {
34         // If expression cache is enabled, wrap first expression (the set)
35
// in a function which will use the expression cache.
36
if (i == 0) {
37             if (MondrianProperties.instance().EnableExpCache.get()) {
38                 Exp arg = args[0];
39                 final Exp cacheCall = new UnresolvedFunCall(
40                         "$Cache",
41                         Syntax.Internal,
42                         new Exp[] {arg});
43                 return validator.validate(cacheCall, false);
44             }
45         }
46         return super.validateArg(validator, args, i, category);
47     }
48
49     /**
50      * Evaluates the list of members used in computing the aggregate.
51      * Keeps track of the number of iterations that will be required to
52      * iterate over the members needed to compute the aggregate within the
53      * current context. In doing so, also determines if the cross product
54      * of all iterations across all parent evaluation contexts will exceed the
55      * limit set in the properties file.
56      *
57      * @param listCalc calculator used to evaluate the member list
58      * @param evaluator current evalutor
59      *
60      * @return list of evaluated members
61      */

62     protected List evaluateCurrentList(ListCalc listCalc, Evaluator evaluator) {
63         List memberList = listCalc.evaluateList(evaluator);
64
65         int currLen = memberList.size();
66         crossProd(evaluator, currLen);
67
68         return memberList;
69     }
70     protected Iterable JavaDoc evaluateCurrentIterable(IterCalc iterCalc,
71                 Evaluator evaluator) {
72         Iterable JavaDoc iter = iterCalc.evaluateIterable(evaluator);
73
74         int currLen = 0;
75         crossProd(evaluator, currLen);
76
77         return iter;
78     }
79     private void crossProd(Evaluator evaluator, int currLen) {
80         long iterationLimit =
81             MondrianProperties.instance().IterationLimit.get();
82         int productLen = currLen;
83         if (iterationLimit > 0) {
84             Evaluator parent = evaluator.getParent();
85             while (parent != null) {
86                 productLen *= parent.getIterationLength();
87                 parent = parent.getParent();
88             }
89             if (productLen > iterationLimit) {
90                 throw MondrianResource.instance().
91                             IterationLimitExceeded.ex(iterationLimit);
92             }
93         }
94         evaluator.setIterationLength(currLen);
95     }
96
97 }
98
99 // End AbstractAggregateFunDef.java
100
Popular Tags