KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/IntersectFunDef.java#12 $
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) 2004-2002 Kana Software, Inc.
7 // Copyright (C) 2004-2007 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 */

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 /**
21  * Definition of the <code>INTERSECT</code> MDX function.
22  *
23  * @author jhyde
24  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/IntersectFunDef.java#12 $
25  * @since Mar 23, 2006
26  */

27 class IntersectFunDef extends FunDefBase
28 {
29     private static final String JavaDoc[] ReservedWords = new String JavaDoc[] {"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 JavaDoc[] {"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 JavaDoc literalArg = getLiteralArg(call, 2, "", ReservedWords);
48         final boolean all = literalArg.equalsIgnoreCase("ALL");
49
50         // todo: optimize for member lists vs. tuple lists
51
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 JavaDoc leftObject : left) {
65                     Object JavaDoc resultObject = leftObject;
66
67                     if (leftObject instanceof Object JavaDoc[]) {
68                         leftObject = new ArrayHolder((Object JavaDoc[]) 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 JavaDoc element = iter.next();
87
88             if (element instanceof Object JavaDoc[]) {
89                 element = new ArrayHolder((Object JavaDoc[])element);
90             }
91
92             result.add(element);
93         }
94
95         return result;
96     }
97 }
98
99 // End IntersectFunDef.java
100
Popular Tags