KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/MedianFunDef.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>Median</code> MDX functions.
26  *
27  * @author jhyde
28  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/MedianFunDef.java#3 $
29  * @since Mar 23, 2006
30  */

31 class MedianFunDef extends AbstractAggregateFunDef {
32     static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
33             "Median",
34             "Median(<Set>[, <Numeric Expression>])",
35             "Returns the median value of a numeric expression evaluated over a set.",
36             new String JavaDoc[]{"fnx", "fnxn"},
37             MedianFunDef.class);
38
39     public MedianFunDef(FunDef dummyFunDef) {
40         super(dummyFunDef);
41     }
42
43     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
44         final ListCalc listCalc =
45                 compiler.compileList(call.getArg(0));
46         final Calc calc = call.getArgCount() > 1 ?
47                 compiler.compileScalar(call.getArg(1), true) :
48                 new ValueCalc(call);
49         return new AbstractCalc(call) {
50             public Object JavaDoc evaluate(Evaluator evaluator) {
51                 List JavaDoc memberList = evaluateCurrentList(listCalc, evaluator);
52                 return median(evaluator.push(), memberList, calc);
53             }
54
55             public Calc[] getCalcs() {
56                 return new Calc[] {listCalc, calc};
57             }
58
59             public boolean dependsOn(Dimension dimension) {
60                 return anyDependsButFirst(getCalcs(), dimension);
61             }
62         };
63     }
64 }
65
66 // End MedianFunDef.java
67
Popular Tags