KickJava   Java API By Example, From Geeks To Geeks.

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


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

31 class CovarianceFunDef extends FunDefBase {
32     static final ReflectiveMultiResolver CovarianceResolver = new ReflectiveMultiResolver(
33             "Covariance",
34             "Covariance(<Set>, <Numeric Expression>[, <Numeric Expression>])",
35             "Returns the covariance of two series evaluated over a set (biased).",
36             new String JavaDoc[]{"fnxn","fnxnn"},
37             CovarianceFunDef.class);
38
39     static final MultiResolver CovarianceNResolver = new ReflectiveMultiResolver(
40             "CovarianceN",
41             "CovarianceN(<Set>, <Numeric Expression>[, <Numeric Expression>])",
42             "Returns the covariance of two series evaluated over a set (unbiased).",
43             new String JavaDoc[]{"fnxn","fnxnn"},
44             CovarianceFunDef.class);
45
46     private final boolean biased;
47
48     public CovarianceFunDef(FunDef dummyFunDef) {
49         super(dummyFunDef);
50         this.biased = dummyFunDef.getName().equals("Covariance");
51     }
52
53     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
54         final ListCalc listCalc =
55                 compiler.compileList(call.getArg(0));
56         final Calc calc1 =
57                 compiler.compileScalar(call.getArg(1), true);
58         final Calc calc2 = call.getArgCount() > 2 ?
59                 compiler.compileScalar(call.getArg(2), true) :
60                 new ValueCalc(call);
61         return new AbstractCalc(call) {
62             public Object JavaDoc evaluate(Evaluator evaluator) {
63                 List JavaDoc memberList = listCalc.evaluateList(evaluator);
64                 return covariance(
65                         evaluator.push(), memberList,
66                         calc1, calc2, biased);
67             }
68
69             public Calc[] getCalcs() {
70                 return new Calc[] {listCalc, calc1, calc2};
71             }
72
73             public boolean dependsOn(Dimension dimension) {
74                 return anyDependsButFirst(getCalcs(), dimension);
75             }
76         };
77     }
78 }
79
80 // End CovarianceFunDef.java
81
Popular Tags