< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2003, 2012, 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 long elements = 0L;


























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