1 22 package org.jboss.util.collection; 23 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Set ; 27 import java.util.AbstractSet ; 28 import java.util.Iterator ; 29 import java.util.Collection ; 30 import java.io.Serializable ; 31 32 import org.jboss.util.NullArgumentException; 33 34 41 public class ListSet 42 extends AbstractSet 43 implements Set , Cloneable , Serializable 44 { 45 46 protected final List list; 47 48 56 public ListSet(final List list) { 57 if (list == null) 58 throw new NullArgumentException("list"); 59 60 int size = list.size(); 62 for (int i=0; i<size; i++) { 63 Object obj = list.get(i); 64 if (list.indexOf(obj) != list.lastIndexOf(obj)) { 65 throw new IllegalArgumentException 66 ("list contains duplicate entries"); 67 } 68 } 69 70 this.list = list; 71 } 72 73 76 public ListSet() { 77 this(new ArrayList ()); 78 } 79 80 86 public ListSet(final Collection elements) { 87 this(new ArrayList (elements)); 88 } 89 90 public List getList() 91 { 92 return list; 93 } 94 95 100 public int size() { 101 return list.size(); 102 } 103 104 109 public Iterator iterator() { 110 return list.iterator(); 111 } 112 113 119 public boolean add(final Object obj) { 120 boolean added = false; 121 122 if (!list.contains(obj)) { 123 added = list.add(obj); 124 } 125 126 return added; 127 } 128 129 134 public boolean isEmpty() { 135 return list.isEmpty(); 136 } 137 138 144 public boolean contains(final Object obj) { 145 return list.contains(obj); 146 } 147 148 154 public boolean remove(final Object obj) { 155 return list.remove(obj); 156 } 157 158 161 public void clear() { 162 list.clear(); 163 } 164 165 170 public Object clone() { 171 try { 172 return super.clone(); 173 } 174 catch (CloneNotSupportedException e) { 175 throw new InternalError (); 176 } 177 } 178 } 179 | Popular Tags |