KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/MinMaxFunDef.java#3 $
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.FunDef;
13 import mondrian.olap.Evaluator;
14 import mondrian.olap.Dimension;
15 import mondrian.calc.Calc;
16 import mondrian.calc.ExpCompiler;
17 import mondrian.calc.ListCalc;
18 import mondrian.calc.impl.ValueCalc;
19 import mondrian.calc.impl.AbstractCalc;
20 import mondrian.mdx.ResolvedFunCall;
21
22 import java.util.List JavaDoc;
23
24 /**
25  * Definition of the <code>Min</code> and <code>Max</code> MDX functions.
26  *
27  * @author jhyde
28  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/MinMaxFunDef.java#3 $
29  * @since Mar 23, 2006
30  */

31 class MinMaxFunDef extends AbstractAggregateFunDef {
32     static final ReflectiveMultiResolver MinResolver = new ReflectiveMultiResolver(
33             "Min",
34             "Min(<Set>[, <Numeric Expression>])",
35             "Returns the minimum value of a numeric expression evaluated over a set.",
36             new String JavaDoc[]{"fnx", "fnxn"},
37             MinMaxFunDef.class);
38
39     static final MultiResolver MaxResolver = new ReflectiveMultiResolver(
40             "Max",
41             "Max(<Set>[, <Numeric Expression>])",
42             "Returns the maximum value of a numeric expression evaluated over a set.",
43             new String JavaDoc[]{"fnx", "fnxn"},
44             MinMaxFunDef.class);
45
46     private final boolean max;
47
48     public MinMaxFunDef(FunDef dummyFunDef) {
49         super(dummyFunDef);
50         this.max = dummyFunDef.getName().equals("Max");
51     }
52
53     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
54         final ListCalc listCalc =
55                 compiler.compileList(call.getArg(0));
56         final Calc calc = call.getArgCount() > 1 ?
57                 compiler.compileScalar(call.getArg(1), true) :
58                 new ValueCalc(call);
59         return new AbstractCalc(call) {
60             public Object JavaDoc evaluate(Evaluator evaluator) {
61                 List JavaDoc memberList = evaluateCurrentList(listCalc, evaluator);
62                 return max ?
63                         max(evaluator.push(), memberList, calc) :
64                         min(evaluator.push(), memberList, calc);
65             }
66
67             public Calc[] getCalcs() {
68                 return new Calc[] {listCalc, calc};
69             }
70
71             public boolean dependsOn(Dimension dimension) {
72                 return anyDependsButFirst(getCalcs(), dimension);
73             }
74         };
75     }
76 }
77
78 // End MinMaxFunDef.java
79
Popular Tags