| 1 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 ; 25 26 33 class CountFunDef extends AbstractAggregateFunDef { 34 static final String [] ReservedWords = new String [] {"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 []{"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 72 if (calc instanceof IterCalc) { 73 IterCalc iterCalc = (IterCalc) calc; 74 Iterable iterable = 75 evaluateCurrentIterable(iterCalc, evaluator); 76 return count(evaluator, iterable, includeEmpty); 77 } else { 78 ListCalc listCalc = (ListCalc) calc; 80 List memberList = evaluateCurrentList(listCalc, evaluator); 81 return count(evaluator, memberList, includeEmpty); 82 } 83 } 84 85 public boolean dependsOn(Dimension dimension) { 86 if (super.dependsOn(dimension)) { 90 return true; 91 } 92 if (includeEmpty) { 93 return false; 94 } 95 return ! calc.getType().usesDimension(dimension, true); 99 } 100 }; 101 102 138 } 139 } 140 141 | Popular Tags |