< prev index next >

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

Print this page




  63  * Set&lt;MyEnum&gt; s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));
  64  * </pre>
  65  *
  66  * <p>Implementation note: All basic operations execute in constant time.
  67  * They are likely (though not guaranteed) to be much faster than their
  68  * {@link HashSet} counterparts.  Even bulk operations execute in
  69  * constant time if their argument is also an enum set.
  70  *
  71  * <p>This class is a member of the
  72  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  73  * Java Collections Framework</a>.
  74  *
  75  * @author Josh Bloch
  76  * @since 1.5
  77  * @see EnumMap
  78  */
  79 public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
  80     implements Cloneable, java.io.Serializable
  81 {
  82     // declare EnumSet.class serialization compatibility with JDK 8

  83     private static final long serialVersionUID = 1009687484059888093L;
  84 
  85     /**
  86      * The class of all the elements of this set.
  87      */
  88     final transient Class<E> elementType;
  89 
  90     /**
  91      * All of the values comprising E.  (Cached for performance.)
  92      */
  93     final transient Enum<?>[] universe;
  94 
  95     EnumSet(Class<E>elementType, Enum<?>[] universe) {
  96         this.elementType = elementType;
  97         this.universe    = universe;
  98     }
  99 
 100     /**
 101      * Creates an empty enum set with the specified element type.
 102      *


 432         /**
 433          * The elements contained in this enum set.
 434          *
 435          * @serial
 436          */
 437         private final Enum<?>[] elements;
 438 
 439         SerializationProxy(EnumSet<E> set) {
 440             elementType = set.elementType;
 441             elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
 442         }
 443 
 444         /**
 445          * Returns an {@code EnumSet} object with initial state
 446          * held by this proxy.
 447          *
 448          * @return a {@code EnumSet} object with initial state
 449          * held by this proxy
 450          */
 451         @SuppressWarnings("unchecked")

 452         private Object readResolve() {
 453             // instead of cast to E, we should perhaps use elementType.cast()
 454             // to avoid injection of forged stream, but it will slow the
 455             // implementation
 456             EnumSet<E> result = EnumSet.noneOf(elementType);
 457             for (Enum<?> e : elements)
 458                 result.add((E)e);
 459             return result;
 460         }
 461 

 462         private static final long serialVersionUID = 362491234563181265L;
 463     }
 464 
 465     /**
 466      * Returns a
 467      * <a href="{@docRoot}/serialized-form.html#java.util.EnumSet.SerializationProxy">
 468      * SerializationProxy</a>
 469      * representing the state of this instance.
 470      *
 471      * @return a {@link SerializationProxy}
 472      * representing the state of this instance
 473      */

 474     Object writeReplace() {
 475         return new SerializationProxy<>(this);
 476     }
 477 
 478     /**
 479      * @param s the stream
 480      * @throws java.io.InvalidObjectException always
 481      */

 482     private void readObject(java.io.ObjectInputStream s)
 483         throws java.io.InvalidObjectException {
 484         throw new java.io.InvalidObjectException("Proxy required");
 485     }
 486 }


  63  * Set&lt;MyEnum&gt; s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));
  64  * </pre>
  65  *
  66  * <p>Implementation note: All basic operations execute in constant time.
  67  * They are likely (though not guaranteed) to be much faster than their
  68  * {@link HashSet} counterparts.  Even bulk operations execute in
  69  * constant time if their argument is also an enum set.
  70  *
  71  * <p>This class is a member of the
  72  * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
  73  * Java Collections Framework</a>.
  74  *
  75  * @author Josh Bloch
  76  * @since 1.5
  77  * @see EnumMap
  78  */
  79 public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
  80     implements Cloneable, java.io.Serializable
  81 {
  82     // declare EnumSet.class serialization compatibility with JDK 8
  83     @java.io.Serial
  84     private static final long serialVersionUID = 1009687484059888093L;
  85 
  86     /**
  87      * The class of all the elements of this set.
  88      */
  89     final transient Class<E> elementType;
  90 
  91     /**
  92      * All of the values comprising E.  (Cached for performance.)
  93      */
  94     final transient Enum<?>[] universe;
  95 
  96     EnumSet(Class<E>elementType, Enum<?>[] universe) {
  97         this.elementType = elementType;
  98         this.universe    = universe;
  99     }
 100 
 101     /**
 102      * Creates an empty enum set with the specified element type.
 103      *


 433         /**
 434          * The elements contained in this enum set.
 435          *
 436          * @serial
 437          */
 438         private final Enum<?>[] elements;
 439 
 440         SerializationProxy(EnumSet<E> set) {
 441             elementType = set.elementType;
 442             elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
 443         }
 444 
 445         /**
 446          * Returns an {@code EnumSet} object with initial state
 447          * held by this proxy.
 448          *
 449          * @return a {@code EnumSet} object with initial state
 450          * held by this proxy
 451          */
 452         @SuppressWarnings("unchecked")
 453         @java.io.Serial
 454         private Object readResolve() {
 455             // instead of cast to E, we should perhaps use elementType.cast()
 456             // to avoid injection of forged stream, but it will slow the
 457             // implementation
 458             EnumSet<E> result = EnumSet.noneOf(elementType);
 459             for (Enum<?> e : elements)
 460                 result.add((E)e);
 461             return result;
 462         }
 463 
 464         @java.io.Serial
 465         private static final long serialVersionUID = 362491234563181265L;
 466     }
 467 
 468     /**
 469      * Returns a
 470      * <a href="{@docRoot}/serialized-form.html#java.util.EnumSet.SerializationProxy">
 471      * SerializationProxy</a>
 472      * representing the state of this instance.
 473      *
 474      * @return a {@link SerializationProxy}
 475      * representing the state of this instance
 476      */
 477     @java.io.Serial
 478     Object writeReplace() {
 479         return new SerializationProxy<>(this);
 480     }
 481 
 482     /**
 483      * @param s the stream
 484      * @throws java.io.InvalidObjectException always
 485      */
 486     @java.io.Serial
 487     private void readObject(java.io.ObjectInputStream s)
 488         throws java.io.InvalidObjectException {
 489         throw new java.io.InvalidObjectException("Proxy required");
 490     }
 491 }
< prev index next >