1 16 package org.apache.commons.collections.set; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Set ; 24 25 import org.apache.commons.collections.iterators.AbstractIteratorDecorator; 26 import org.apache.commons.collections.list.UnmodifiableList; 27 28 52 public class ListOrderedSet extends AbstractSerializableSetDecorator implements Set { 53 54 55 private static final long serialVersionUID = -228664372470420141L; 56 57 58 protected final List setOrder; 59 60 69 public static ListOrderedSet decorate(Set set, List list) { 70 if (set == null) { 71 throw new IllegalArgumentException ("Set must not be null"); 72 } 73 if (list == null) { 74 throw new IllegalArgumentException ("List must not be null"); 75 } 76 if (set.size() > 0 || list.size() > 0) { 77 throw new IllegalArgumentException ("Set and List must be empty"); 78 } 79 return new ListOrderedSet(set, list); 80 } 81 82 90 public static ListOrderedSet decorate(Set set) { 91 return new ListOrderedSet(set); 92 } 93 94 102 public static ListOrderedSet decorate(List list) { 103 if (list == null) { 104 throw new IllegalArgumentException ("List must not be null"); 105 } 106 Set set = new HashSet (list); 107 list.retainAll(set); 108 109 return new ListOrderedSet(set, list); 110 } 111 112 119 public ListOrderedSet() { 120 super(new HashSet ()); 121 setOrder = new ArrayList (); 122 } 123 124 130 protected ListOrderedSet(Set set) { 131 super(set); 132 setOrder = new ArrayList (set); 133 } 134 135 144 protected ListOrderedSet(Set set, List list) { 145 super(set); 146 if (list == null) { 147 throw new IllegalArgumentException ("List must not be null"); 148 } 149 setOrder = list; 150 } 151 152 158 public List asList() { 159 return UnmodifiableList.decorate(setOrder); 160 } 161 162 public void clear() { 164 collection.clear(); 165 setOrder.clear(); 166 } 167 168 public Iterator iterator() { 169 return new OrderedSetIterator(setOrder.iterator(), collection); 170 } 171 172 public boolean add(Object object) { 173 if (collection.contains(object)) { 174 return collection.add(object); 176 } else { 177 boolean result = collection.add(object); 179 setOrder.add(object); 180 return result; 181 } 182 } 183 184 public boolean addAll(Collection coll) { 185 boolean result = false; 186 for (Iterator it = coll.iterator(); it.hasNext();) { 187 Object object = it.next(); 188 result = result | add(object); 189 } 190 return result; 191 } 192 193 public boolean remove(Object object) { 194 boolean result = collection.remove(object); 195 setOrder.remove(object); 196 return result; 197 } 198 199 public boolean removeAll(Collection coll) { 200 boolean result = false; 201 for (Iterator it = coll.iterator(); it.hasNext();) { 202 Object object = it.next(); 203 result = result | remove(object); 204 } 205 return result; 206 } 207 208 public boolean retainAll(Collection coll) { 209 boolean result = collection.retainAll(coll); 210 if (result == false) { 211 return false; 212 } else if (collection.size() == 0) { 213 setOrder.clear(); 214 } else { 215 for (Iterator it = setOrder.iterator(); it.hasNext();) { 216 Object object = it.next(); 217 if (collection.contains(object) == false) { 218 it.remove(); 219 } 220 } 221 } 222 return result; 223 } 224 225 public Object [] toArray() { 226 return setOrder.toArray(); 227 } 228 229 public Object [] toArray(Object a[]) { 230 return setOrder.toArray(a); 231 } 232 233 public Object get(int index) { 235 return setOrder.get(index); 236 } 237 238 public int indexOf(Object object) { 239 return setOrder.indexOf(object); 240 } 241 242 public void add(int index, Object object) { 243 if (contains(object) == false) { 244 collection.add(object); 245 setOrder.add(index, object); 246 } 247 } 248 249 public boolean addAll(int index, Collection coll) { 250 boolean changed = false; 251 for (Iterator it = coll.iterator(); it.hasNext();) { 252 Object object = it.next(); 253 if (contains(object) == false) { 254 collection.add(object); 255 setOrder.add(index, object); 256 index++; 257 changed = true; 258 } 259 } 260 return changed; 261 } 262 263 public Object remove(int index) { 264 Object obj = setOrder.remove(index); 265 remove(obj); 266 return obj; 267 } 268 269 274 public String toString() { 276 return setOrder.toString(); 277 } 278 279 283 static class OrderedSetIterator extends AbstractIteratorDecorator { 284 285 286 protected final Collection set; 287 288 protected Object last; 289 290 private OrderedSetIterator(Iterator iterator, Collection set) { 291 super(iterator); 292 this.set = set; 293 } 294 295 public Object next() { 296 last = iterator.next(); 297 return last; 298 } 299 300 public void remove() { 301 set.remove(last); 302 iterator.remove(); 303 last = null; 304 } 305 } 306 307 } 308 | Popular Tags |