1 7 8 package java.util; 9 10 18 class RegularEnumSet<E extends Enum <E>> extends EnumSet <E> { 19 23 private long elements = 0L; 24 25 RegularEnumSet(Class <E>elementType, Enum [] universe) { 26 super(elementType, universe); 27 } 28 29 void addRange(E from, E to) { 30 elements = (-1L >>> (from.ordinal() - to.ordinal() - 1)) << from.ordinal(); 31 } 32 33 void addAll() { 34 if (universe.length != 0) 35 elements = -1L >>> -universe.length; 36 } 37 38 void complement() { 39 if (universe.length != 0) { 40 elements = ~elements; 41 elements &= -1L >>> -universe.length; } 43 } 44 45 55 public Iterator <E> iterator() { 56 return new EnumSetIterator<E>(); 57 } 58 59 private class EnumSetIterator<E extends Enum <E>> implements Iterator <E> { 60 64 long unseen; 65 66 70 long lastReturned = 0; 71 72 EnumSetIterator() { 73 unseen = elements; 74 } 75 76 public boolean hasNext() { 77 return unseen != 0; 78 } 79 80 public E next() { 81 if (unseen == 0) 82 throw new NoSuchElementException (); 83 lastReturned = unseen & -unseen; 84 unseen -= lastReturned; 85 return (E) universe[Long.numberOfTrailingZeros(lastReturned)]; 86 } 87 88 public void remove() { 89 if (lastReturned == 0) 90 throw new IllegalStateException (); 91 elements -= lastReturned; 92 lastReturned = 0; 93 } 94 } 95 96 101 public int size() { 102 return Long.bitCount(elements); 103 } 104 105 110 public boolean isEmpty() { 111 return elements == 0; 112 } 113 114 120 public boolean contains(Object e) { 121 if (e == null) 122 return false; 123 Class eClass = e.getClass(); 124 if (eClass != elementType && eClass.getSuperclass() != elementType) 125 return false; 126 127 return (elements & (1L << ((Enum )e).ordinal())) != 0; 128 } 129 130 132 140 public boolean add(E e) { 141 typeCheck(e); 142 143 long oldElements = elements; 144 elements |= (1L << ((Enum )e).ordinal()); 145 return elements != oldElements; 146 } 147 148 154 public boolean remove(Object e) { 155 if (e == null) 156 return false; 157 Class eClass = e.getClass(); 158 if (eClass != elementType && eClass.getSuperclass() != elementType) 159 return false; 160 161 long oldElements = elements; 162 elements &= ~(1L << ((Enum )e).ordinal()); 163 return elements != oldElements; 164 } 165 166 168 177 public boolean containsAll(Collection <?> c) { 178 if (!(c instanceof RegularEnumSet )) 179 return super.containsAll(c); 180 181 RegularEnumSet es = (RegularEnumSet )c; 182 if (es.elementType != elementType) 183 return es.isEmpty(); 184 185 return (es.elements & ~elements) == 0; 186 } 187 188 196 public boolean addAll(Collection <? extends E> c) { 197 if (!(c instanceof RegularEnumSet )) 198 return super.addAll(c); 199 200 RegularEnumSet es = (RegularEnumSet )c; 201 if (es.elementType != elementType) { 202 if (es.isEmpty()) 203 return false; 204 else 205 throw new ClassCastException ( 206 es.elementType + " != " + elementType); 207 } 208 209 long oldElements = elements; 210 elements |= es.elements; 211 return elements != oldElements; 212 } 213 214 222 public boolean removeAll(Collection <?> c) { 223 if (!(c instanceof RegularEnumSet )) 224 return super.removeAll(c); 225 226 RegularEnumSet es = (RegularEnumSet )c; 227 if (es.elementType != elementType) 228 return false; 229 230 long oldElements = elements; 231 elements &= ~es.elements; 232 return elements != oldElements; 233 } 234 235 243 public boolean retainAll(Collection <?> c) { 244 if (!(c instanceof RegularEnumSet )) 245 return super.retainAll(c); 246 247 RegularEnumSet es = (RegularEnumSet )c; 248 if (es.elementType != elementType) { 249 elements = 0; 250 return true; 251 } 252 253 long oldElements = elements; 254 elements &= es.elements; 255 return elements != oldElements; 256 } 257 258 261 public void clear() { 262 elements = 0; 263 } 264 265 274 public boolean equals(Object o) { 275 if (!(o instanceof RegularEnumSet )) 276 return super.equals(o); 277 278 RegularEnumSet es = (RegularEnumSet )o; 279 if (es.elementType != elementType) 280 return elements == 0 && es.elements == 0; 281 return es.elements == elements; 282 } 283 } 284 | Popular Tags |