KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/fun/ExceptFunDef.java#2 $
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-2006 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.olap.Member;
15 import mondrian.olap.type.Type;
16 import mondrian.olap.type.SetType;
17 import mondrian.olap.type.TupleType;
18 import mondrian.calc.Calc;
19 import mondrian.calc.ExpCompiler;
20 import mondrian.calc.ListCalc;
21 import mondrian.calc.impl.AbstractListCalc;
22 import mondrian.mdx.ResolvedFunCall;
23
24 import java.util.*;
25
26 /**
27  * Definition of the <code>Except</code> MDX function.
28  *
29  * @author jhyde
30  * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/ExceptFunDef.java#2 $
31  * @since Mar 23, 2006
32  */

33 class ExceptFunDef extends FunDefBase {
34     static final ReflectiveMultiResolver Resolver = new ReflectiveMultiResolver(
35             "Except",
36             "Except(<Set1>, <Set2>[, ALL])",
37             "Finds the difference between two sets, optionally retaining duplicates.",
38             new String JavaDoc[]{"fxxx", "fxxxy"},
39             ExceptFunDef.class);
40
41     public ExceptFunDef(FunDef dummyFunDef) {
42         super(dummyFunDef);
43     }
44
45     public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
46         // todo: implement ALL
47
final ListCalc listCalc0 =
48                 compiler.compileList(call.getArg(0));
49         final ListCalc listCalc1 =
50                 compiler.compileList(call.getArg(1));
51         final Type elementType = ((SetType) listCalc0.getType()).getElementType();
52         if (elementType instanceof TupleType) {
53             return new AbstractListCalc(call, new Calc[] {listCalc0, listCalc1}) {
54                 public List evaluateList(Evaluator evaluator) {
55                     List<Member[]> list0 = listCalc0.evaluateList(evaluator);
56                     if (list0.isEmpty()) {
57                         return list0;
58                     }
59                     List<Member[]> list1 = listCalc1.evaluateList(evaluator);
60                     return exceptTuples(list0, list1);
61                 }
62             };
63         } else {
64             return new AbstractListCalc(call, new Calc[] {listCalc0, listCalc1}) {
65                 public List evaluateList(Evaluator evaluator) {
66                     List<Member> list0 = listCalc0.evaluateList(evaluator);
67                     if (list0.isEmpty()) {
68                         return list0;
69                     }
70                     List<Member> list1 = listCalc1.evaluateList(evaluator);
71                     return except(list0, list1);
72                 }
73             };
74         }
75     }
76
77     <T> List<T> except(final List<T> list0, final List<T> list1) {
78         if (list0.size() == 0) {
79             return list0;
80         }
81         Set<T> set = new HashSet<T>(list1);
82         List<T> result = new ArrayList<T>();
83         for (int i = 0, count = list0.size(); i < count; i++) {
84             T o = list0.get(i);
85             if (!set.contains(o)) {
86                 result.add(o);
87             }
88         }
89         return result;
90     }
91
92     List exceptTuples(final List<Member[]> list0, final List list1) {
93         if (list0.size() == 0) {
94             return list0;
95         }
96         // Because the .equals and .hashCode methods of
97
// Member[] use identity, wrap each tuple in a list.
98
Set<List<Member>> set = new HashSet<List<Member>>();
99         for (int i = 0, count1 = list1.size(); i < count1; i++) {
100             Member[] members = (Member[]) list1.get(i);
101             set.add(Arrays.asList(members));
102         }
103         List<Member[]> result = new ArrayList<Member[]>();
104         for (int i = 0, count0 = list0.size(); i < count0; i++) {
105             Member[] members = list0.get(i);
106             if (!set.contains(Arrays.asList(members))) {
107                 result.add(members);
108             }
109         }
110         return result;
111     }
112 }
113
114 // End ExceptFunDef.java
115
Popular Tags