KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/AggregateFunDef.java#7 $
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.olap.*;
13 import mondrian.calc.*;
14 import mondrian.calc.impl.AbstractCalc;
15 import mondrian.calc.impl.ValueCalc;
16 import mondrian.mdx.ResolvedFunCall;
17
18 import java.util.List JavaDoc;
19
20 /**
21  * Definition of the <code>AGGREGATE</code> MDX function.
22  *
23  * @author jhyde
24  * @since 2005/8/14
25  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/AggregateFunDef.java#7 $
26  */

27 class AggregateFunDef extends AbstractAggregateFunDef {
28     static final ReflectiveMultiResolver resolver =
29             new ReflectiveMultiResolver(
30                     "Aggregate", "Aggregate(<Set>[, <Numeric Expression>])",
31                     "Returns a calculated value using the appropriate aggregate function, based on the context of the query.",
32                     new String JavaDoc[]{"fnx", "fnxn"},
33                     AggregateFunDef.class);
34
35     public AggregateFunDef(FunDef dummyFunDef) {
36         super(dummyFunDef);
37     }
38
39     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
40         final ListCalc listCalc = compiler.compileList(call.getArg(0));
41         final Calc calc = call.getArgCount() > 1 ?
42                 compiler.compileScalar(call.getArg(1), true) :
43                 new ValueCalc(call);
44         return new AbstractCalc(call) {
45             public Object JavaDoc evaluate(Evaluator evaluator) {
46                 Aggregator aggregator =
47                         (Aggregator) evaluator.getProperty(
48                                 Property.AGGREGATION_TYPE.name, null);
49                 if (aggregator == null) {
50                     throw newEvalException(null, "Could not find an aggregator in the current evaluation context");
51                 }
52                 Aggregator rollup = aggregator.getRollup();
53                 if (rollup == null) {
54                     throw newEvalException(null, "Don't know how to rollup aggregator '" + aggregator + "'");
55                 }
56                 final List JavaDoc list = evaluateCurrentList(listCalc, evaluator);
57                 return rollup.aggregate(evaluator.push(), list, calc);
58             }
59
60             public Calc[] getCalcs() {
61                 return new Calc[] {listCalc, calc};
62             }
63
64             public boolean dependsOn(Dimension dimension) {
65                 return anyDependsButFirst(getCalcs(), dimension);
66             }
67         };
68     }
69 }
70
71 // End AggregateFunDef.java
72
Popular Tags