< prev index next >

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

Print this page




 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 <tt>true</tt> if this set contains no elements.
 127      *
 128      * @return <tt>true</tt> if this set contains no elements
 129      */
 130     public boolean isEmpty() {
 131         return elements == 0;
 132     }
 133 
 134     /**
 135      * Returns <tt>true</tt> if this set contains the specified element.
 136      *
 137      * @param e element to be checked for containment in this collection
 138      * @return <tt>true</tt> 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 <tt>true</tt> if the set changed as a result of the call
 157      *
 158      * @throws NullPointerException if <tt>e</tt> 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 <tt>true</tt> 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 <tt>true</tt> 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 <tt>true</tt> 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 <tt>true</tt> 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 <tt>true</tt> 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 <tt>true</tt> 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      * <tt>true</tt> 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 <tt>true</tt> 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 }


 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 }
< prev index next >