1 /* 2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 /** 29 * Private implementation class for EnumSet, for "regular sized" enum types 30 * (i.e., those with 64 or fewer enum constants). 31 * 32 * @author Josh Bloch 33 * @since 1.5 34 * @serial exclude 35 */ 36 class RegularEnumSet<E extends Enum<E>> extends EnumSet<E> { 37 /** 38 * Bit vector representation of this set. The 2^k bit indicates the 39 * presence of universe[k] in this set. 40 */ 41 private long elements = 0L; 42 43 RegularEnumSet(Class<E>elementType, Enum[] universe) { 44 super(elementType, universe); 45 } 46 47 void addRange(E from, E to) { 48 elements = (-1L >>> (from.ordinal() - to.ordinal() - 1)) << from.ordinal(); 49 } 50 51 void addAll() { 52 if (universe.length != 0) 53 elements = -1L >>> -universe.length; 54 } 55 56 void complement() { 57 if (universe.length != 0) { 58 elements = ~elements; 59 elements &= -1L >>> -universe.length; // Mask unused bits 60 } 61 } 62 63 /** 64 * Returns an iterator over the elements contained in this set. The 65 * iterator traverses the elements in their <i>natural order</i> (which is 66 * the order in which the enum constants are declared). The returned 67 * Iterator is a "snapshot" iterator that will never throw {@link 68 * ConcurrentModificationException}; the elements are traversed as they 69 * existed when this call was invoked. 70 * 71 * @return an iterator over the elements contained in this set 72 */ 73 public Iterator<E> iterator() { 74 return new EnumSetIterator<E>(); 75 } 76 77 private class EnumSetIterator<E extends Enum<E>> implements Iterator<E> { 78 /** 79 * A bit vector representing the elements in the set not yet 80 * returned by this iterator. 81 */ 82 long unseen; 83 84 /** 85 * The bit representing the last element returned by this iterator 86 * but not removed, or zero if no such element exists. 87 */ 88 long lastReturned = 0; 89 90 EnumSetIterator() { 91 unseen = elements; 92 } 93 94 public boolean hasNext() { 95 return unseen != 0; 96 } 97 98 public E next() { 99 if (unseen == 0) 100 throw new NoSuchElementException(); 101 lastReturned = unseen & -unseen; 102 unseen -= lastReturned; 103 return (E) universe[Long.numberOfTrailingZeros(lastReturned)]; 104 } 105 106 public void remove() { 107 if (lastReturned == 0) 108 throw new IllegalStateException(); 109 elements -= lastReturned; 110 lastReturned = 0; 111 } 112 } 113 114 /** 115 * Returns the number of elements in this set. 116 * 117 * @return the number of elements in this set 118 */ 119 public int size() { 120 return Long.bitCount(elements); 121 } 122 123 /** 124 * Returns <tt>true</tt> if this set contains no elements. 125 * 126 * @return <tt>true</tt> if this set contains no elements 127 */ 128 public boolean isEmpty() { 129 return elements == 0; 130 } 131 132 /** 133 * Returns <tt>true</tt> if this set contains the specified element. 134 * 135 * @param e element to be checked for containment in this collection 136 * @return <tt>true</tt> if this set contains the specified element 137 */ 138 public boolean contains(Object e) { 139 if (e == null) 140 return false; 141 Class eClass = e.getClass(); 142 if (eClass != elementType && eClass.getSuperclass() != elementType) 143 return false; 144 145 return (elements & (1L << ((Enum)e).ordinal())) != 0; 146 } 147 148 // Modification Operations 149 150 /** 151 * Adds the specified element to this set if it is not already present. 152 * 153 * @param e element to be added to this set 154 * @return <tt>true</tt> if the set changed as a result of the call 155 * 156 * @throws NullPointerException if <tt>e</tt> is null 157 */ 158 public boolean add(E e) { 159 typeCheck(e); 160 161 long oldElements = elements; 162 elements |= (1L << ((Enum)e).ordinal()); 163 return elements != oldElements; 164 } 165 166 /** 167 * Removes the specified element from this set if it is present. 168 * 169 * @param e element to be removed from this set, if present 170 * @return <tt>true</tt> if the set contained the specified element 171 */ 172 public boolean remove(Object e) { 173 if (e == null) 174 return false; 175 Class eClass = e.getClass(); 176 if (eClass != elementType && eClass.getSuperclass() != elementType) 177 return false; 178 179 long oldElements = elements; 180 elements &= ~(1L << ((Enum)e).ordinal()); 181 return elements != oldElements; 182 } 183 184 // Bulk Operations 185 186 /** 187 * Returns <tt>true</tt> if this set contains all of the elements 188 * in the specified collection. 189 * 190 * @param c collection to be checked for containment in this set 191 * @return <tt>true</tt> if this set contains all of the elements 192 * in the specified collection 193 * @throws NullPointerException if the specified collection is null 194 */ 195 public boolean containsAll(Collection<?> c) { 196 if (!(c instanceof RegularEnumSet)) 197 return super.containsAll(c); 198 199 RegularEnumSet es = (RegularEnumSet)c; 200 if (es.elementType != elementType) 201 return es.isEmpty(); 202 203 return (es.elements & ~elements) == 0; 204 } 205 206 /** 207 * Adds all of the elements in the specified collection to this set. 208 * 209 * @param c collection whose elements are to be added to this set 210 * @return <tt>true</tt> if this set changed as a result of the call 211 * @throws NullPointerException if the specified collection or any 212 * of its elements are null 213 */ 214 public boolean addAll(Collection<? extends E> c) { 215 if (!(c instanceof RegularEnumSet)) 216 return super.addAll(c); 217 218 RegularEnumSet es = (RegularEnumSet)c; 219 if (es.elementType != elementType) { 220 if (es.isEmpty()) 221 return false; 222 else 223 throw new ClassCastException( 224 es.elementType + " != " + elementType); 225 } 226 227 long oldElements = elements; 228 elements |= es.elements; 229 return elements != oldElements; 230 } 231 232 /** 233 * Removes from this set all of its elements that are contained in 234 * the specified collection. 235 * 236 * @param c elements to be removed from this set 237 * @return <tt>true</tt> if this set changed as a result of the call 238 * @throws NullPointerException if the specified collection is null 239 */ 240 public boolean removeAll(Collection<?> c) { 241 if (!(c instanceof RegularEnumSet)) 242 return super.removeAll(c); 243 244 RegularEnumSet es = (RegularEnumSet)c; 245 if (es.elementType != elementType) 246 return false; 247 248 long oldElements = elements; 249 elements &= ~es.elements; 250 return elements != oldElements; 251 } 252 253 /** 254 * Retains only the elements in this set that are contained in the 255 * specified collection. 256 * 257 * @param c elements to be retained in this set 258 * @return <tt>true</tt> if this set changed as a result of the call 259 * @throws NullPointerException if the specified collection is null 260 */ 261 public boolean retainAll(Collection<?> c) { 262 if (!(c instanceof RegularEnumSet)) 263 return super.retainAll(c); 264 265 RegularEnumSet<?> es = (RegularEnumSet<?>)c; 266 if (es.elementType != elementType) { 267 boolean changed = (elements != 0); 268 elements = 0; 269 return changed; 270 } 271 272 long oldElements = elements; 273 elements &= es.elements; 274 return elements != oldElements; 275 } 276 277 /** 278 * Removes all of the elements from this set. 279 */ 280 public void clear() { 281 elements = 0; 282 } 283 284 /** 285 * Compares the specified object with this set for equality. Returns 286 * <tt>true</tt> if the given object is also a set, the two sets have 287 * the same size, and every member of the given set is contained in 288 * this set. 289 * 290 * @param e object to be compared for equality with this set 291 * @return <tt>true</tt> if the specified object is equal to this set 292 */ 293 public boolean equals(Object o) { 294 if (!(o instanceof RegularEnumSet)) 295 return super.equals(o); 296 297 RegularEnumSet es = (RegularEnumSet)o; 298 if (es.elementType != elementType) 299 return elements == 0 && es.elements == 0; 300 return es.elements == elements; 301 } 302 }