KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 class CountFunDef extends AbstractAggregateFunDef {
34     static final String JavaDoc[] ReservedWords = new String JavaDoc[] {"INCLUDEEMPTY", "EXCLUDEEMPTY"};
35
36     static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
37             "Count",
38             "Count(<Set>[, EXCLUDEEMPTY | INCLUDEEMPTY])",
39             "Returns the number of tuples in a set, empty cells included unless the optional EXCLUDEEMPTY flag is used.",
40             new String JavaDoc[]{"fnx", "fnxy"},
41             CountFunDef.class,
42             ReservedWords);
43
44     public CountFunDef(FunDef dummyFunDef) {
45         super(dummyFunDef);
46     }
47
48     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
49         final Calc calc = compiler.compile(call.getArg(0),
50                                 ExpCompiler.ITERABLE_ANY_RESULT_STYLE_ARRAY
51                                 );
52         final boolean includeEmpty =
53                 call.getArgCount() < 2 ||
54                 ((Literal) call.getArg(1)).getValue().equals(
55                         "INCLUDEEMPTY");
56         return new AbstractIntegerCalc(
57                 call, new Calc[] {calc}) {
58             public int evaluateInteger(Evaluator evaluator) {
59 /*
60                 if (calc instanceof ListCalc) {
61                     ListCalc listCalc = (ListCalc) calc;
62                     List memberList = evaluateCurrentList(listCalc, evaluator);
63                     return count(evaluator, memberList, includeEmpty);
64                 } else {
65                     // must be IterCalc
66                     IterCalc iterCalc = (IterCalc) calc;
67                     Iterable iterable =
68                         evaluateCurrentIterable(iterCalc, evaluator);
69                     return count(evaluator, iterable, includeEmpty);
70                 }
71 */

72                 if (calc instanceof IterCalc) {
73                     IterCalc iterCalc = (IterCalc) calc;
74                     Iterable JavaDoc iterable =
75                         evaluateCurrentIterable(iterCalc, evaluator);
76                     return count(evaluator, iterable, includeEmpty);
77                 } else {
78                     // must be ListCalc
79
ListCalc listCalc = (ListCalc) calc;
80                     List JavaDoc memberList = evaluateCurrentList(listCalc, evaluator);
81                     return count(evaluator, memberList, includeEmpty);
82                 }
83             }
84
85             public boolean dependsOn(Dimension dimension) {
86                 // COUNT(<set>, INCLUDEEMPTY) is straightforward -- it
87
// depends only on the dimensions that <Set> depends
88
// on.
89
if (super.dependsOn(dimension)) {
90                     return true;
91                 }
92                 if (includeEmpty) {
93                     return false;
94                 }
95                 // COUNT(<set>, EXCLUDEEMPTY) depends only on the
96
// dimensions that <Set> depends on, plus all
97
// dimensions not masked by the set.
98
return ! calc.getType().usesDimension(dimension, true);
99             }
100         };
101
102 /*
103  RME OLD STUFF
104         final ListCalc memberListCalc =
105                 compiler.compileList(call.getArg(0));
106         final boolean includeEmpty =
107                 call.getArgCount() < 2 ||
108                 ((Literal) call.getArg(1)).getValue().equals(
109                         "INCLUDEEMPTY");
110         return new AbstractIntegerCalc(
111                 call, new Calc[] {memberListCalc}) {
112             public int evaluateInteger(Evaluator evaluator) {
113                 List memberList =
114                     evaluateCurrentList(memberListCalc, evaluator);
115                 return count(evaluator, memberList, includeEmpty);
116             }
117
118             public boolean dependsOn(Dimension dimension) {
119                 // COUNT(<set>, INCLUDEEMPTY) is straightforward -- it
120                 // depends only on the dimensions that <Set> depends
121                 // on.
122                 if (super.dependsOn(dimension)) {
123                     return true;
124                 }
125                 if (includeEmpty) {
126                     return false;
127                 }
128                 // COUNT(<set>, EXCLUDEEMPTY) depends only on the
129                 // dimensions that <Set> depends on, plus all
130                 // dimensions not masked by the set.
131                 if (memberListCalc.getType().usesDimension(dimension, true) ) {
132                     return false;
133                 }
134                 return true;
135             }
136         };
137 */

138     }
139 }
140
141 // End CountFunDef.java
142
Popular Tags