KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > rolap > agg > ListColumnPredicate


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/agg/ListColumnPredicate.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-2007 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.rolap.agg;
11
12 import mondrian.olap.Util;
13 import mondrian.rolap.RolapStar;
14 import mondrian.rolap.StarPredicate;
15 import mondrian.rolap.StarColumnPredicate;
16
17 import java.util.*;
18
19 /**
20  * Predicate which is the union of a list of predicates. It evaluates to
21  * true if any of the predicates evaluates to true.
22  *
23  * @author jhyde
24  * @version $Id: //open/mondrian/src/main/mondrian/rolap/agg/ListColumnPredicate.java#2 $
25  * @since Nov 2, 2006
26  */

27 public class ListColumnPredicate extends AbstractColumnPredicate {
28     /**
29      * List of predicates.
30      */

31     private final List<StarColumnPredicate> children;
32
33     public ListColumnPredicate(
34         RolapStar.Column column, List<StarColumnPredicate> list) {
35         super(column);
36         this.children = list;
37     }
38
39     public List<StarColumnPredicate> getPredicates() {
40         return children;
41     }
42
43
44     public int hashCode() {
45         return children.hashCode();
46     }
47
48     public boolean equals(Object JavaDoc obj) {
49         if (obj instanceof ListColumnPredicate) {
50             ListColumnPredicate that = (ListColumnPredicate) obj;
51             return this.children.equals(that.children);
52         } else {
53             return false;
54         }
55     }
56
57     public void values(Collection collection) {
58         for (StarColumnPredicate child : children) {
59             child.values(collection);
60         }
61     }
62
63     public boolean evaluate(Object JavaDoc value) {
64         // NOTE: If we know that every predicate in the list is a
65
// ValueColumnPredicate, we could optimize the evaluate method by
66
// building a value list at construction time. But it's a tradeoff,
67
// considering the extra time and space required.
68
for (StarColumnPredicate childPredicate : children) {
69             if (childPredicate.evaluate(value)) {
70                 return true;
71             }
72         }
73         return false;
74     }
75
76     public void describe(StringBuilder JavaDoc buf) {
77         buf.append("={");
78         for (int j = 0; j < children.size(); j++) {
79             if (j > 0) {
80                 buf.append(", ");
81             }
82             buf.append(children.get(j));
83         }
84         buf.append('}');
85     }
86
87     public Overlap intersect(StarColumnPredicate predicate) {
88         int matchCount = 0;
89         for (StarColumnPredicate flushPredicate : children) {
90             final Overlap r2 = flushPredicate.intersect(predicate);
91             if (r2.matched) {
92                 // A hit!
93
if (r2.remaining == null) {
94                     // Total match.
95
return r2;
96                 } else {
97                     // Partial match.
98
predicate = r2.remaining;
99                     ++matchCount;
100                 }
101             }
102         }
103         if (matchCount == 0) {
104             return new Overlap(false, null, 0f);
105         } else {
106             float selectivity =
107                 (float) matchCount /
108                     (float) children.size();
109             return new Overlap(true, predicate, selectivity);
110         }
111     }
112
113     public boolean mightIntersect(StarPredicate other) {
114         if (other instanceof LiteralStarPredicate) {
115             return ((LiteralStarPredicate) other).getValue();
116         }
117         if (other instanceof ValueColumnPredicate) {
118             ValueColumnPredicate valueColumnPredicate =
119                 (ValueColumnPredicate) other;
120             return evaluate(valueColumnPredicate.getValue());
121         }
122         if (other instanceof ListColumnPredicate) {
123             final ArrayList thatSet = new ArrayList();
124             ((ListColumnPredicate) other).values(thatSet);
125             for (Object JavaDoc o : thatSet) {
126                 if (evaluate(o)) {
127                     return true;
128                 }
129             }
130             return false;
131         }
132         throw Util.newInternal("unknown constraint type " + other);
133     }
134
135     public StarColumnPredicate minus(StarPredicate predicate) {
136         assert predicate != null;
137         if (predicate instanceof LiteralStarPredicate) {
138             LiteralStarPredicate literalStarPredicate =
139                 (LiteralStarPredicate) predicate;
140             if (literalStarPredicate.getValue()) {
141                 // X minus TRUE --> FALSE
142
return LiteralStarPredicate.FALSE;
143             } else {
144                 // X minus FALSE --> X
145
return this;
146             }
147         }
148         StarColumnPredicate columnPredicate = (StarColumnPredicate) predicate;
149         List<StarColumnPredicate> newChildren =
150             new ArrayList<StarColumnPredicate>(children);
151         int changeCount = 0;
152         final Iterator iterator = newChildren.iterator();
153         while (iterator.hasNext()) {
154             ValueColumnPredicate child =
155                 (ValueColumnPredicate) iterator.next();
156             if (columnPredicate.evaluate(child.getValue())) {
157                 ++changeCount;
158                 iterator.remove();
159             }
160         }
161         if (changeCount > 0) {
162             return new ListColumnPredicate(getConstrainedColumn(), newChildren);
163         } else {
164             return this;
165         }
166     }
167 }
168
169 // End ListColumnPredicate.java
170
Popular Tags