KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > expression > RangePredicate


1 package prefuse.data.expression;
2
3 import java.util.Comparator JavaDoc;
4
5 import prefuse.data.Schema;
6 import prefuse.data.Tuple;
7 import prefuse.util.TypeLib;
8 import prefuse.util.collections.DefaultLiteralComparator;
9 import prefuse.util.collections.LiteralComparator;
10
11 /**
12  * Predicate instance that evaluates if a value is contained within
13  * a bounded range.
14  *
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public class RangePredicate extends BinaryExpression implements Predicate {
18
19     /** Indicates the both the left and right bounds are inclusive */
20     public static final int IN_IN = 0;
21     /** Indicates an inclusive left bound and exclusive right bound */
22     public static final int IN_EX = 1;
23     /** Indicates an exclusive left bound and inclusive right bound */
24     public static final int EX_IN = 2;
25     /** Indicates the both the left and right bounds are exclusive */
26     public static final int EX_EX = 3;
27     
28     private Expression m_middle;
29     private Comparator JavaDoc m_cmp;
30     
31     // ------------------------------------------------------------------------
32
// Constructors
33

34     /**
35      * Create a new RangePredicate. Both bounds are assumed to be inclusive.
36      * @param middle the value to test for membership in the range
37      * @param left the lower range bound
38      * @param right the upper range bound
39      */

40     public RangePredicate(Expression middle,
41             Expression left, Expression right)
42     {
43         this(IN_IN, middle, left, right,
44              DefaultLiteralComparator.getInstance());
45     }
46     
47     /**
48      * Create a new RangePredicate. Both bounds are assumed to be inclusive.
49      * @param middle the value to test for membership in the range
50      * @param left the lower range bound
51      * @param right the upper range bound
52      * @param cmp the comparator to use for comparing data values
53      */

54     public RangePredicate(Expression middle,
55             Expression left, Expression right, Comparator JavaDoc cmp)
56     {
57         this(IN_IN, middle, left, right, cmp);
58     }
59     
60     /**
61      * Create a new RangePredicate.
62      * @param operation operation code indicating the inclusiveness /
63      * exclusiveness of the bounds
64      * @param middle the value to test for membership in the range
65      * @param left the lower range bound
66      * @param right the upper range bound
67      */

68     public RangePredicate(int operation, Expression middle,
69             Expression left, Expression right)
70     {
71         this(operation, middle, left, right,
72              DefaultLiteralComparator.getInstance());
73     }
74     
75     /**
76      * Create a new RangePredicate.
77      * @param operation operation code indicating the inclusiveness /
78      * exclusiveness of the bounds
79      * @param middle the value to test for membership in the range
80      * @param left the lower range bound
81      * @param right the upper range bound
82      * @param cmp the comparator to use for comparing data values
83      */

84     public RangePredicate(int operation, Expression middle,
85             Expression left, Expression right, Comparator JavaDoc cmp)
86     {
87         super(operation, IN_IN, EX_EX, left, right);
88         
89         this.m_middle = middle;
90         this.m_cmp = cmp;
91     }
92     
93     // ------------------------------------------------------------------------
94
// Accessors
95

96     /**
97      * Get the middle expression being tested for inclusion in the range
98      * @return the middle expression
99      */

100     public Expression getMiddleExpression() {
101         return m_middle;
102     }
103     
104     /**
105      * Get the comparator used to compare data values.
106      * @return the comparator used to compare data values
107      */

108     public Comparator JavaDoc getComparator() {
109         return m_cmp;
110     }
111     
112     // ------------------------------------------------------------------------
113
// Expression Interface
114

115     /**
116      * @see prefuse.data.expression.Expression#getBoolean(prefuse.data.Tuple)
117      */

118     public boolean getBoolean(Tuple t) {
119         Class JavaDoc lType = m_left.getType(t.getSchema());
120         Class JavaDoc rType = m_right.getType(t.getSchema());
121         Class JavaDoc mType = m_middle.getType(t.getSchema());
122         Class JavaDoc sType = null;
123         
124         // see if we can match the end-points' type
125
if ( lType.isAssignableFrom(rType) ) {
126             sType = lType;
127         } else if ( rType.isAssignableFrom(lType) ) {
128             sType = rType;
129         }
130         
131         int c1, c2 = 0;
132         if ( sType != null && TypeLib.isNumericType(sType) &&
133                 TypeLib.isNumericType(mType) )
134         {
135             // the range is of numeric types
136
Class JavaDoc type = TypeLib.getNumericType(sType, mType);
137             if ( type == int.class ) {
138                 int lo = m_left.getInt(t);
139                 int hi = m_right.getInt(t);
140                 int x = m_middle.getInt(t);
141                 c1 = ((LiteralComparator)m_cmp).compare(x,lo);
142                 c2 = ((LiteralComparator)m_cmp).compare(x,hi);
143             } else if ( type == long.class ) {
144                 long lo = m_left.getLong(t);
145                 long hi = m_right.getLong(t);
146                 long x = m_middle.getLong(t);
147                 c1 = ((LiteralComparator)m_cmp).compare(x,lo);
148                 c2 = ((LiteralComparator)m_cmp).compare(x,hi);
149             } else if ( type == float.class ) {
150                 float lo = m_left.getFloat(t);
151                 float hi = m_right.getFloat(t);
152                 float x = m_middle.getFloat(t);
153                 c1 = ((LiteralComparator)m_cmp).compare(x,lo);
154                 c2 = ((LiteralComparator)m_cmp).compare(x,hi);
155             } else if ( type == double.class ) {
156                 double lo = m_left.getDouble(t);
157                 double hi = m_right.getDouble(t);
158                 double x = m_middle.getDouble(t);
159                 c1 = ((LiteralComparator)m_cmp).compare(x,lo);
160                 c2 = ((LiteralComparator)m_cmp).compare(x,hi);
161             } else {
162                 throw new IllegalStateException JavaDoc();
163             }
164         } else {
165             Object JavaDoc lo = m_left.get(t);
166             Object JavaDoc hi = m_right.get(t);
167             Object JavaDoc x = m_middle.get(t);
168             c1 = m_cmp.compare(x, lo);
169             c2 = m_cmp.compare(x, hi);
170         }
171        
172         // check the comparison values to see if it is in-range
173
switch ( m_op ) {
174         case IN_IN:
175             return ( c1 >= 0 && c2 <= 0 );
176         case IN_EX:
177             return ( c1 >= 0 && c2 < 0 );
178         case EX_IN:
179             return ( c1 > 0 && c2 <= 0 );
180         case EX_EX:
181             return ( c1 > 0 && c2 < 0 );
182         default:
183             throw new IllegalStateException JavaDoc("Unknown operation.");
184         }
185     }
186     
187     /**
188      * @see prefuse.data.expression.Expression#getType(prefuse.data.Schema)
189      */

190     public Class JavaDoc getType(Schema s) {
191         return boolean.class;
192     }
193
194     /**
195      * @see prefuse.data.expression.Expression#get(prefuse.data.Tuple)
196      */

197     public Object JavaDoc get(Tuple t) {
198         return ( getBoolean(t) ? Boolean.TRUE : Boolean.FALSE );
199     }
200     
201     /**
202      * @see prefuse.data.expression.Expression#visit(prefuse.data.expression.ExpressionVisitor)
203      */

204     public void visit(ExpressionVisitor v) {
205         v.visitExpression(this);
206         v.down(); m_left.visit(v); v.up();
207         v.down(); m_middle.visit(v); v.up();
208         v.down(); m_right.visit(v); v.up();
209     }
210     
211     /**
212      * @see prefuse.data.expression.AbstractExpression#addChildListeners()
213      */

214     protected void addChildListeners() {
215         super.addChildListeners();
216         m_middle.addExpressionListener(this);
217     }
218     
219     /**
220      * @see prefuse.data.expression.AbstractExpression#removeChildListeners()
221      */

222     protected void removeChildListeners() {
223         super.removeChildListeners();
224         m_middle.removeExpressionListener(this);
225     }
226     
227     /**
228      * @see java.lang.Object#toString()
229      */

230     public String JavaDoc toString() {
231         String JavaDoc lop = "?", rop = "?";
232         switch ( m_op ) {
233         case IN_IN:
234             lop = rop = "<=";
235             break;
236         case IN_EX:
237             lop = "<="; rop = "<";
238             break;
239         case EX_IN:
240             lop = "<"; rop = "<=";
241             break;
242         case EX_EX:
243             lop = rop = "<";
244             break;
245         }
246         return '('+m_left.toString()+' '+lop+' '+m_middle.toString()+" AND "+
247                    m_middle.toString()+' '+rop+' '+m_right.toString()+')';
248     }
249     
250 } // end of class RangePredicate
251
Popular Tags