KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > communication > sms > util > SortComparator


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.communication.sms.util;
6
7 import java.lang.reflect.InvocationTargetException JavaDoc;
8 import java.util.Comparator JavaDoc;
9
10 import org.apache.commons.beanutils.PropertyUtils;
11
12 /**
13  *
14  * @author: Ove Ranheim
15  * @email: oranheim@users.sourceforge.net
16  */

17 public class SortComparator implements Comparator JavaDoc {
18
19     public String JavaDoc p_fieldName;
20
21     public SortComparator(String JavaDoc fieldName) {
22         p_fieldName = fieldName;
23     }
24
25     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
26         try {
27
28             Object JavaDoc val1 = PropertyUtils.getProperty(o1, p_fieldName);
29             Object JavaDoc val2 = PropertyUtils.getProperty(o2, p_fieldName);
30
31             if (val1.equals(val2)) { return 0; }
32
33             if (val1 instanceof String JavaDoc) { return ((String JavaDoc) val1).compareTo((String JavaDoc) val2); }
34             if (val1 instanceof Integer JavaDoc) {
35                 Integer JavaDoc int1 = new Integer JavaDoc((String JavaDoc) val1);
36                 Integer JavaDoc int2 = new Integer JavaDoc((String JavaDoc) val2);
37
38                 return int1.compareTo(int2);
39             }
40             if (val1 instanceof java.util.Date JavaDoc) {
41                 java.util.Date JavaDoc date1 = (java.util.Date JavaDoc) val1;
42                 java.util.Date JavaDoc date2 = (java.util.Date JavaDoc) val2;
43                 return date1.compareTo(date2);
44             }
45
46         } catch (IllegalAccessException JavaDoc e) {
47             throw new SortException(e);
48         } catch (InvocationTargetException JavaDoc e) {
49             throw new SortException(e);
50         } catch (NoSuchMethodException JavaDoc e) {
51             throw new SortException(e);
52         }
53
54         return 0;
55     }
56 }
57
58
Popular Tags