KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/SumFunDef.java#8 $
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-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.Calc;
14 import mondrian.calc.ExpCompiler;
15 import mondrian.calc.ExpCompiler.ResultStyle;
16 import mondrian.calc.ListCalc;
17 import mondrian.calc.IterCalc;
18 import mondrian.calc.impl.ValueCalc;
19 import mondrian.calc.impl.AbstractDoubleCalc;
20 import mondrian.mdx.ResolvedFunCall;
21
22 import java.util.List JavaDoc;
23
24 /**
25  * Definition of the <code>Sum</code> MDX function.
26  *
27  * @author jhyde
28  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/SumFunDef.java#8 $
29  * @since Mar 23, 2006
30  */

31 class SumFunDef extends AbstractAggregateFunDef {
32     static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
33             "Sum",
34             "Sum(<Set>[, <Numeric Expression>])",
35             "Returns the sum of a numeric expression evaluated over a set.",
36             new String JavaDoc[]{"fnx", "fnxn"},
37             SumFunDef.class);
38
39     public SumFunDef(FunDef dummyFunDef) {
40         super(dummyFunDef);
41     }
42
43     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
44         ResultStyle[] rs = compiler.getAcceptableResultStyles();
45         // What is the desired type to use to get the underlying values
46
for (int i = 0; i < rs.length; i++) {
47             switch (rs[i]) {
48             case ITERABLE :
49             case ANY :
50                 // Consumer wants ITERABLE or ANY to be used
51
//return compileCallIterable(call, compiler);
52
return compileCall(call, compiler, ResultStyle.ITERABLE);
53             case MUTABLE_LIST:
54                 // Consumer wants MUTABLE_LIST
55
return compileCall(call, compiler, ResultStyle.MUTABLE_LIST);
56             case LIST :
57                 // Consumer wants LIST to be used
58
//return compileCallList(call, compiler);
59
return compileCall(call, compiler, ResultStyle.LIST);
60             }
61         }
62         throw ResultStyleException.generate(
63             new ResultStyle[] {
64                 ResultStyle.ITERABLE,
65                 ResultStyle.LIST,
66                 ResultStyle.MUTABLE_LIST,
67                 ResultStyle.ANY
68             },
69             rs
70         );
71     }
72     protected Calc compileCall(final ResolvedFunCall call,
73             ExpCompiler compiler, ResultStyle resultStyle) {
74         final Calc ncalc = compiler.compile(call.getArg(0),
75                     new ResultStyle[] { resultStyle});
76         final Calc calc = call.getArgCount() > 1 ?
77                 compiler.compileScalar(call.getArg(1), true) :
78                 new ValueCalc(call);
79         // we may have asked for one sort of Calc, but here's what we got.
80
if (ncalc instanceof ListCalc) {
81             return genListCalc(call, ncalc, calc);
82         } else {
83             return genIterCalc(call, ncalc, calc);
84         }
85     }
86     protected Calc genIterCalc(final ResolvedFunCall call,
87             final Calc ncalc, final Calc calc) {
88         return new AbstractDoubleCalc(call, new Calc[] {ncalc, calc}) {
89             public double evaluateDouble(Evaluator evaluator) {
90                 IterCalc iterCalc = (IterCalc) ncalc;
91                 Iterable JavaDoc iterable =
92                     evaluateCurrentIterable(iterCalc, evaluator);
93                 return sumDouble(evaluator.push(), iterable, calc);
94             }
95
96             public Calc[] getCalcs() {
97                 return new Calc[] {ncalc, calc};
98             }
99
100             public boolean dependsOn(Dimension dimension) {
101                 return anyDependsButFirst(getCalcs(), dimension);
102             }
103         };
104     }
105
106     protected Calc genListCalc(final ResolvedFunCall call,
107             final Calc ncalc, final Calc calc) {
108         return new AbstractDoubleCalc(call, new Calc[] {ncalc, calc}) {
109             public double evaluateDouble(Evaluator evaluator) {
110                 ListCalc listCalc = (ListCalc) ncalc;
111                 List JavaDoc memberList = evaluateCurrentList(listCalc, evaluator);
112                 return sumDouble(evaluator.push(), memberList, calc);
113             }
114
115             public Calc[] getCalcs() {
116                 return new Calc[] {ncalc, calc};
117             }
118
119             public boolean dependsOn(Dimension dimension) {
120                 return anyDependsButFirst(getCalcs(), dimension);
121             }
122         };
123     }
124 }
125
126 // End SumFunDef.java
127
Popular Tags