1 16 package org.apache.commons.collections.comparators; 17 18 import java.util.Comparator ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 50 public class FixedOrderComparator implements Comparator { 51 52 56 public static final int UNKNOWN_BEFORE = 0; 57 58 62 public static final int UNKNOWN_AFTER = 1; 63 64 69 public static final int UNKNOWN_THROW_EXCEPTION = 2; 70 71 72 private final Map map = new HashMap (); 73 74 private int counter = 0; 75 76 private boolean isLocked = false; 77 78 private int unknownObjectBehavior = UNKNOWN_THROW_EXCEPTION; 79 80 85 public FixedOrderComparator() { 86 super(); 87 } 88 89 98 public FixedOrderComparator(Object [] items) { 99 super(); 100 if (items == null) { 101 throw new IllegalArgumentException ("The list of items must not be null"); 102 } 103 for (int i = 0; i < items.length; i++) { 104 add(items[i]); 105 } 106 } 107 108 117 public FixedOrderComparator(List items) { 118 super(); 119 if (items == null) { 120 throw new IllegalArgumentException ("The list of items must not be null"); 121 } 122 for (Iterator it = items.iterator(); it.hasNext();) { 123 add(it.next()); 124 } 125 } 126 127 136 public boolean isLocked() { 137 return isLocked; 138 } 139 140 145 protected void checkLocked() { 146 if (isLocked()) { 147 throw new UnsupportedOperationException ("Cannot modify a FixedOrderComparator after a comparison"); 148 } 149 } 150 151 157 public int getUnknownObjectBehavior() { 158 return unknownObjectBehavior; 159 } 160 161 169 public void setUnknownObjectBehavior(int unknownObjectBehavior) { 170 checkLocked(); 171 if (unknownObjectBehavior != UNKNOWN_AFTER 172 && unknownObjectBehavior != UNKNOWN_BEFORE 173 && unknownObjectBehavior != UNKNOWN_THROW_EXCEPTION) { 174 throw new IllegalArgumentException ("Unrecognised value for unknown behaviour flag"); 175 } 176 this.unknownObjectBehavior = unknownObjectBehavior; 177 } 178 179 191 public boolean add(Object obj) { 192 checkLocked(); 193 Object position = map.put(obj, new Integer (counter++)); 194 return (position == null); 195 } 196 197 210 public boolean addAsEqual(Object existingObj, Object newObj) { 211 checkLocked(); 212 Integer position = (Integer ) map.get(existingObj); 213 if (position == null) { 214 throw new IllegalArgumentException (existingObj + " not known to " + this); 215 } 216 Object result = map.put(newObj, position); 217 return (result == null); 218 } 219 220 236 public int compare(Object obj1, Object obj2) { 237 isLocked = true; 238 Integer position1 = (Integer ) map.get(obj1); 239 Integer position2 = (Integer ) map.get(obj2); 240 if (position1 == null || position2 == null) { 241 switch (unknownObjectBehavior) { 242 case UNKNOWN_BEFORE : 243 if (position1 == null) { 244 return (position2 == null) ? 0 : -1; 245 } else { 246 return 1; 247 } 248 case UNKNOWN_AFTER : 249 if (position1 == null) { 250 return (position2 == null) ? 0 : 1; 251 } else { 252 return -1; 253 } 254 case UNKNOWN_THROW_EXCEPTION : 255 Object unknownObj = (position1 == null) ? obj1 : obj2; 256 throw new IllegalArgumentException ("Attempting to compare unknown object " + unknownObj); 257 default : 258 throw new UnsupportedOperationException ("Unknown unknownObjectBehavior: " + unknownObjectBehavior); 259 } 260 } else { 261 return position1.compareTo(position2); 262 } 263 } 264 265 } 266 | Popular Tags |