1 7 package org.ejtools.beans; 8 9 import java.beans.PropertyDescriptor ; 10 import java.lang.reflect.Method ; 11 import java.util.Collections ; 12 import java.util.Comparator ; 13 import java.util.Iterator ; 14 import java.util.Vector ; 15 16 22 public class Sort 23 { 24 25 private Sort() { } 26 27 28 35 public static Iterator getChildrenByClass(Iterator iterator, Class clazz) 36 { 37 Object object; 38 Vector vector = new Vector (); 39 40 while (iterator.hasNext()) 41 { 42 if (clazz.isAssignableFrom((object = iterator.next()).getClass())) 44 { 45 vector.addElement(object); 46 } 47 } 48 49 return vector.iterator(); 51 } 52 53 54 60 public static Iterator sortByClass(Iterator iterator) 61 { 62 Vector vector = new Vector (); 64 while (iterator.hasNext()) 65 { 66 vector.addElement(iterator.next()); 67 } 68 69 Collections.sort(vector, 71 new Comparator () 72 { 73 public int compare(Object o1, Object o2) 75 { 76 return o1.getClass().getName().compareTo(o2.getClass().getName()); 77 } 78 79 80 public boolean equals(Object obj) 81 { 82 return true; 83 } 85 }); 86 87 return vector.iterator(); 89 } 90 91 92 98 public static Iterator sortByClassAndName(Iterator iterator) 99 { 100 Vector vector = new Vector (); 102 while (iterator.hasNext()) 103 { 104 vector.addElement(iterator.next()); 105 } 106 107 Collections.sort(vector, 109 new Comparator () 110 { 111 public int compare(Object o1, Object o2) 112 { 113 int ret = o1.getClass().getName().compareTo(o2.getClass().getName()); 114 if (ret == 0) 115 { 116 ret = o1.toString().compareTo(o2.toString()); 117 } 118 return ret; 119 } 120 121 122 public boolean equals(Object obj) 123 { 124 return true; 125 } 127 }); 128 129 return vector.iterator(); 131 } 132 133 134 140 public static Iterator sortByName(Iterator iterator) 141 { 142 Vector vector = new Vector (); 144 while (iterator.hasNext()) 145 { 146 vector.addElement(iterator.next()); 147 } 148 149 Collections.sort(vector, 151 new Comparator () 152 { 153 public int compare(Object o1, Object o2) 154 { 155 return o1.toString().compareTo(o2.toString()); 156 } 157 158 159 public boolean equals(Object obj) 160 { 161 return true; 162 } 164 }); 165 166 return vector.iterator(); 168 } 169 170 171 179 public static Iterator sortByProperty(Iterator iterator, Class clazz, String propertyName) 180 { 181 Vector vector = new Vector (); 183 while (iterator.hasNext()) 184 { 185 vector.addElement(iterator.next()); 186 } 187 188 try 189 { 190 final PropertyDescriptor pd = new PropertyDescriptor (propertyName, clazz); 192 final Method m = pd.getReadMethod(); 193 194 Collections.sort(vector, 196 new Comparator () 197 { 198 public int compare(Object o1, Object o2) 199 { 200 Comparable val1; 201 Comparable val2; 202 203 try 204 { 205 val1 = (Comparable ) m.invoke(o1, new Object [0]); 206 val2 = (Comparable ) m.invoke(o2, new Object [0]); 207 } 208 catch (Exception e) 209 { 210 return 0; 211 } 212 213 if (val1 == null) 214 { 215 return -1; 216 } 217 if (val2 == null) 218 { 219 return 1; 220 } 221 222 try 223 { 224 return val1.compareTo(val2); 225 } 226 catch (Exception e) 227 { 228 } 230 return 0; 231 } 232 233 234 public boolean equals(Object obj) 235 { 236 return true; 237 } 239 }); 240 } 241 catch (Exception e) 242 { 243 } 245 246 return vector.iterator(); 248 } 249 } 250 251 | Popular Tags |