001: /**
002: *
003: */package prefuse.data.util;
004:
005: import java.util.Comparator;
006:
007: import prefuse.data.Tuple;
008: import prefuse.util.collections.DefaultLiteralComparator;
009: import prefuse.util.collections.LiteralComparator;
010:
011: /**
012: * Comparator that compares Tuples based on the value of a single field.
013: *
014: * @author <a href="http://jheer.org">jeffrey heer</a>
015: */
016: public class TupleComparator implements Comparator {
017:
018: private String m_field;
019: private int m_col;
020: private Comparator m_cmp;
021: private Class m_type;
022: private int m_rev;
023:
024: /**
025: * Creates a new TupleComparator.
026: * @param field the data field to compare
027: * @param type the expected type of the data field
028: * @param ascend true to sort in ascending order, false for descending
029: */
030: public TupleComparator(String field, Class type, boolean ascend) {
031: this (field, type, ascend, DefaultLiteralComparator
032: .getInstance());
033: }
034:
035: /**
036: * Creates a new TupleComparator.
037: * @param field the data field to compare
038: * @param type the expected type of the data field
039: * @param ascend true to sort in ascending order, false for descending
040: * @param c the comparator to use. Note that for primitive types,
041: * this should be an instance of LiteralComparator, otherwise
042: * subequent errors will occur.
043: */
044: public TupleComparator(String field, Class type, boolean ascend,
045: Comparator c) {
046: m_field = field;
047: m_col = -1;
048: m_type = type;
049: m_rev = ascend ? 1 : -1;
050: m_cmp = c;
051: }
052:
053: /**
054: * Creates a new TupleComparator.
055: * @param col the column number of the data field to compare
056: * @param type the expected type of the data field
057: * @param ascend true to sort in ascending order, false for descending
058: */
059: public TupleComparator(int col, Class type, boolean ascend) {
060: this (col, type, ascend, DefaultLiteralComparator.getInstance());
061: }
062:
063: /**
064: * Creates a new TupleComparator.
065: * @param col the column number of the data field to compare
066: * @param type the expected type of the data field
067: * @param ascend true to sort in ascending order, false for descending
068: */
069: public TupleComparator(int col, Class type, boolean ascend,
070: Comparator c) {
071: m_field = null;
072: m_col = col;
073: m_type = type;
074: m_rev = ascend ? 1 : -1;
075: m_cmp = c;
076: }
077:
078: /**
079: * Compares two tuples. If either input Object is not a Tuple,
080: * a ClassCastException will be thrown.
081: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
082: */
083: public int compare(Object o1, Object o2) {
084: Tuple t1 = (Tuple) o1, t2 = (Tuple) o2;
085: int c = 0;
086:
087: if (m_col == -1) {
088: if (m_type == int.class || m_type == byte.class) {
089: c = ((LiteralComparator) m_cmp).compare(t1
090: .getInt(m_field), t2.getInt(m_field));
091: } else if (m_type == double.class) {
092: c = ((LiteralComparator) m_cmp).compare(t1
093: .getDouble(m_field), t2.getDouble(m_field));
094: } else if (m_type == long.class) {
095: c = ((LiteralComparator) m_cmp).compare(t1
096: .getLong(m_field), t2.getLong(m_field));
097: } else if (m_type == float.class) {
098: c = ((LiteralComparator) m_cmp).compare(t1
099: .getFloat(m_field), t2.getFloat(m_field));
100: } else if (m_type == boolean.class) {
101: c = ((LiteralComparator) m_cmp).compare(t1
102: .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("Unsupported type: "
107: + m_type.getName());
108: }
109: } else {
110: if (m_type == int.class || m_type == byte.class) {
111: c = ((LiteralComparator) m_cmp).compare(t1
112: .getInt(m_col), t2.getInt(m_col));
113: } else if (m_type == double.class) {
114: c = ((LiteralComparator) m_cmp).compare(t1
115: .getDouble(m_col), t2.getDouble(m_col));
116: } else if (m_type == long.class) {
117: c = ((LiteralComparator) m_cmp).compare(t1
118: .getLong(m_col), t2.getLong(m_col));
119: } else if (m_type == float.class) {
120: c = ((LiteralComparator) m_cmp).compare(t1
121: .getFloat(m_col), t2.getFloat(m_col));
122: } else if (m_type == boolean.class) {
123: c = ((LiteralComparator) m_cmp).compare(t1
124: .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("Unsupported type: "
129: + m_type.getName());
130: }
131: }
132: return m_rev * c;
133: }
134:
135: } // end of class TupleComparator
|