KickJava   Java API By Example, From Geeks To Geeks.

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


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 implementation that computes a comparison operation. Supported
13  * operations are equals, not equals, less than, greater than, less than or
14  * equal to, and greater than or equal to.
15  *
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class ComparisonPredicate extends BinaryExpression implements Predicate {
19
20     /** Indicates a less-than comparison. */
21     public static final int LT = 0;
22     /** Indicates a greater-than comparison. */
23     public static final int GT = 1;
24     /** Indicates a equals comparison. */
25     public static final int EQ = 2;
26     /** Indicates a not-equals comparison. */
27     public static final int NEQ = 3;
28     /** Indicates a less-than-or-equals comparison. */
29     public static final int LTEQ = 4;
30     /** Indicates a greater-than-or-equals comparison. */
31     public static final int GTEQ = 5;
32     
33     private Comparator JavaDoc m_cmp;
34     
35     /**
36      * Create a new ComparisonPredicate. Uses a default comparator instance.
37      * @param operation the comparison operation to compute
38      * @param left the left sub-expression
39      * @param right the right sub-expression
40      */

41     public ComparisonPredicate(int operation,
42             Expression left, Expression right)
43     {
44         this(operation, left, right, DefaultLiteralComparator.getInstance());
45     }
46
47     /**
48      * Create a new ComparisonPredicate.
49      * @param operation the comparison operation to compute
50      * @param left the left sub-expression
51      * @param right the right sub-expression
52      * @param cmp the comparator to use to compare values
53      */

54     public ComparisonPredicate(int operation,
55             Expression left, Expression right, Comparator JavaDoc cmp)
56     {
57         super(operation, LT, GTEQ, left, right);
58         this.m_cmp = cmp;
59     }
60     
61     
62     /**
63      * Get the comparator used to compare instances.
64      * @return the comparator instance
65      */

66     public Comparator JavaDoc getComparator() {
67         return m_cmp;
68     }
69     
70     // ------------------------------------------------------------------------
71

72     /**
73      * @see prefuse.data.expression.Expression#getType(prefuse.data.Schema)
74      */

75     public Class JavaDoc getType(Schema s) {
76         return boolean.class;
77     }
78     
79     /**
80      * @see prefuse.data.expression.Expression#getBoolean(prefuse.data.Tuple)
81      */

82     public boolean getBoolean(Tuple t) {
83         Class JavaDoc lType = m_left.getType(t.getSchema());
84         Class JavaDoc rType = m_right.getType(t.getSchema());
85         
86         int c = 0;
87         if ( TypeLib.isNumericType(lType) && TypeLib.isNumericType(rType) ) {
88             Class JavaDoc type = TypeLib.getNumericType(lType, rType);
89             if ( type == int.class || type == byte.class ) {
90                 int x = m_left.getInt(t);
91                 int y = m_right.getInt(t);
92                 c = ((LiteralComparator)m_cmp).compare(x,y);
93             } else if ( type == long.class ) {
94                 long x = m_left.getLong(t);
95                 long y = m_right.getLong(t);
96                 c = ((LiteralComparator)m_cmp).compare(x,y);
97             } else if ( type == float.class ) {
98                 float x = m_left.getFloat(t);
99                 float y = m_right.getFloat(t);
100                 c = ((LiteralComparator)m_cmp).compare(x,y);
101             } else if ( type == double.class ) {
102                 double x = m_left.getDouble(t);
103                 double y = m_right.getDouble(t);
104                 c = ((LiteralComparator)m_cmp).compare(x,y);
105             } else {
106                 throw new IllegalStateException JavaDoc();
107             }
108         } else {
109             c = m_cmp.compare(m_left.get(t), m_right.get(t));
110         }
111        
112         switch ( m_op ) {
113         case LT:
114             return ( c == -1 );
115         case GT:
116             return ( c == 1 );
117         case EQ:
118             return ( c == 0 );
119         case NEQ:
120             return ( c != 0 );
121         case LTEQ:
122             return ( c <= 0 );
123         case GTEQ:
124             return ( c >= 0 );
125         default:
126             throw new IllegalStateException JavaDoc("Unknown operation.");
127         }
128     }
129
130     /**
131      * @see prefuse.data.expression.Expression#get(prefuse.data.Tuple)
132      */

133     public Object JavaDoc get(Tuple t) {
134         return ( getBoolean(t) ? Boolean.TRUE : Boolean.FALSE );
135     }
136     
137     /**
138      * @see java.lang.Object#toString()
139      */

140     public String JavaDoc toString() {
141         String JavaDoc op = "?";
142         switch ( m_op ) {
143         case LT:
144             op = "<";
145             break;
146         case GT:
147             op = ">";
148             break;
149         case EQ:
150             op = "=";
151             break;
152         case NEQ:
153             op = "!=";
154             break;
155         case LTEQ:
156             op = "<=";
157             break;
158         case GTEQ:
159             op = ">=";
160             break;
161         }
162         return m_left.toString()+' '+op+' '+m_right.toString();
163     }
164     
165 } // end of class BinaryPredicate
166
Popular Tags