KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/agg/MinusStarPredicate.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.rolap.StarPredicate;
13 import mondrian.rolap.StarColumnPredicate;
14 import mondrian.rolap.RolapStar;
15
16 import java.util.*;
17
18 /**
19  * A <code>StarPredicate</code> which evaluates to true if its
20  * first child evaluates to true and its second child evaluates to false.
21  *
22  * @author jhyde
23  * @version $Id: //open/mondrian/src/main/mondrian/rolap/agg/MinusStarPredicate.java#2 $
24  * @since Nov 6, 2006
25  */

26 public class MinusStarPredicate extends AbstractColumnPredicate {
27     private final StarColumnPredicate plus;
28     private final StarColumnPredicate minus;
29
30     /**
31      * Creates a MinusStarPredicate.
32      *
33      * @param plus Positive predicate
34      * @param minus Negative predicate
35      * @pre plus != null
36      * @pre minus != null
37      */

38     public MinusStarPredicate(
39         StarColumnPredicate plus,
40         StarColumnPredicate minus)
41     {
42         super(plus.getConstrainedColumn());
43         assert minus != null;
44         this.plus = plus;
45         this.minus = minus;
46     }
47
48
49     public boolean equals(Object JavaDoc obj) {
50         if (obj instanceof MinusStarPredicate) {
51             MinusStarPredicate that = (MinusStarPredicate) obj;
52             return this.plus.equals(that.plus) &&
53                 this.minus.equals(that.minus);
54         } else {
55             return false;
56         }
57     }
58
59     public int hashCode() {
60         return plus.hashCode() * 31 +
61             minus.hashCode();
62     }
63
64     public RolapStar.Column getConstrainedColumn() {
65         return plus.getConstrainedColumn();
66     }
67
68     public void values(Collection collection) {
69         Set plusValues = new HashSet();
70         plus.values(plusValues);
71         List minusValues = new ArrayList();
72         minus.values(minusValues);
73         plusValues.removeAll(minusValues);
74         collection.addAll(plusValues);
75     }
76
77     public boolean evaluate(Object JavaDoc value) {
78         return plus.evaluate(value) &&
79             !minus.evaluate(value);
80     }
81
82     public void describe(StringBuilder JavaDoc buf) {
83         buf.append("(").append(plus).append(" - ").append(minus).append(")");
84     }
85
86     public Overlap intersect(StarColumnPredicate predicate) {
87         throw new UnsupportedOperationException JavaDoc();
88     }
89
90     public boolean mightIntersect(StarPredicate other) {
91         // Approximately, this constraint might intersect if it intersects
92
// with the 'plus' side. It's possible that the 'minus' side might
93
// wipe out all of those intersections, but we don't consider that.
94
return plus.mightIntersect(other);
95     }
96
97     public StarColumnPredicate minus(StarPredicate predicate) {
98         assert predicate != null;
99         if (predicate instanceof ValueColumnPredicate) {
100             ValueColumnPredicate valuePredicate =
101                 (ValueColumnPredicate) predicate;
102             if (!evaluate(valuePredicate.getValue())) {
103                 // Case 3: 'minus' is a list, 'constraint' is a value
104
// which is not matched by this
105
return this;
106             }
107         }
108         if (minus instanceof ListColumnPredicate) {
109             ListColumnPredicate minusList = (ListColumnPredicate) minus;
110             RolapStar.Column column = plus.getConstrainedColumn();
111             if (predicate instanceof ListColumnPredicate) {
112                 // Case 1: 'minus' and 'constraint' are both lists.
113
ListColumnPredicate list =
114                     (ListColumnPredicate) predicate;
115                 List<StarColumnPredicate> unionList =
116                     new ArrayList<StarColumnPredicate>();
117                 unionList.addAll(minusList.getPredicates());
118                 unionList.addAll(list.getPredicates());
119                 return new MinusStarPredicate(
120                     plus,
121                     new ListColumnPredicate(
122                         column,
123                         unionList));
124             }
125             if (predicate instanceof ValueColumnPredicate) {
126                 ValueColumnPredicate valuePredicate =
127                     (ValueColumnPredicate) predicate;
128                 if (!evaluate(valuePredicate.getValue())) {
129                     // Case 3: 'minus' is a list, 'constraint' is a value
130
// which is not matched by this
131
return this;
132                 }
133                 // Case 2: 'minus' is a list, 'constraint' is a value.
134
List<StarColumnPredicate> unionList =
135                     new ArrayList<StarColumnPredicate>();
136                 unionList.addAll(minusList.getPredicates());
137                 unionList.add(
138                     new ValueColumnPredicate(column, valuePredicate.getValue()));
139                 return new MinusStarPredicate(
140                     plus,
141                     new ListColumnPredicate(column, unionList));
142             }
143         }
144         return new MinusStarPredicate(
145             this,
146             (StarColumnPredicate) predicate);
147     }
148 }
149
150 // End MinusStarPredicate.java
151
Popular Tags