1 /*
   2  * Copyright (c) 2003, 2018, 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     private static final long serialVersionUID = 3411599620347842686L;
  38     /**
  39      * Bit vector representation of this set.  The 2^k bit indicates the
  40      * presence of universe[k] in this set.
  41      */
  42     private byte[] bits = new byte[8];
  43 
  44     private void setBits(long elems) {
  45         for (int i=0; i<bits.length; i++)
  46             bits[i] = (byte)(elems >> (i<<3));
  47     }
  48 
  49     private long elems() {
  50         long elems = 0L;
  51         for (int i=0; i<bits.length; i++)
  52             elems |= (((long)bits[i]) & 0xFFL) << (i<<3);
  53 
  54         return elems;
  55     }
  56 
  57     /**
  58      * Returns a copy of this set.
  59      *
  60      * @return a copy of this set
  61      */
  62     @SuppressWarnings("unchecked")
  63     public EnumSet<E> clone() {
  64         RegularEnumSet<E> es = (RegularEnumSet<E>) super.clone();
  65         es.bits = new byte[8];
  66         es.setBits(elems());
  67         return es;
  68     }
  69 
  70     RegularEnumSet(Class<E>elementType, Enum<?>[] universe) {
  71         super(elementType, universe);
  72     }
  73 
  74     void addRange(E from, E to) {
  75         setBits((-1L >>>  (from.ordinal() - to.ordinal() - 1)) << from.ordinal());
  76     }
  77 
  78     void addAll() {
  79         if (universe.length != 0)
  80             setBits(-1L >>> -universe.length);
  81     }
  82 
  83     void complement() {
  84         if (universe.length != 0) {
  85             setBits(~elems() & (-1L >>> -universe.length));
  86         }
  87     }
  88 
  89     /**
  90      * Returns an iterator over the elements contained in this set.  The
  91      * iterator traverses the elements in their <i>natural order</i> (which is
  92      * the order in which the enum constants are declared). The returned
  93      * Iterator is a "snapshot" iterator that will never throw {@link
  94      * ConcurrentModificationException}; the elements are traversed as they
  95      * existed when this call was invoked.
  96      *
  97      * @return an iterator over the elements contained in this set
  98      */
  99     public Iterator<E> iterator() {
 100         return new EnumSetIterator<>();
 101     }
 102 
 103     private class EnumSetIterator<E extends Enum<E>> implements Iterator<E> {
 104         /**
 105          * A bit vector representing the elements in the set not yet
 106          * returned by this iterator.
 107          */
 108         long unseen;
 109 
 110         /**
 111          * The bit representing the last element returned by this iterator
 112          * but not removed, or zero if no such element exists.
 113          */
 114         long lastReturned = 0;
 115 
 116         EnumSetIterator() {
 117             unseen = elems();
 118         }
 119 
 120         public boolean hasNext() {
 121             return unseen != 0;
 122         }
 123 
 124         @SuppressWarnings("unchecked")
 125         public E next() {
 126             if (unseen == 0)
 127                 throw new NoSuchElementException();
 128             lastReturned = unseen & -unseen;
 129             unseen -= lastReturned;
 130             return (E) universe[Long.numberOfTrailingZeros(lastReturned)];
 131         }
 132 
 133         public void remove() {
 134             if (lastReturned == 0)
 135                 throw new IllegalStateException();
 136             setBits(elems() & ~lastReturned);
 137             lastReturned = 0;
 138         }
 139     }
 140 
 141     /**
 142      * Returns the number of elements in this set.
 143      *
 144      * @return the number of elements in this set
 145      */
 146     public int size() {
 147         return Long.bitCount(elems());
 148     }
 149 
 150     /**
 151      * Returns {@code true} if this set contains no elements.
 152      *
 153      * @return {@code true} if this set contains no elements
 154      */
 155     public boolean isEmpty() {
 156         return elems() == 0;
 157     }
 158 
 159     /**
 160      * Returns {@code true} if this set contains the specified element.
 161      *
 162      * @param e element to be checked for containment in this collection
 163      * @return {@code true} if this set contains the specified element
 164      */
 165     public boolean contains(Object e) {
 166         if (e == null)
 167             return false;
 168         Class<?> eClass = e.getClass();
 169         if (eClass != elementType && eClass.getSuperclass() != elementType)
 170             return false;
 171 
 172         return System.isBit(bits, ((Enum<?>)e).ordinal());
 173     }
 174 
 175     // Modification Operations
 176 
 177     /**
 178      * Adds the specified element to this set if it is not already present.
 179      *
 180      * @param e element to be added to this set
 181      * @return {@code true} if the set changed as a result of the call
 182      *
 183      * @throws NullPointerException if {@code e} is null
 184      */
 185     public boolean add(E e) {
 186         typeCheck(e);
 187         long oldElements = elems();
 188         System.setBit(bits, ((Enum<?>)e).ordinal());
 189         return elems() != oldElements;
 190     }
 191 
 192     /**
 193      * Removes the specified element from this set if it is present.
 194      *
 195      * @param e element to be removed from this set, if present
 196      * @return {@code true} if the set contained the specified element
 197      */
 198     public boolean remove(Object e) {
 199         if (e == null)
 200             return false;
 201         Class<?> eClass = e.getClass();
 202         if (eClass != elementType && eClass.getSuperclass() != elementType)
 203             return false;
 204         long oldElements = elems();
 205         System.clrBit(bits, ((Enum<?>)e).ordinal());
 206         return elems() != oldElements;
 207     }
 208 
 209     // Bulk Operations
 210 
 211     /**
 212      * Returns {@code true} if this set contains all of the elements
 213      * in the specified collection.
 214      *
 215      * @param c collection to be checked for containment in this set
 216      * @return {@code true} if this set contains all of the elements
 217      *        in the specified collection
 218      * @throws NullPointerException if the specified collection is null
 219      */
 220     public boolean containsAll(Collection<?> c) {
 221         if (!(c instanceof RegularEnumSet))
 222             return super.containsAll(c);
 223 
 224         RegularEnumSet<?> es = (RegularEnumSet<?>)c;
 225         if (es.elementType != elementType)
 226             return es.isEmpty();
 227 
 228         return (es.elems() & ~elems()) == 0;
 229     }
 230 
 231     /**
 232      * Adds all of the elements in the specified collection to this set.
 233      *
 234      * @param c collection whose elements are to be added to this set
 235      * @return {@code true} if this set changed as a result of the call
 236      * @throws NullPointerException if the specified collection or any
 237      *     of its elements are null
 238      */
 239     public boolean addAll(Collection<? extends E> c) {
 240         if (!(c instanceof RegularEnumSet))
 241             return super.addAll(c);
 242 
 243         RegularEnumSet<?> es = (RegularEnumSet<?>)c;
 244         if (es.elementType != elementType) {
 245             if (es.isEmpty())
 246                 return false;
 247             else
 248                 throw new ClassCastException(
 249                     es.elementType + " != " + elementType);
 250         }
 251         long oldElements = elems();
 252         setBits(elems() | es.elems());
 253         return elems() != oldElements;
 254     }
 255 
 256     /**
 257      * Removes from this set all of its elements that are contained in
 258      * the specified collection.
 259      *
 260      * @param c elements to be removed from this set
 261      * @return {@code true} if this set changed as a result of the call
 262      * @throws NullPointerException if the specified collection is null
 263      */
 264     public boolean removeAll(Collection<?> c) {
 265         if (!(c instanceof RegularEnumSet))
 266             return super.removeAll(c);
 267 
 268         RegularEnumSet<?> es = (RegularEnumSet<?>)c;
 269         if (es.elementType != elementType)
 270             return false;
 271         long oldElements = elems();
 272         setBits(elems() & ~es.elems());
 273         return elems() != oldElements;
 274     }
 275 
 276     /**
 277      * Retains only the elements in this set that are contained in the
 278      * specified collection.
 279      *
 280      * @param c elements to be retained in this set
 281      * @return {@code true} if this set changed as a result of the call
 282      * @throws NullPointerException if the specified collection is null
 283      */
 284     public boolean retainAll(Collection<?> c) {
 285         if (!(c instanceof RegularEnumSet))
 286             return super.retainAll(c);
 287 
 288         RegularEnumSet<?> es = (RegularEnumSet<?>)c;
 289         if (es.elementType != elementType) {
 290             boolean changed = (elems() != 0);
 291             setBits(0);
 292             return changed;
 293         }
 294         long oldElements = elems();
 295         setBits(elems() & es.elems());
 296         return elems() != oldElements;
 297     }
 298 
 299     /**
 300      * Removes all of the elements from this set.
 301      */
 302     public void clear() {
 303         setBits(0);
 304     }
 305 
 306     /**
 307      * Compares the specified object with this set for equality.  Returns
 308      * {@code true} if the given object is also a set, the two sets have
 309      * the same size, and every member of the given set is contained in
 310      * this set.
 311      *
 312      * @param o object to be compared for equality with this set
 313      * @return {@code true} if the specified object is equal to this set
 314      */
 315     public boolean equals(Object o) {
 316         if (!(o instanceof RegularEnumSet))
 317             return super.equals(o);
 318 
 319         RegularEnumSet<?> es = (RegularEnumSet<?>)o;
 320         if (es.elementType != elementType)
 321             return elems() == 0 && es.elems() == 0;
 322         return es.elems() == elems();
 323     }
 324 }