1 16 package org.apache.commons.collections.list; 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.ListIterator ; 24 import java.util.Set ; 25 26 import org.apache.commons.collections.iterators.AbstractIteratorDecorator; 27 import org.apache.commons.collections.iterators.AbstractListIteratorDecorator; 28 import org.apache.commons.collections.set.UnmodifiableSet; 29 30 51 public class SetUniqueList extends AbstractSerializableListDecorator { 52 53 54 private static final long serialVersionUID = 7196982186153478694L; 55 56 59 protected final Set set; 60 61 70 public static SetUniqueList decorate(List list) { 71 if (list == null) { 72 throw new IllegalArgumentException ("List must not be null"); 73 } 74 if (list.isEmpty()) { 75 return new SetUniqueList(list, new HashSet ()); 76 } else { 77 List temp = new ArrayList (list); 78 list.clear(); 79 SetUniqueList sl = new SetUniqueList(list, new HashSet ()); 80 sl.addAll(temp); 81 return sl; 82 } 83 } 84 85 95 protected SetUniqueList(List list, Set set) { 96 super(list); 97 if (set == null) { 98 throw new IllegalArgumentException ("Set must not be null"); 99 } 100 this.set = set; 101 } 102 103 109 public Set asSet() { 110 return UnmodifiableSet.decorate(set); 111 } 112 113 125 public boolean add(Object object) { 126 final int sizeBefore = size(); 128 129 add(size(), object); 131 132 return (sizeBefore != size()); 134 } 135 136 146 public void add(int index, Object object) { 147 if (set.contains(object) == false) { 149 super.add(index, object); 150 set.add(object); 151 } 152 } 153 154 163 public boolean addAll(Collection coll) { 164 return addAll(size(), coll); 165 } 166 167 181 public boolean addAll(int index, Collection coll) { 182 final int sizeBefore = size(); 184 185 for (final Iterator it = coll.iterator(); it.hasNext();) { 187 add(it.next()); 188 } 189 190 return sizeBefore != size(); 192 } 193 194 207 public Object set(int index, Object object) { 208 int pos = indexOf(object); 209 Object result = super.set(index, object); 210 if (pos == -1 || pos == index) { 211 return result; 212 } 213 return remove(pos); 214 } 215 216 public boolean remove(Object object) { 217 boolean result = super.remove(object); 218 set.remove(object); 219 return result; 220 } 221 222 public Object remove(int index) { 223 Object result = super.remove(index); 224 set.remove(result); 225 return result; 226 } 227 228 public boolean removeAll(Collection coll) { 229 boolean result = super.removeAll(coll); 230 set.removeAll(coll); 231 return result; 232 } 233 234 public boolean retainAll(Collection coll) { 235 boolean result = super.retainAll(coll); 236 set.retainAll(coll); 237 return result; 238 } 239 240 public void clear() { 241 super.clear(); 242 set.clear(); 243 } 244 245 public boolean contains(Object object) { 246 return set.contains(object); 247 } 248 249 public boolean containsAll(Collection coll) { 250 return set.containsAll(coll); 251 } 252 253 public Iterator iterator() { 254 return new SetListIterator(super.iterator(), set); 255 } 256 257 public ListIterator listIterator() { 258 return new SetListListIterator(super.listIterator(), set); 259 } 260 261 public ListIterator listIterator(int index) { 262 return new SetListListIterator(super.listIterator(index), set); 263 } 264 265 public List subList(int fromIndex, int toIndex) { 266 return new SetUniqueList(super.subList(fromIndex, toIndex), set); 267 } 268 269 273 static class SetListIterator extends AbstractIteratorDecorator { 274 275 protected final Set set; 276 protected Object last = null; 277 278 protected SetListIterator(Iterator it, Set set) { 279 super(it); 280 this.set = set; 281 } 282 283 public Object next() { 284 last = super.next(); 285 return last; 286 } 287 288 public void remove() { 289 super.remove(); 290 set.remove(last); 291 last = null; 292 } 293 } 294 295 298 static class SetListListIterator extends AbstractListIteratorDecorator { 299 300 protected final Set set; 301 protected Object last = null; 302 303 protected SetListListIterator(ListIterator it, Set set) { 304 super(it); 305 this.set = set; 306 } 307 308 public Object next() { 309 last = super.next(); 310 return last; 311 } 312 313 public Object previous() { 314 last = super.previous(); 315 return last; 316 } 317 318 public void remove() { 319 super.remove(); 320 set.remove(last); 321 last = null; 322 } 323 324 public void add(Object object) { 325 if (set.contains(object) == false) { 326 super.add(object); 327 set.add(object); 328 } 329 } 330 331 public void set(Object object) { 332 throw new UnsupportedOperationException ("ListIterator does not support set"); 333 } 334 } 335 336 } 337 | Popular Tags |