Update of /cvsroot/ejtools/libraries/common/src/main/net/sourceforge/ejtools/util
In directory usw-pr-cvs1:/tmp/cvs-serv1672/libraries/common/src/main/net/sourceforge/ejtools/util
Modified Files:
Sort.java
Log Message:
Add sort method based on JavaBean property
Index: Sort.java
===================================================================
RCS file: /cvsroot/ejtools/libraries/common/src/main/net/sourceforge/ejtools/util/Sort.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Sort.java 19 Apr 2002 07:05:02 -0000 1.1
--- Sort.java 30 Apr 2002 21:02:50 -0000 1.2
***************
*** 7,10 ****
--- 7,12 ----
package net.sourceforge.ejtools.util;
+ import java.beans.PropertyDescriptor;
+ import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
***************
*** 17,21 ****
* @author laurent
* @created 24 décembre 2001
- * @todo Javadoc to complete
*/
public abstract class Sort
--- 19,22 ----
***************
*** 38,41 ****
--- 39,118 ----
v.addElement(obj);
}
+ }
+
+ return v.iterator();
+ }
+
+
+ /**
+ * Description of the Method
+ *
+ * @param enum Description of Parameter
+ * @param bean Description of Parameter
+ * @param propertyName Description of Parameter
+ * @return Description of the Returned Value
+ */
+ public static Iterator sortByProperty(Iterator enum, Class bean, String propertyName)
+ {
+ Vector v = new Vector();
+ while (enum.hasNext())
+ {
+ v.addElement(enum.next());
+ }
+
+ try
+ {
+ final PropertyDescriptor pd = new PropertyDescriptor(propertyName, bean);
+ final Method m = pd.getReadMethod();
+
+ Collections.sort(v,
+ new Comparator()
+ {
+ public int compare(Object o1, Object o2)
+ {
+ Comparable val1;
+ Comparable val2;
+
+ try
+ {
+ val1 = (Comparable) m.invoke(o1, new Object[0]);
+ val2 = (Comparable) m.invoke(o2, new Object[0]);
+ }
+ catch (Exception e)
+ {
+ return 0;
+ }
+
+ if (val1 == null)
+ {
+ return -1;
+ }
+ if (val2 == null)
+ {
+ return 1;
+ }
+
+ try
+ {
+ return val1.compareTo(val2);
+ }
+ catch (Exception e)
+ {
+ // Ignore it
+ }
+ return 0;
+ }
+
+
+ public boolean equals(Object obj)
+ {
+ return true;
+ // Ever called?
+ }
+ });
+ }
+ catch (Exception e)
+ {
+ // Ignore it
}
|