KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/rolap/agg/RangeColumnPredicate.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.Collection JavaDoc;
17
18 /**
19  * Predicate constraining a column to be greater than or less than a given
20  * bound, or between a pair of bounds.
21  *
22  * @author jhyde
23  * @version $Id: //open/mondrian/src/main/mondrian/rolap/agg/RangeColumnPredicate.java#2 $
24  * @since Nov 26, 2006
25  */

26 public class RangeColumnPredicate extends AbstractColumnPredicate {
27     private final boolean lowerInclusive;
28     private final ValueColumnPredicate lowerBound;
29     private final boolean upperInclusive;
30     private final ValueColumnPredicate upperBound;
31
32     /**
33      * Creates a RangeColumnPredicate.
34      *
35      * @param column Constrained column
36      * @param lowerInclusive Whether range includes the lower bound;
37      * must be false if not bounded below
38      * @param lowerBound Lower bound, or null if not bounded below
39      * @param upperInclusive Whether range includes the upper bound;
40      * must be false if not bounded above
41      * @param upperBound Upper bound, or null if not bounded above
42      */

43     public RangeColumnPredicate(
44         RolapStar.Column column,
45         boolean lowerInclusive,
46         ValueColumnPredicate lowerBound,
47         boolean upperInclusive,
48         ValueColumnPredicate upperBound)
49     {
50         super(column);
51         assert lowerBound == null ||
52             lowerBound.getConstrainedColumn() == column;
53         assert !(lowerBound == null && lowerInclusive);
54         assert upperBound == null ||
55             upperBound.getConstrainedColumn() == column;
56         assert !(upperBound == null && upperInclusive);
57         this.lowerInclusive = lowerInclusive;
58         this.lowerBound = lowerBound;
59         this.upperInclusive = upperInclusive;
60         this.upperBound = upperBound;
61     }
62
63     public String JavaDoc toString() {
64         final StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
65         describe(buf);
66         return buf.toString();
67     }
68
69
70     public int hashCode() {
71         int h = lowerInclusive ? 2 : 1;
72         h = 31 * h + lowerBound.hashCode();
73         h = 31 * h + (upperInclusive ? 2 : 1);
74         h = 31 * h + upperBound.hashCode();
75         return h;
76     }
77
78     public boolean equals(Object JavaDoc obj) {
79         if (obj instanceof RangeColumnPredicate) {
80             RangeColumnPredicate that =
81                 (RangeColumnPredicate) obj;
82             return this.lowerInclusive == that.lowerInclusive &&
83                 this.lowerBound.equals(that.lowerBound) &&
84                 this.upperInclusive == that.upperInclusive &&
85                 this.upperBound.equals(that.upperBound);
86         } else {
87             return false;
88         }
89     }
90
91     public void values(Collection JavaDoc collection) {
92         // Besides the end points, don't know what values may be in the range.
93
// FIXME: values() is only a half-useful method. Replace it?
94
throw new UnsupportedOperationException JavaDoc();
95     }
96
97     public boolean evaluate(Object JavaDoc value) {
98         if (lowerBound != null) {
99             int c = ((Comparable JavaDoc<Object JavaDoc>) lowerBound.getValue()).compareTo(value);
100             if (lowerInclusive ? c > 0 : c >= 0) {
101                 return false;
102             }
103         }
104         if (upperBound != null) {
105             int c = ((Comparable JavaDoc<Object JavaDoc>) upperBound.getValue()).compareTo(value);
106             if (upperInclusive ? c < 0 : c <= 0) {
107                 return false;
108             }
109         }
110         return true;
111     }
112
113     public void describe(StringBuilder JavaDoc buf) {
114         buf.append("Range(");
115         if (lowerBound == null) {
116             buf.append("unbounded");
117         } else {
118             lowerBound.describe(buf);
119             if (lowerInclusive) {
120                 buf.append(" inclusive");
121             }
122         }
123         buf.append(" to ");
124         if (upperBound == null) {
125             buf.append("unbounded");
126         } else {
127             upperBound.describe(buf);
128             if (upperInclusive) {
129                 buf.append(" inclusive");
130             }
131         }
132         buf.append(")");
133     }
134
135     public Overlap intersect(StarColumnPredicate predicate) {
136         throw new UnsupportedOperationException JavaDoc();
137     }
138
139     public boolean mightIntersect(StarPredicate other) {
140         if (other instanceof ValueColumnPredicate) {
141             return evaluate(((ValueColumnPredicate) other).getValue());
142         } else {
143             // It MIGHT intersect. (Might not.)
144
// todo: Handle case 'other instanceof RangeColumnPredicate'
145
return true;
146         }
147     }
148
149     public StarColumnPredicate minus(StarPredicate predicate) {
150         assert predicate != null;
151         // todo: Implement some common cases, such as Range minus Range, and
152
// Range minus true/false
153
return new MinusStarPredicate(
154             this, (StarColumnPredicate) predicate);
155     }
156
157     public ValueColumnPredicate getLowerBound() {
158         return lowerBound;
159     }
160
161     public boolean getLowerInclusive() {
162         return lowerInclusive;
163     }
164
165     public ValueColumnPredicate getUpperBound() {
166         return upperBound;
167     }
168
169     public boolean getUpperInclusive() {
170         return upperInclusive;
171     }
172 }
173
174 // End RangeColumnPredicate.java
175
Popular Tags