1 4 package prefuse.data.util; 5 6 import java.util.Comparator ; 7 8 import prefuse.data.Tuple; 9 import prefuse.util.collections.DefaultLiteralComparator; 10 import prefuse.util.collections.LiteralComparator; 11 12 17 public class TupleComparator implements Comparator { 18 19 private String m_field; 20 private int m_col; 21 private Comparator m_cmp; 22 private Class m_type; 23 private int m_rev; 24 25 31 public TupleComparator(String field, Class type, boolean ascend) { 32 this(field, type, ascend, DefaultLiteralComparator.getInstance()); 33 } 34 35 44 public TupleComparator(String field, Class type, 45 boolean ascend, Comparator 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 60 public TupleComparator(int col, Class type, boolean ascend) { 61 this(col, type, ascend, DefaultLiteralComparator.getInstance()); 62 } 63 64 70 public TupleComparator(int col, Class type, boolean ascend, Comparator 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 83 public int compare(Object o1, Object 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 ( 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 ( 129 "Unsupported type: " + m_type.getName()); 130 } 131 } 132 return m_rev * c; 133 } 134 135 } | Popular Tags |