< prev index next >

src/java.base/share/classes/java/util/JumboEnumSet.java

Print this page




 146                 throw new IllegalStateException();
 147             final long oldElements = elements[lastReturnedIndex];
 148             elements[lastReturnedIndex] &= ~lastReturned;
 149             if (oldElements != elements[lastReturnedIndex]) {
 150                 size--;
 151             }
 152             lastReturned = 0;
 153         }
 154     }
 155 
 156     /**
 157      * Returns the number of elements in this set.
 158      *
 159      * @return the number of elements in this set
 160      */
 161     public int size() {
 162         return size;
 163     }
 164 
 165     /**
 166      * Returns <tt>true</tt> if this set contains no elements.
 167      *
 168      * @return <tt>true</tt> if this set contains no elements
 169      */
 170     public boolean isEmpty() {
 171         return size == 0;
 172     }
 173 
 174     /**
 175      * Returns <tt>true</tt> if this set contains the specified element.
 176      *
 177      * @param e element to be checked for containment in this collection
 178      * @return <tt>true</tt> if this set contains the specified element
 179      */
 180     public boolean contains(Object e) {
 181         if (e == null)
 182             return false;
 183         Class<?> eClass = e.getClass();
 184         if (eClass != elementType && eClass.getSuperclass() != elementType)
 185             return false;
 186 
 187         int eOrdinal = ((Enum<?>)e).ordinal();
 188         return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0;
 189     }
 190 
 191     // Modification Operations
 192 
 193     /**
 194      * Adds the specified element to this set if it is not already present.
 195      *
 196      * @param e element to be added to this set
 197      * @return <tt>true</tt> if the set changed as a result of the call
 198      *
 199      * @throws NullPointerException if <tt>e</tt> is null
 200      */
 201     public boolean add(E e) {
 202         typeCheck(e);
 203 
 204         int eOrdinal = e.ordinal();
 205         int eWordNum = eOrdinal >>> 6;
 206 
 207         long oldElements = elements[eWordNum];
 208         elements[eWordNum] |= (1L << eOrdinal);
 209         boolean result = (elements[eWordNum] != oldElements);
 210         if (result)
 211             size++;
 212         return result;
 213     }
 214 
 215     /**
 216      * Removes the specified element from this set if it is present.
 217      *
 218      * @param e element to be removed from this set, if present
 219      * @return <tt>true</tt> if the set contained the specified element
 220      */
 221     public boolean remove(Object e) {
 222         if (e == null)
 223             return false;
 224         Class<?> eClass = e.getClass();
 225         if (eClass != elementType && eClass.getSuperclass() != elementType)
 226             return false;
 227         int eOrdinal = ((Enum<?>)e).ordinal();
 228         int eWordNum = eOrdinal >>> 6;
 229 
 230         long oldElements = elements[eWordNum];
 231         elements[eWordNum] &= ~(1L << eOrdinal);
 232         boolean result = (elements[eWordNum] != oldElements);
 233         if (result)
 234             size--;
 235         return result;
 236     }
 237 
 238     // Bulk Operations
 239 
 240     /**
 241      * Returns <tt>true</tt> if this set contains all of the elements
 242      * in the specified collection.
 243      *
 244      * @param c collection to be checked for containment in this set
 245      * @return <tt>true</tt> if this set contains all of the elements
 246      *        in the specified collection
 247      * @throws NullPointerException if the specified collection is null
 248      */
 249     public boolean containsAll(Collection<?> c) {
 250         if (!(c instanceof JumboEnumSet))
 251             return super.containsAll(c);
 252 
 253         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 254         if (es.elementType != elementType)
 255             return es.isEmpty();
 256 
 257         for (int i = 0; i < elements.length; i++)
 258             if ((es.elements[i] & ~elements[i]) != 0)
 259                 return false;
 260         return true;
 261     }
 262 
 263     /**
 264      * Adds all of the elements in the specified collection to this set.
 265      *
 266      * @param c collection whose elements are to be added to this set
 267      * @return <tt>true</tt> if this set changed as a result of the call
 268      * @throws NullPointerException if the specified collection or any of
 269      *     its elements are null
 270      */
 271     public boolean addAll(Collection<? extends E> c) {
 272         if (!(c instanceof JumboEnumSet))
 273             return super.addAll(c);
 274 
 275         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 276         if (es.elementType != elementType) {
 277             if (es.isEmpty())
 278                 return false;
 279             else
 280                 throw new ClassCastException(
 281                     es.elementType + " != " + elementType);
 282         }
 283 
 284         for (int i = 0; i < elements.length; i++)
 285             elements[i] |= es.elements[i];
 286         return recalculateSize();
 287     }
 288 
 289     /**
 290      * Removes from this set all of its elements that are contained in
 291      * the specified collection.
 292      *
 293      * @param c elements to be removed from this set
 294      * @return <tt>true</tt> if this set changed as a result of the call
 295      * @throws NullPointerException if the specified collection is null
 296      */
 297     public boolean removeAll(Collection<?> c) {
 298         if (!(c instanceof JumboEnumSet))
 299             return super.removeAll(c);
 300 
 301         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 302         if (es.elementType != elementType)
 303             return false;
 304 
 305         for (int i = 0; i < elements.length; i++)
 306             elements[i] &= ~es.elements[i];
 307         return recalculateSize();
 308     }
 309 
 310     /**
 311      * Retains only the elements in this set that are contained in the
 312      * specified collection.
 313      *
 314      * @param c elements to be retained in this set
 315      * @return <tt>true</tt> if this set changed as a result of the call
 316      * @throws NullPointerException if the specified collection is null
 317      */
 318     public boolean retainAll(Collection<?> c) {
 319         if (!(c instanceof JumboEnumSet))
 320             return super.retainAll(c);
 321 
 322         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 323         if (es.elementType != elementType) {
 324             boolean changed = (size != 0);
 325             clear();
 326             return changed;
 327         }
 328 
 329         for (int i = 0; i < elements.length; i++)
 330             elements[i] &= es.elements[i];
 331         return recalculateSize();
 332     }
 333 
 334     /**
 335      * Removes all of the elements from this set.
 336      */
 337     public void clear() {
 338         Arrays.fill(elements, 0);
 339         size = 0;
 340     }
 341 
 342     /**
 343      * Compares the specified object with this set for equality.  Returns
 344      * <tt>true</tt> if the given object is also a set, the two sets have
 345      * the same size, and every member of the given set is contained in
 346      * this set.
 347      *
 348      * @param o object to be compared for equality with this set
 349      * @return <tt>true</tt> if the specified object is equal to this set
 350      */
 351     public boolean equals(Object o) {
 352         if (!(o instanceof JumboEnumSet))
 353             return super.equals(o);
 354 
 355         JumboEnumSet<?> es = (JumboEnumSet<?>)o;
 356         if (es.elementType != elementType)
 357             return size == 0 && es.size == 0;
 358 
 359         return Arrays.equals(es.elements, elements);
 360     }
 361 
 362     /**
 363      * Recalculates the size of the set.  Returns true if it's changed.
 364      */
 365     private boolean recalculateSize() {
 366         int oldSize = size;
 367         size = 0;
 368         for (long elt : elements)
 369             size += Long.bitCount(elt);


 146                 throw new IllegalStateException();
 147             final long oldElements = elements[lastReturnedIndex];
 148             elements[lastReturnedIndex] &= ~lastReturned;
 149             if (oldElements != elements[lastReturnedIndex]) {
 150                 size--;
 151             }
 152             lastReturned = 0;
 153         }
 154     }
 155 
 156     /**
 157      * Returns the number of elements in this set.
 158      *
 159      * @return the number of elements in this set
 160      */
 161     public int size() {
 162         return size;
 163     }
 164 
 165     /**
 166      * Returns {@code true} if this set contains no elements.
 167      *
 168      * @return {@code true} if this set contains no elements
 169      */
 170     public boolean isEmpty() {
 171         return size == 0;
 172     }
 173 
 174     /**
 175      * Returns {@code true} if this set contains the specified element.
 176      *
 177      * @param e element to be checked for containment in this collection
 178      * @return {@code true} if this set contains the specified element
 179      */
 180     public boolean contains(Object e) {
 181         if (e == null)
 182             return false;
 183         Class<?> eClass = e.getClass();
 184         if (eClass != elementType && eClass.getSuperclass() != elementType)
 185             return false;
 186 
 187         int eOrdinal = ((Enum<?>)e).ordinal();
 188         return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0;
 189     }
 190 
 191     // Modification Operations
 192 
 193     /**
 194      * Adds the specified element to this set if it is not already present.
 195      *
 196      * @param e element to be added to this set
 197      * @return {@code true} if the set changed as a result of the call
 198      *
 199      * @throws NullPointerException if {@code e} is null
 200      */
 201     public boolean add(E e) {
 202         typeCheck(e);
 203 
 204         int eOrdinal = e.ordinal();
 205         int eWordNum = eOrdinal >>> 6;
 206 
 207         long oldElements = elements[eWordNum];
 208         elements[eWordNum] |= (1L << eOrdinal);
 209         boolean result = (elements[eWordNum] != oldElements);
 210         if (result)
 211             size++;
 212         return result;
 213     }
 214 
 215     /**
 216      * Removes the specified element from this set if it is present.
 217      *
 218      * @param e element to be removed from this set, if present
 219      * @return {@code true} if the set contained the specified element
 220      */
 221     public boolean remove(Object e) {
 222         if (e == null)
 223             return false;
 224         Class<?> eClass = e.getClass();
 225         if (eClass != elementType && eClass.getSuperclass() != elementType)
 226             return false;
 227         int eOrdinal = ((Enum<?>)e).ordinal();
 228         int eWordNum = eOrdinal >>> 6;
 229 
 230         long oldElements = elements[eWordNum];
 231         elements[eWordNum] &= ~(1L << eOrdinal);
 232         boolean result = (elements[eWordNum] != oldElements);
 233         if (result)
 234             size--;
 235         return result;
 236     }
 237 
 238     // Bulk Operations
 239 
 240     /**
 241      * Returns {@code true} if this set contains all of the elements
 242      * in the specified collection.
 243      *
 244      * @param c collection to be checked for containment in this set
 245      * @return {@code true} if this set contains all of the elements
 246      *        in the specified collection
 247      * @throws NullPointerException if the specified collection is null
 248      */
 249     public boolean containsAll(Collection<?> c) {
 250         if (!(c instanceof JumboEnumSet))
 251             return super.containsAll(c);
 252 
 253         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 254         if (es.elementType != elementType)
 255             return es.isEmpty();
 256 
 257         for (int i = 0; i < elements.length; i++)
 258             if ((es.elements[i] & ~elements[i]) != 0)
 259                 return false;
 260         return true;
 261     }
 262 
 263     /**
 264      * Adds all of the elements in the specified collection to this set.
 265      *
 266      * @param c collection whose elements are to be added to this set
 267      * @return {@code true} if this set changed as a result of the call
 268      * @throws NullPointerException if the specified collection or any of
 269      *     its elements are null
 270      */
 271     public boolean addAll(Collection<? extends E> c) {
 272         if (!(c instanceof JumboEnumSet))
 273             return super.addAll(c);
 274 
 275         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 276         if (es.elementType != elementType) {
 277             if (es.isEmpty())
 278                 return false;
 279             else
 280                 throw new ClassCastException(
 281                     es.elementType + " != " + elementType);
 282         }
 283 
 284         for (int i = 0; i < elements.length; i++)
 285             elements[i] |= es.elements[i];
 286         return recalculateSize();
 287     }
 288 
 289     /**
 290      * Removes from this set all of its elements that are contained in
 291      * the specified collection.
 292      *
 293      * @param c elements to be removed from this set
 294      * @return {@code true} if this set changed as a result of the call
 295      * @throws NullPointerException if the specified collection is null
 296      */
 297     public boolean removeAll(Collection<?> c) {
 298         if (!(c instanceof JumboEnumSet))
 299             return super.removeAll(c);
 300 
 301         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 302         if (es.elementType != elementType)
 303             return false;
 304 
 305         for (int i = 0; i < elements.length; i++)
 306             elements[i] &= ~es.elements[i];
 307         return recalculateSize();
 308     }
 309 
 310     /**
 311      * Retains only the elements in this set that are contained in the
 312      * specified collection.
 313      *
 314      * @param c elements to be retained in this set
 315      * @return {@code true} if this set changed as a result of the call
 316      * @throws NullPointerException if the specified collection is null
 317      */
 318     public boolean retainAll(Collection<?> c) {
 319         if (!(c instanceof JumboEnumSet))
 320             return super.retainAll(c);
 321 
 322         JumboEnumSet<?> es = (JumboEnumSet<?>)c;
 323         if (es.elementType != elementType) {
 324             boolean changed = (size != 0);
 325             clear();
 326             return changed;
 327         }
 328 
 329         for (int i = 0; i < elements.length; i++)
 330             elements[i] &= es.elements[i];
 331         return recalculateSize();
 332     }
 333 
 334     /**
 335      * Removes all of the elements from this set.
 336      */
 337     public void clear() {
 338         Arrays.fill(elements, 0);
 339         size = 0;
 340     }
 341 
 342     /**
 343      * Compares the specified object with this set for equality.  Returns
 344      * {@code true} if the given object is also a set, the two sets have
 345      * the same size, and every member of the given set is contained in
 346      * this set.
 347      *
 348      * @param o object to be compared for equality with this set
 349      * @return {@code true} if the specified object is equal to this set
 350      */
 351     public boolean equals(Object o) {
 352         if (!(o instanceof JumboEnumSet))
 353             return super.equals(o);
 354 
 355         JumboEnumSet<?> es = (JumboEnumSet<?>)o;
 356         if (es.elementType != elementType)
 357             return size == 0 && es.size == 0;
 358 
 359         return Arrays.equals(es.elements, elements);
 360     }
 361 
 362     /**
 363      * Recalculates the size of the set.  Returns true if it's changed.
 364      */
 365     private boolean recalculateSize() {
 366         int oldSize = size;
 367         size = 0;
 368         for (long elt : elements)
 369             size += Long.bitCount(elt);
< prev index next >