1 11 package mondrian.olap.fun; 12 13 import mondrian.olap.*; 14 import mondrian.calc.*; 15 import mondrian.calc.impl.AbstractListCalc; 16 import mondrian.mdx.ResolvedFunCall; 17 18 import java.util.*; 19 20 27 class IntersectFunDef extends FunDefBase 28 { 29 private static final String [] ReservedWords = new String [] {"ALL"}; 30 31 static final Resolver resolver = new ReflectiveMultiResolver( 32 "Intersect", 33 "Intersect(<Set1>, <Set2>[, ALL])", 34 "Returns the intersection of two input sets, optionally retaining duplicates.", 35 new String [] {"fxxxy", "fxxx"}, 36 IntersectFunDef.class, 37 ReservedWords); 38 39 public IntersectFunDef(FunDef dummyFunDef) 40 { 41 super(dummyFunDef); 42 } 43 44 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 45 final ListCalc listCalc1 = compiler.compileList(call.getArg(0)); 46 final ListCalc listCalc2 = compiler.compileList(call.getArg(1)); 47 final String literalArg = getLiteralArg(call, 2, "", ReservedWords); 48 final boolean all = literalArg.equalsIgnoreCase("ALL"); 49 50 return new AbstractListCalc(call, new Calc[] {listCalc1, listCalc2}) { 52 public List evaluateList(Evaluator evaluator) { 53 List left = listCalc1.evaluateList(evaluator); 54 if (left == null || left.isEmpty()) { 55 return Collections.EMPTY_LIST; 56 } 57 Collection right = listCalc2.evaluateList(evaluator); 58 if (right == null || right.isEmpty()) { 59 return Collections.EMPTY_LIST; 60 } 61 right = buildSearchableCollection(right); 62 List result = new ArrayList(); 63 64 for (Object leftObject : left) { 65 Object resultObject = leftObject; 66 67 if (leftObject instanceof Object []) { 68 leftObject = new ArrayHolder((Object []) leftObject); 69 } 70 71 if (right.contains(leftObject)) { 72 if (all || !result.contains(leftObject)) { 73 result.add(resultObject); 74 } 75 } 76 } 77 return result; 78 } 79 }; 80 } 81 82 private static Collection buildSearchableCollection(Collection right) { 83 Iterator iter = right.iterator(); 84 Set result = new HashSet(right.size(), 1); 85 while (iter.hasNext()) { 86 Object element = iter.next(); 87 88 if (element instanceof Object []) { 89 element = new ArrayHolder((Object [])element); 90 } 91 92 result.add(element); 93 } 94 95 return result; 96 } 97 } 98 99 | Popular Tags |