1 4 package prefuse.data.util; 5 6 import java.util.Arrays ; 7 import java.util.Comparator ; 8 9 import prefuse.data.Schema; 10 import prefuse.data.Table; 11 import prefuse.data.Tuple; 12 import prefuse.data.expression.Predicate; 13 import prefuse.data.tuple.TupleSet; 14 import prefuse.util.collections.CompositeComparator; 15 import prefuse.util.collections.NullComparator; 16 17 49 public class Sort { 50 51 private static final String ASC = " ASC"; 52 private static final String DESC = " DESC"; 53 private static final String asc = ASC.toLowerCase(); 54 private static final String desc = DESC.toLowerCase(); 55 56 private String [] m_fields; 57 private boolean[] m_ascend; 58 59 62 public Sort() { 63 this(new String [0], new boolean[0]); 64 } 65 66 71 public Sort(String [] fields) { 72 this(fields, new boolean[fields.length]); 73 Arrays.fill(m_ascend, true); 74 } 75 76 83 public Sort(String [] fields, boolean[] ascend) { 84 m_fields = fields; 85 m_ascend = ascend; 86 } 87 88 94 public void add(String field, boolean ascend) { 95 String [] f = new String [m_fields.length+1]; 96 System.arraycopy(m_fields, 0, f, 0, m_fields.length); 97 f[m_fields.length] = field; 98 m_fields = f; 99 100 boolean[] b = new boolean[m_fields.length+1]; 101 System.arraycopy(m_ascend, 0, b, 0, m_ascend.length); 102 b[m_ascend.length] = ascend; 103 m_ascend = b; 104 } 105 106 110 public int size() { 111 return m_fields.length; 112 } 113 114 119 public String getField(int i) { 120 return m_fields[i]; 121 } 122 123 129 public boolean isAscending(int i) { 130 return m_ascend[i]; 131 } 132 133 140 public Comparator getComparator(TupleSet ts) { 141 Schema s = null; 143 if ( ts instanceof Table ) { 144 s = ((Table)ts).getSchema(); 146 } else { 147 if ( ts.getTupleCount() == 0 ) 149 return new NullComparator(); 150 s = ((Tuple)ts.tuples().next()).getSchema(); 152 } 153 CompositeComparator cc = new CompositeComparator(m_fields.length); 155 for ( int i=0; i<m_fields.length; ++i ) { 156 cc.add(new TupleComparator(m_fields[i], 157 s.getColumnType(m_fields[i]), m_ascend[i])); 158 } 159 return cc; 160 } 161 162 164 private static void subparse(String s, Object [] res) { 165 s = s.trim(); 166 167 res[1] = Boolean.TRUE; 169 if ( s.endsWith(DESC) || s.endsWith(desc) ) { 170 res[1] = Boolean.FALSE; 171 s = s.substring(0, s.length()-DESC.length()).trim(); 172 } else if ( s.endsWith(ASC) || s.endsWith(asc) ) { 173 s = s.substring(0, s.length()-ASC.length()).trim(); 174 } 175 176 if ( s.startsWith("[") ) { 177 if ( s.lastIndexOf("[") == 0 && 178 s.endsWith("]") && s.indexOf("]") == s.length() ) { 179 res[0] = s.substring(1, s.length()-1); 180 } else { 181 throw new RuntimeException (); 182 } 183 } else { 184 if ( s.indexOf(" ") < 0 && s.indexOf("\t") < 0 ) { 185 res[0] = s; 186 } else { 187 throw new RuntimeException (); 188 } 189 } 190 } 191 192 214 public static Sort parse(String s) { 215 Sort sort = new Sort(); 216 Object [] res = new Object [2]; 217 int idx = 0, len = s.length(); 218 int comma = s.indexOf(','); 219 int quote = s.indexOf('['); 220 while ( idx < len ) { 221 if ( comma < 0 ) { 222 subparse(s.substring(idx), res); 223 sort.add((String )res[0], ((Boolean )res[1]).booleanValue()); 224 break; 225 } else if ( quote < 0 || comma < quote ) { 226 subparse(s.substring(idx, comma), res); 227 sort.add((String )res[0], ((Boolean )res[1]).booleanValue()); 228 idx = comma + 1; 229 comma = s.indexOf(idx, ','); 230 } else { 231 int q2 = s.indexOf(quote, ']'); 232 if ( q2 < 0 ) { 233 throw new RuntimeException (); 234 } else { 235 comma = s.indexOf(q2, ','); 236 subparse(s.substring(idx, comma), res); 237 sort.add((String )res[0], ((Boolean )res[1]).booleanValue()); 238 idx = comma + 1; 239 comma = s.indexOf(idx, ','); 240 } 241 } 242 } 243 return sort; 244 } 245 246 249 public String toString() { 250 StringBuffer sbuf = new StringBuffer (); 251 for ( int i=0; i<m_fields.length; ++i ) { 252 if ( i > 0 ) sbuf.append(", "); 253 sbuf.append('[').append(m_fields[i]).append(']'); 254 sbuf.append((m_ascend[i]) ? ASC : DESC); 255 } 256 return sbuf.toString(); 257 } 258 259 } | Popular Tags |