BeanComparator.java

00001 
00026 package org.objectweb.jonas.webapp.jonasadmin.common;
00027 
00028 import java.util.Comparator;
00029 
00030 import org.apache.commons.beanutils.PropertyUtils;
00031 
00040 public class BeanComparator implements Comparator {
00041 
00042 // --------------------------------------------------------- Instance Variables
00043 
00044     private String[] m_Orders = null;
00045 
00046 // --------------------------------------------------------- Constructors
00047 
00051     public BeanComparator() {
00052         m_Orders = null;
00053     }
00054 
00060     public BeanComparator(String[] p_Orders) {
00061         m_Orders = p_Orders;
00062     }
00063 
00064 // --------------------------------------------------------- Public Methods
00065 
00073     public int compare(Object p_O1, Object p_O2) {
00074         if (m_Orders != null) {
00075             return compareOrder(p_O1, p_O2);
00076         }
00077         return compareDefault(p_O1, p_O2);
00078     }
00079 
00086     public boolean equals(Object p_Obj) {
00087         if (this.getClass().getName().equals(p_Obj.getClass().getName())) {
00088             return (compare(this, p_Obj) == 0);
00089         }
00090         return false;
00091     }
00092 
00100     public static int compareNull(Object p_O1, Object p_O2) {
00101         int iRet = 0;
00102         if (p_O1 != p_O2) {
00103             if (p_O1 == null) {
00104                 iRet = 1;
00105             }
00106             else if (p_O2 == null) {
00107                 iRet = -1;
00108             }
00109         }
00110         return iRet;
00111     }
00112 
00121     public static int compareString(String p_S1, String p_S2) {
00122         int iRet = compareNull(p_S1, p_S2);
00123         if (iRet == 0) {
00124             try {
00125                 iRet = p_S1.compareToIgnoreCase(p_S2);
00126                 if (iRet == 0) {
00127                     iRet = p_S1.compareTo(p_S2);
00128                 }
00129             }
00130             catch (NullPointerException e) {
00131                 iRet = 0;
00132             }
00133         }
00134         return iRet;
00135     }
00136 
00137 // --------------------------------------------------------- Protected Methods
00138 
00147     protected int compareDefault(Object p_O1, Object p_O2) {
00148         int iRet = compareNull(p_O1, p_O2);
00149         if (iRet == 0) {
00150             iRet = compareString(p_O1.toString(), p_O2.toString());
00151         }
00152         return iRet;
00153     }
00154 
00163     protected int compareOrder(Object p_O1, Object p_O2) {
00164         int iRet = 0;
00165         Object oValue1, oValue2;
00166         try {
00167             for (int i = 0; i < m_Orders.length; i++) {
00168                 // Get values
00169                 oValue1 = PropertyUtils.getProperty(p_O1, m_Orders[i]);
00170                 oValue2 = PropertyUtils.getProperty(p_O2, m_Orders[i]);
00171                 // Compare
00172                 iRet = compareNull(oValue1, oValue2);
00173                 if (iRet == 0) {
00174                     try {
00175                         iRet = compareString(oValue1.toString(), oValue2.toString());
00176                     }
00177                     catch (NullPointerException e) {
00178                         iRet = 0;
00179                     }
00180                 }
00181                 // Continue in order if always equals
00182                 if (iRet != 0) {
00183                     break;
00184                 }
00185             }
00186         }
00187         catch (Exception e) {
00188             System.err.println("[" + e.getClass().getName() + "] " + e.getMessage());
00189             iRet = 0;
00190         }
00191         return iRet;
00192     }
00193 }

Generated on Tue Feb 15 15:05:26 2005 for JOnAS by  doxygen 1.3.9.1