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

Print this page


   1 /*
   2  * Copyright (c) 2003, 2011, 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


 122     }
 123 
 124     /**
 125      * Returns <tt>true</tt> if this set contains no elements.
 126      *
 127      * @return <tt>true</tt> if this set contains no elements
 128      */
 129     public boolean isEmpty() {
 130         return elements == 0;
 131     }
 132 
 133     /**
 134      * Returns <tt>true</tt> if this set contains the specified element.
 135      *
 136      * @param e element to be checked for containment in this collection
 137      * @return <tt>true</tt> if this set contains the specified element
 138      */
 139     public boolean contains(Object e) {
 140         if (e == null)
 141             return false;
 142         Class eClass = e.getClass();
 143         if (eClass != elementType && eClass.getSuperclass() != elementType)
 144             return false;
 145 
 146         return (elements & (1L << ((Enum)e).ordinal())) != 0;
 147     }
 148 
 149     // Modification Operations
 150 
 151     /**
 152      * Adds the specified element to this set if it is not already present.
 153      *
 154      * @param e element to be added to this set
 155      * @return <tt>true</tt> if the set changed as a result of the call
 156      *
 157      * @throws NullPointerException if <tt>e</tt> is null
 158      */
 159     public boolean add(E e) {
 160         typeCheck(e);
 161 
 162         long oldElements = elements;
 163         elements |= (1L << ((Enum)e).ordinal());
 164         return elements != oldElements;
 165     }
 166 
 167     /**
 168      * Removes the specified element from this set if it is present.
 169      *
 170      * @param e element to be removed from this set, if present
 171      * @return <tt>true</tt> if the set contained the specified element
 172      */
 173     public boolean remove(Object e) {
 174         if (e == null)
 175             return false;
 176         Class eClass = e.getClass();
 177         if (eClass != elementType && eClass.getSuperclass() != elementType)
 178             return false;
 179 
 180         long oldElements = elements;
 181         elements &= ~(1L << ((Enum)e).ordinal());
 182         return elements != oldElements;
 183     }
 184 
 185     // Bulk Operations
 186 
 187     /**
 188      * Returns <tt>true</tt> if this set contains all of the elements
 189      * in the specified collection.
 190      *
 191      * @param c collection to be checked for containment in this set
 192      * @return <tt>true</tt> if this set contains all of the elements
 193      *        in the specified collection
 194      * @throws NullPointerException if the specified collection is null
 195      */
 196     public boolean containsAll(Collection<?> c) {


   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


 122     }
 123 
 124     /**
 125      * Returns <tt>true</tt> if this set contains no elements.
 126      *
 127      * @return <tt>true</tt> if this set contains no elements
 128      */
 129     public boolean isEmpty() {
 130         return elements == 0;
 131     }
 132 
 133     /**
 134      * Returns <tt>true</tt> if this set contains the specified element.
 135      *
 136      * @param e element to be checked for containment in this collection
 137      * @return <tt>true</tt> if this set contains the specified element
 138      */
 139     public boolean contains(Object e) {
 140         if (e == null)
 141             return false;
 142         Class<?> eClass = e.getClass();
 143         if (eClass != elementType && eClass.getSuperclass() != elementType)
 144             return false;
 145 
 146         return (elements & (1L << ((Enum)e).ordinal())) != 0;
 147     }
 148 
 149     // Modification Operations
 150 
 151     /**
 152      * Adds the specified element to this set if it is not already present.
 153      *
 154      * @param e element to be added to this set
 155      * @return <tt>true</tt> if the set changed as a result of the call
 156      *
 157      * @throws NullPointerException if <tt>e</tt> is null
 158      */
 159     public boolean add(E e) {
 160         typeCheck(e);
 161 
 162         long oldElements = elements;
 163         elements |= (1L << ((Enum)e).ordinal());
 164         return elements != oldElements;
 165     }
 166 
 167     /**
 168      * Removes the specified element from this set if it is present.
 169      *
 170      * @param e element to be removed from this set, if present
 171      * @return <tt>true</tt> if the set contained the specified element
 172      */
 173     public boolean remove(Object e) {
 174         if (e == null)
 175             return false;
 176         Class<?> eClass = e.getClass();
 177         if (eClass != elementType && eClass.getSuperclass() != elementType)
 178             return false;
 179 
 180         long oldElements = elements;
 181         elements &= ~(1L << ((Enum)e).ordinal());
 182         return elements != oldElements;
 183     }
 184 
 185     // Bulk Operations
 186 
 187     /**
 188      * Returns <tt>true</tt> if this set contains all of the elements
 189      * in the specified collection.
 190      *
 191      * @param c collection to be checked for containment in this set
 192      * @return <tt>true</tt> if this set contains all of the elements
 193      *        in the specified collection
 194      * @throws NullPointerException if the specified collection is null
 195      */
 196     public boolean containsAll(Collection<?> c) {