60 * unsynchronized access:
61 *
62 * <pre>
63 * Set<MyEnum> 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}/../technotes/guides/collections/index.html">
73 * Java Collections Framework</a>.
74 *
75 * @author Josh Bloch
76 * @since 1.5
77 * @see EnumMap
78 * @serial exclude
79 */
80 @SuppressWarnings("serial") // No serialVersionUID due to usage of
81 // serial proxy pattern
82 public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
83 implements Cloneable, java.io.Serializable
84 {
85 /**
86 * The class of all the elements of this set.
87 */
88 final Class<E> elementType;
89
90 /**
91 * All of the values comprising T. (Cached for performance.)
92 */
93 final Enum<?>[] universe;
94
95 private static Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
96
97 EnumSet(Class<E>elementType, Enum<?>[] universe) {
98 this.elementType = elementType;
99 this.universe = universe;
100 }
101
102 /**
103 * Creates an empty enum set with the specified element type.
104 *
105 * @param <E> The class of the elements in the set
106 * @param elementType the class object of the element type for this enum
107 * set
108 * @return An empty enum set of the specified type.
109 * @throws NullPointerException if {@code elementType} is null
110 */
111 public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
112 Enum<?>[] universe = getUniverse(elementType);
113 if (universe == null)
114 throw new ClassCastException(elementType + " not an enum");
115
116 if (universe.length <= 64)
404 * Returns all of the values comprising E.
405 * The result is uncloned, cached, and shared by all callers.
406 */
407 private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
408 return SharedSecrets.getJavaLangAccess()
409 .getEnumConstantsShared(elementType);
410 }
411
412 /**
413 * This class is used to serialize all EnumSet instances, regardless of
414 * implementation type. It captures their "logical contents" and they
415 * are reconstructed using public static factories. This is necessary
416 * to ensure that the existence of a particular implementation type is
417 * an implementation detail.
418 *
419 * @serial include
420 */
421 private static class SerializationProxy <E extends Enum<E>>
422 implements java.io.Serializable
423 {
424 /**
425 * The element type of this enum set.
426 *
427 * @serial
428 */
429 private final Class<E> elementType;
430
431 /**
432 * The elements contained in this enum set.
433 *
434 * @serial
435 */
436 private final Enum<?>[] elements;
437
438 SerializationProxy(EnumSet<E> set) {
439 elementType = set.elementType;
440 elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
441 }
442
443 // instead of cast to E, we should perhaps use elementType.cast()
444 // to avoid injection of forged stream, but it will slow the implementation
445 @SuppressWarnings("unchecked")
446 private Object readResolve() {
447 EnumSet<E> result = EnumSet.noneOf(elementType);
448 for (Enum<?> e : elements)
449 result.add((E)e);
450 return result;
451 }
452
453 private static final long serialVersionUID = 362491234563181265L;
454 }
455
456 Object writeReplace() {
457 return new SerializationProxy<>(this);
458 }
459
460 // readObject method for the serialization proxy pattern
461 // See Effective Java, Second Ed., Item 78.
462 private void readObject(java.io.ObjectInputStream stream)
463 throws java.io.InvalidObjectException {
464 throw new java.io.InvalidObjectException("Proxy required");
465 }
466 }
|
60 * unsynchronized access:
61 *
62 * <pre>
63 * Set<MyEnum> 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}/../technotes/guides/collections/index.html">
73 * Java Collections Framework</a>.
74 *
75 * @author Josh Bloch
76 * @since 1.5
77 * @see EnumMap
78 * @serial exclude
79 */
80 public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
81 implements Cloneable, java.io.Serializable
82 {
83 /**
84 * The class of all the elements of this set.
85 */
86 final Class<E> elementType;
87
88 /**
89 * All of the values comprising T. (Cached for performance.)
90 */
91 final Enum<?>[] universe;
92
93 EnumSet(Class<E>elementType, Enum<?>[] universe) {
94 this.elementType = elementType;
95 this.universe = universe;
96 }
97
98 /**
99 * Creates an empty enum set with the specified element type.
100 *
101 * @param <E> The class of the elements in the set
102 * @param elementType the class object of the element type for this enum
103 * set
104 * @return An empty enum set of the specified type.
105 * @throws NullPointerException if {@code elementType} is null
106 */
107 public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
108 Enum<?>[] universe = getUniverse(elementType);
109 if (universe == null)
110 throw new ClassCastException(elementType + " not an enum");
111
112 if (universe.length <= 64)
400 * Returns all of the values comprising E.
401 * The result is uncloned, cached, and shared by all callers.
402 */
403 private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {
404 return SharedSecrets.getJavaLangAccess()
405 .getEnumConstantsShared(elementType);
406 }
407
408 /**
409 * This class is used to serialize all EnumSet instances, regardless of
410 * implementation type. It captures their "logical contents" and they
411 * are reconstructed using public static factories. This is necessary
412 * to ensure that the existence of a particular implementation type is
413 * an implementation detail.
414 *
415 * @serial include
416 */
417 private static class SerializationProxy <E extends Enum<E>>
418 implements java.io.Serializable
419 {
420
421 private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
422
423 /**
424 * The element type of this enum set.
425 *
426 * @serial
427 */
428 private final Class<E> elementType;
429
430 /**
431 * The elements contained in this enum set.
432 *
433 * @serial
434 */
435 private final Enum<?>[] elements;
436
437 SerializationProxy(EnumSet<E> set) {
438 elementType = set.elementType;
439 elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);
440 }
441
442 // instead of cast to E, we should perhaps use elementType.cast()
443 // to avoid injection of forged stream, but it will slow the implementation
444 @SuppressWarnings("unchecked")
445 private Object readResolve() {
446 EnumSet<E> result = EnumSet.noneOf(elementType);
447 for (Enum<?> e : elements)
448 result.add((E)e);
449 return result;
450 }
451
452 private static final long serialVersionUID = 362491234563181265L;
453 }
454
455 Object writeReplace() {
456 return new SerializationProxy<>(this);
457 }
458
459 private static final long serialVersionUID = 1009687484059888093L;
460
461 // readObject method for the serialization proxy pattern
462 // See Effective Java, Second Ed., Item 78.
463 private void readObject(java.io.ObjectInputStream stream)
464 throws java.io.InvalidObjectException {
465 throw new java.io.InvalidObjectException("Proxy required");
466 }
467 }
|