src/share/classes/java/util/EnumSet.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


 341      */
 342     public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
 343         if (from.compareTo(to) > 0)
 344             throw new IllegalArgumentException(from + " > " + to);
 345         EnumSet<E> result = noneOf(from.getDeclaringClass());
 346         result.addRange(from, to);
 347         return result;
 348     }
 349 
 350     /**
 351      * Adds the specified range to this enum set, which is empty prior
 352      * to the call.
 353      */
 354     abstract void addRange(E from, E to);
 355 
 356     /**
 357      * Returns a copy of this set.
 358      *
 359      * @return a copy of this set
 360      */

 361     public EnumSet<E> clone() {
 362         try {
 363             return (EnumSet<E>) super.clone();


 364         } catch(CloneNotSupportedException e) {
 365             throw new AssertionError(e);
 366         }
 367     }
 368 
 369     /**
 370      * Complements the contents of this enum set.
 371      */
 372     abstract void complement();
 373 
 374     /**
 375      * Throws an exception if e is not of the correct type for this enum set.
 376      */
 377     final void typeCheck(E e) {
 378         Class eClass = e.getClass();
 379         if (eClass != elementType && eClass.getSuperclass() != elementType)
 380             throw new ClassCastException(eClass + " != " + elementType);
 381     }
 382 
 383     /**
 384      * Returns all of the values comprising E.
 385      * The result is uncloned, cached, and shared by all callers.
 386      */
 387     private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
 388         return SharedSecrets.getJavaLangAccess()
 389                                         .getEnumConstantsShared(elementType);
 390     }
 391 
 392     /**
 393      * This class is used to serialize all EnumSet instances, regardless of
 394      * implementation type.  It captures their "logical contents" and they
 395      * are reconstructed using public static factories.  This is necessary
 396      * to ensure that the existence of a particular implementation type is
 397      * an implementation detail.
 398      *


   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


 341      */
 342     public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
 343         if (from.compareTo(to) > 0)
 344             throw new IllegalArgumentException(from + " > " + to);
 345         EnumSet<E> result = noneOf(from.getDeclaringClass());
 346         result.addRange(from, to);
 347         return result;
 348     }
 349 
 350     /**
 351      * Adds the specified range to this enum set, which is empty prior
 352      * to the call.
 353      */
 354     abstract void addRange(E from, E to);
 355 
 356     /**
 357      * Returns a copy of this set.
 358      *
 359      * @return a copy of this set
 360      */
 361     @Override
 362     public EnumSet<E> clone() {
 363         try {
 364             @SuppressWarnings("unchecked")
 365             EnumSet<E> clone = (EnumSet<E>) super.clone();
 366             return clone;
 367         } catch(CloneNotSupportedException e) {
 368             throw new AssertionError(e);
 369         }
 370     }
 371 
 372     /**
 373      * Complements the contents of this enum set.
 374      */
 375     abstract void complement();
 376 
 377     /**
 378      * Throws an exception if e is not of the correct type for this enum set.
 379      */
 380     final void typeCheck(E e) {
 381         Class<?> eClass = e.getClass();
 382         if (eClass != elementType && eClass.getSuperclass() != elementType)
 383             throw new ClassCastException(eClass + " != " + elementType);
 384     }
 385 
 386     /**
 387      * Returns all of the values comprising E.
 388      * The result is uncloned, cached, and shared by all callers.
 389      */
 390     private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
 391         return SharedSecrets.getJavaLangAccess()
 392                                         .getEnumConstantsShared(elementType);
 393     }
 394 
 395     /**
 396      * This class is used to serialize all EnumSet instances, regardless of
 397      * implementation type.  It captures their "logical contents" and they
 398      * are reconstructed using public static factories.  This is necessary
 399      * to ensure that the existence of a particular implementation type is
 400      * an implementation detail.
 401      *