KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 class UnionFunDef extends FunDefBase {
33     static final String JavaDoc[] ReservedWords = new String JavaDoc[] {"ALL", "DISTINCT"};
34
35     static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
36             "Union",
37             "Union(<Set1>, <Set2>[, ALL])",
38             "Returns the union of two sets, optionally retaining duplicates.",
39             new String JavaDoc[] {"fxxx", "fxxxy"},
40             UnionFunDef.class,
41             ReservedWords);
42
43     public UnionFunDef(FunDef dummyFunDef) {
44         super(dummyFunDef);
45     }
46
47     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
48         String JavaDoc allString = getLiteralArg(call, 2, "DISTINCT", ReservedWords);
49         final boolean all = allString.equalsIgnoreCase("ALL");
50         checkCompatible(call.getArg(0), call.getArg(1), null); // todo: do at validate time
51
final ListCalc listCalc0 =
52                 compiler.compileList(call.getArg(0));
53         final ListCalc listCalc1 =
54                 compiler.compileList(call.getArg(1));
55         return new AbstractListCalc(call, new Calc[] {listCalc0, listCalc1}) {
56             public List JavaDoc evaluateList(Evaluator evaluator) {
57                 List JavaDoc list0 = listCalc0.evaluateList(evaluator);
58                 List JavaDoc list1 = listCalc1.evaluateList(evaluator);
59                 return union(list0, list1, all);
60             }
61         };
62     }
63
64     <T> List JavaDoc<T> union(List JavaDoc<T> list0, List JavaDoc<T> list1, final boolean all) {
65         assert list0 != null;
66         assert list1 != null;
67         if (all) {
68             if (list0.isEmpty()) {
69                 return list1;
70             }
71             if (list1.isEmpty()) {
72                 return list0;
73             }
74             List JavaDoc<T> result = new ArrayList JavaDoc<T>();
75             result.addAll(list0);
76             result.addAll(list1);
77             return result;
78         } else {
79             Set JavaDoc added = new HashSet JavaDoc();
80             List JavaDoc<T> result = new ArrayList JavaDoc<T>();
81             FunUtil.addUnique(result, list0, added);
82             FunUtil.addUnique(result, list1, added);
83             return result;
84         }
85     }
86 }
87
88 // End UnionFunDef.java
89
Popular Tags