1 16 package org.apache.commons.collections.comparators; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.BitSet ; 21 import java.util.Comparator ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 56 public class ComparatorChain implements Comparator , Serializable { 57 58 59 private static final long serialVersionUID = -721644942746081630L; 60 61 62 protected List comparatorChain = null; 63 64 protected BitSet orderingBits = null; 65 66 protected boolean isLocked = false; 67 68 75 public ComparatorChain() { 76 this(new ArrayList (),new BitSet ()); 77 } 78 79 85 public ComparatorChain(Comparator comparator) { 86 this(comparator,false); 87 } 88 89 96 public ComparatorChain(Comparator comparator, boolean reverse) { 97 comparatorChain = new ArrayList (); 98 comparatorChain.add(comparator); 99 orderingBits = new BitSet (1); 100 if (reverse == true) { 101 orderingBits.set(0); 102 } 103 } 104 105 113 public ComparatorChain(List list) { 114 this(list,new BitSet (list.size())); 115 } 116 117 132 public ComparatorChain(List list, BitSet bits) { 133 comparatorChain = list; 134 orderingBits = bits; 135 } 136 137 144 public void addComparator(Comparator comparator) { 145 addComparator(comparator,false); 146 } 147 148 155 public void addComparator(Comparator comparator, boolean reverse) { 156 checkLocked(); 157 158 comparatorChain.add(comparator); 159 if (reverse == true) { 160 orderingBits.set(comparatorChain.size() - 1); 161 } 162 } 163 164 173 public void setComparator(int index, Comparator comparator) 174 throws IndexOutOfBoundsException { 175 setComparator(index,comparator,false); 176 } 177 178 186 public void setComparator(int index, Comparator comparator, boolean reverse) { 187 checkLocked(); 188 189 comparatorChain.set(index,comparator); 190 if (reverse == true) { 191 orderingBits.set(index); 192 } else { 193 orderingBits.clear(index); 194 } 195 } 196 197 198 204 public void setForwardSort(int index) { 205 checkLocked(); 206 orderingBits.clear(index); 207 } 208 209 215 public void setReverseSort(int index) { 216 checkLocked(); 217 orderingBits.set(index); 218 } 219 220 225 public int size() { 226 return comparatorChain.size(); 227 } 228 229 237 public boolean isLocked() { 238 return isLocked; 239 } 240 241 private void checkLocked() { 243 if (isLocked == true) { 244 throw new UnsupportedOperationException ("Comparator ordering cannot be changed after the first comparison is performed"); 245 } 246 } 247 248 private void checkChainIntegrity() { 249 if (comparatorChain.size() == 0) { 250 throw new UnsupportedOperationException ("ComparatorChains must contain at least one Comparator"); 251 } 252 } 253 254 266 public int compare(Object o1, Object o2) throws UnsupportedOperationException { 267 if (isLocked == false) { 268 checkChainIntegrity(); 269 isLocked = true; 270 } 271 272 Iterator comparators = comparatorChain.iterator(); 274 for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex) { 275 276 Comparator comparator = (Comparator ) comparators.next(); 277 int retval = comparator.compare(o1,o2); 278 if (retval != 0) { 279 if (orderingBits.get(comparatorIndex) == true) { 281 if(Integer.MIN_VALUE == retval) { 282 retval = Integer.MAX_VALUE; 283 } else { 284 retval *= -1; 285 } 286 } 287 288 return retval; 289 } 290 291 } 292 293 return 0; 295 } 296 297 305 public int hashCode() { 306 int hash = 0; 307 if(null != comparatorChain) { 308 hash ^= comparatorChain.hashCode(); 309 } 310 if(null != orderingBits) { 311 hash ^= orderingBits.hashCode(); 312 } 313 return hash; 314 } 315 316 332 public boolean equals(Object object) { 333 if(this == object) { 334 return true; 335 } else if(null == object) { 336 return false; 337 } else if(object.getClass().equals(this.getClass())) { 338 ComparatorChain chain = (ComparatorChain)object; 339 return ( (null == orderingBits ? null == chain.orderingBits : orderingBits.equals(chain.orderingBits)) 340 && (null == comparatorChain ? null == chain.comparatorChain : comparatorChain.equals(chain.comparatorChain)) ); 341 } else { 342 return false; 343 } 344 } 345 346 } 347 | Popular Tags |