KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > fun > extra > NthQuartileFunDef


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/extra/NthQuartileFunDef.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.extra;
11
12 import mondrian.calc.Calc;
13 import mondrian.calc.DoubleCalc;
14 import mondrian.calc.ExpCompiler;
15 import mondrian.calc.ListCalc;
16 import mondrian.calc.impl.AbstractDoubleCalc;
17 import mondrian.calc.impl.ValueCalc;
18 import mondrian.mdx.ResolvedFunCall;
19 import mondrian.olap.Dimension;
20 import mondrian.olap.Evaluator;
21 import mondrian.olap.FunDef;
22 import mondrian.olap.fun.AbstractAggregateFunDef;
23 import mondrian.olap.fun.MultiResolver;
24 import mondrian.olap.fun.ReflectiveMultiResolver;
25
26 import java.util.List JavaDoc;
27
28 /**
29  * Definition of the <code>FirstQ</code> and <code>ThirdQ</code> MDX extension functions.
30  *
31  * <p>These functions are not standard.
32  *
33  * @author jhyde
34  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/extra/NthQuartileFunDef.java#3 $
35  * @since Mar 23, 2006
36  */

37 public class NthQuartileFunDef extends AbstractAggregateFunDef {
38     private final int range;
39
40     public static final MultiResolver ThirdQResolver = new ReflectiveMultiResolver(
41             "ThirdQ",
42             "ThirdQ(<Set>[, <Numeric Expression>])",
43             "Returns the 3rd quartile value of a numeric expression evaluated over a set.",
44             new String JavaDoc[]{"fnx", "fnxn"},
45         NthQuartileFunDef.class);
46
47     public static final MultiResolver FirstQResolver = new ReflectiveMultiResolver(
48             "FirstQ",
49             "FirstQ(<Set>[, <Numeric Expression>])",
50             "Returns the 1st quartile value of a numeric expression evaluated over a set.",
51             new String JavaDoc[]{"fnx", "fnxn"},
52             NthQuartileFunDef.class);
53
54     public NthQuartileFunDef(FunDef dummyFunDef) {
55         super(dummyFunDef);
56         this.range = dummyFunDef.getName().equals("FirstQ") ? 1 : 3;
57     }
58
59     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
60         final ListCalc listCalc =
61                 compiler.compileList(call.getArg(0));
62         final DoubleCalc doubleCalc =
63                 call.getArgCount() > 1 ?
64                 compiler.compileDouble(call.getArg(1)) :
65                 new ValueCalc(call);
66         return new AbstractDoubleCalc(call, new Calc[] {listCalc, doubleCalc}) {
67             public double evaluateDouble(Evaluator evaluator) {
68                 List JavaDoc members = evaluateCurrentList(listCalc, evaluator);
69                 return quartile(evaluator.push(), members, doubleCalc, range);
70             }
71
72             public boolean dependsOn(Dimension dimension) {
73                 return anyDependsButFirst(getCalcs(), dimension);
74             }
75         };
76     }
77 }
78
79 // End NthQuartileFunDef.java
80
Popular Tags