KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > util > TupleComparator


1 /**
2  *
3  */

4 package prefuse.data.util;
5
6 import java.util.Comparator JavaDoc;
7
8 import prefuse.data.Tuple;
9 import prefuse.util.collections.DefaultLiteralComparator;
10 import prefuse.util.collections.LiteralComparator;
11
12 /**
13  * Comparator that compares Tuples based on the value of a single field.
14  *
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public class TupleComparator implements Comparator JavaDoc {
18
19     private String JavaDoc m_field;
20     private int m_col;
21     private Comparator JavaDoc m_cmp;
22     private Class JavaDoc m_type;
23     private int m_rev;
24     
25     /**
26      * Creates a new TupleComparator.
27      * @param field the data field to compare
28      * @param type the expected type of the data field
29      * @param ascend true to sort in ascending order, false for descending
30      */

31     public TupleComparator(String JavaDoc field, Class JavaDoc type, boolean ascend) {
32         this(field, type, ascend, DefaultLiteralComparator.getInstance());
33     }
34     
35     /**
36      * Creates a new TupleComparator.
37      * @param field the data field to compare
38      * @param type the expected type of the data field
39      * @param ascend true to sort in ascending order, false for descending
40      * @param c the comparator to use. Note that for primitive types,
41      * this should be an instance of LiteralComparator, otherwise
42      * subequent errors will occur.
43      */

44     public TupleComparator(String JavaDoc field, Class JavaDoc type,
45                            boolean ascend, Comparator JavaDoc c)
46     {
47         m_field = field;
48         m_col = -1;
49         m_type = type;
50         m_rev = ascend ? 1 : -1;
51         m_cmp = c;
52     }
53     
54     /**
55      * Creates a new TupleComparator.
56      * @param col the column number of the data field to compare
57      * @param type the expected type of the data field
58      * @param ascend true to sort in ascending order, false for descending
59      */

60     public TupleComparator(int col, Class JavaDoc type, boolean ascend) {
61         this(col, type, ascend, DefaultLiteralComparator.getInstance());
62     }
63     
64     /**
65      * Creates a new TupleComparator.
66      * @param col the column number of the data field to compare
67      * @param type the expected type of the data field
68      * @param ascend true to sort in ascending order, false for descending
69      */

70     public TupleComparator(int col, Class JavaDoc type, boolean ascend, Comparator JavaDoc c) {
71         m_field = null;
72         m_col = col;
73         m_type = type;
74         m_rev = ascend ? 1 : -1;
75         m_cmp = c;
76     }
77     
78     /**
79      * Compares two tuples. If either input Object is not a Tuple,
80      * a ClassCastException will be thrown.
81      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
82      */

83     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
84         Tuple t1 = (Tuple)o1, t2 = (Tuple)o2;
85         int c = 0;
86         
87         if ( m_col == -1 ) {
88             if ( m_type == int.class || m_type == byte.class ) {
89                 c = ((LiteralComparator)m_cmp).compare(
90                         t1.getInt(m_field), t2.getInt(m_field));
91             } else if ( m_type == double.class ) {
92                 c = ((LiteralComparator)m_cmp).compare(
93                         t1.getDouble(m_field), t2.getDouble(m_field));
94             } else if ( m_type == long.class ) {
95                 c = ((LiteralComparator)m_cmp).compare(
96                         t1.getLong(m_field), t2.getLong(m_field));
97             } else if ( m_type == float.class ) {
98                 c = ((LiteralComparator)m_cmp).compare(
99                         t1.getFloat(m_field), t2.getFloat(m_field));
100             } else if ( m_type == boolean.class ) {
101                 c = ((LiteralComparator)m_cmp).compare(
102                         t1.getBoolean(m_field), t2.getBoolean(m_field));
103             } else if ( !m_type.isPrimitive() ) {
104                 c = m_cmp.compare(t1.get(m_field), t2.get(m_field));
105             } else {
106                 throw new IllegalStateException JavaDoc(
107                         "Unsupported type: " + m_type.getName());
108             }
109         } else {
110             if ( m_type == int.class || m_type == byte.class ) {
111                 c = ((LiteralComparator)m_cmp).compare(
112                         t1.getInt(m_col), t2.getInt(m_col));
113             } else if ( m_type == double.class ) {
114                 c = ((LiteralComparator)m_cmp).compare(
115                         t1.getDouble(m_col), t2.getDouble(m_col));
116             } else if ( m_type == long.class ) {
117                 c = ((LiteralComparator)m_cmp).compare(
118                         t1.getLong(m_col), t2.getLong(m_col));
119             } else if ( m_type == float.class ) {
120                 c = ((LiteralComparator)m_cmp).compare(
121                         t1.getFloat(m_col), t2.getFloat(m_col));
122             } else if ( m_type == boolean.class ) {
123                 c = ((LiteralComparator)m_cmp).compare(
124                         t1.getBoolean(m_col), t2.getBoolean(m_col));
125             } else if ( !m_type.isPrimitive() ) {
126                 c = m_cmp.compare(t1.get(m_col), t2.get(m_col));
127             } else {
128                 throw new IllegalStateException JavaDoc(
129                         "Unsupported type: " + m_type.getName());
130             }
131         }
132         return m_rev * c;
133     }
134
135 } // end of class TupleComparator
136
Popular Tags