1 25 26 package org.objectweb.jonas.webapp.jonasadmin.common; 27 28 import java.util.Comparator ; 29 30 import org.apache.commons.beanutils.PropertyUtils; 31 32 39 40 public class BeanComparator implements Comparator { 41 42 44 private String [] m_Orders = null; 45 46 48 51 public BeanComparator() { 52 m_Orders = null; 53 } 54 55 60 public BeanComparator(String [] p_Orders) { 61 m_Orders = p_Orders; 62 } 63 64 66 73 public int compare(Object p_O1, Object p_O2) { 74 if (m_Orders != null) { 75 return compareOrder(p_O1, p_O2); 76 } 77 return compareDefault(p_O1, p_O2); 78 } 79 80 86 public boolean equals(Object p_Obj) { 87 if (this.getClass().getName().equals(p_Obj.getClass().getName())) { 88 return (compare(this, p_Obj) == 0); 89 } 90 return false; 91 } 92 93 100 public static int compareNull(Object p_O1, Object p_O2) { 101 int iRet = 0; 102 if (p_O1 != p_O2) { 103 if (p_O1 == null) { 104 iRet = 1; 105 } 106 else if (p_O2 == null) { 107 iRet = -1; 108 } 109 } 110 return iRet; 111 } 112 113 121 public static int compareString(String p_S1, String p_S2) { 122 int iRet = compareNull(p_S1, p_S2); 123 if (iRet == 0) { 124 try { 125 iRet = p_S1.compareToIgnoreCase(p_S2); 126 if (iRet == 0) { 127 iRet = p_S1.compareTo(p_S2); 128 } 129 } 130 catch (NullPointerException e) { 131 iRet = 0; 132 } 133 } 134 return iRet; 135 } 136 137 139 147 protected int compareDefault(Object p_O1, Object p_O2) { 148 int iRet = compareNull(p_O1, p_O2); 149 if (iRet == 0) { 150 iRet = compareString(p_O1.toString(), p_O2.toString()); 151 } 152 return iRet; 153 } 154 155 163 protected int compareOrder(Object p_O1, Object p_O2) { 164 int iRet = 0; 165 Object oValue1, oValue2; 166 try { 167 for (int i = 0; i < m_Orders.length; i++) { 168 oValue1 = PropertyUtils.getProperty(p_O1, m_Orders[i]); 170 oValue2 = PropertyUtils.getProperty(p_O2, m_Orders[i]); 171 iRet = compareNull(oValue1, oValue2); 173 if (iRet == 0) { 174 try { 175 iRet = compareString(oValue1.toString(), oValue2.toString()); 176 } 177 catch (NullPointerException e) { 178 iRet = 0; 179 } 180 } 181 if (iRet != 0) { 183 break; 184 } 185 } 186 } 187 catch (Exception e) { 188 System.err.println("[" + e.getClass().getName() + "] " + e.getMessage()); 189 iRet = 0; 190 } 191 return iRet; 192 } 193 } | Popular Tags |