Read the source code of the EnumSet abstract class and the abstract source code of enumset.

Source: Internet
Author: User
Tags addall

Read the source code of the EnumSet abstract class and the abstract source code of enumset.

EnumSet

EnumSet is a generic Java Enumeration type container. Since Java has SortedSet, TreeSet, HashSet, and other containers, why should we have another EnumSet <T>? The answer is that EnumSet has certain characteristics. For example, EnumSet is fast. Other features are not listed in detail. After all, this article does not introduce the features of EnumSet.

A collection class specially designed for enumeration classes. All elements must be of the enumeration type.

The collection elements of the EnumSet are ordered and stored internally as bitvectors. Therefore, the memory usage is small and the efficiency is high.

Null elements cannot be added.

Source code

Package java. util; import sun. misc. sharedSecrets; public abstract class EnumSet <E extends Enum <E> extends actset <E> implements Cloneable, java. io. serializable {/*** element type */final Class <E> elementType;/*** store elements through arrays */final Enum [] universe; private static Enum [] ZERO_LENGTH_ENUM_ARRAY = new Enum [0]; EnumSet (Class <E> elementType, Enum [] universe) {this. elementType = elementType; this. universe = Universe ;} /*** create an empty enum set and specify its element type * @ param elementType the class object of the element type for this enum * set * @ throws NullPointerException if <tt> elementType </tt> is null */public static <E extends Enum <E> EnumSet <E> noneOf (Class <E> elementType) {Enum [] universe = getUniverse (elementType); if (universe = null) throw new ClassCastException (elementType + "not an enum"); if (unive Rse. length <= 64) return new RegularEnumSet <> (elementType, universe); else return new JumboEnumSet <> (elementType, universe );} /*** create an enumeration set containing all elements of the specified element type ** @ param elementType the class object of the element type for this enum * set * @ throws NullPointerException if <tt> elementType </tt> is null */public static <E extends Enum <E> EnumSet <E> allOf (Class <E> elementType) {EnumSet <E> result = n OneOf (elementType); result. addAll (); return result;}/*** Adds all of the elements from the appropriate enum type to this enum * set, which is empty prior to the call. */abstract void addAll (); /*** create an enumeration with the same element type as the specified enumeration set ** @ param s the enum set from which to initialize this enum set * @ throws NullPointerException if <tt> s </tt> is null */public static <E extends Enum <E> EnumSet <E> copyOf (EnumSet <E> s) {return s. clone ();}/*** create an enumeration set to initialize from the specified set, initially contains the same element * @ param c the collection from which to initialize this enum set * @ throws IllegalArgumentException if <tt> c </tt> is not an * <tt> EnumSet </tt> instance and contains no elements * @ throws NullPointerException if <tt> c </tt> is null */public static <E extends Enum <E> EnumSet <E> copyOf (Collection <E> c) {if (c instanceof EnumSet ){ Return (EnumSet <E>) c ). clone ();} else {if (c. isEmpty () throw new IllegalArgumentException ("Collection is empty"); Iterator <E> I = c. iterator (); E first = I. next (); EnumSet <E> result = EnumSet. of (first); while (I. hasNext () result. add (I. next (); return result ;}}/*** creates an enumeration set, the element is the same as that of s * @ param s the enum set from whose complement to initialize this enum set * @ throws NullPointerException if <Tt> s </tt> is null */public static <E extends Enum <E> EnumSet <E> complementOf (EnumSet <E> s) {EnumSet <E> result = copyOf (s); result. complement (); return result ;} /*** one element enumeration set ** @ param e the element that this set is to contain initially * @ throws NullPointerException if <tt> e </tt> is null *@ return an enum set initially containing the specified element */public static <E extends Enum <E> E NumSet <E> of (E e) {EnumSet <E> result = noneOf (e. getDeclaringClass (); result. add (e); return result ;} /*** TWO element enumeration sets ** @ param e1 an element that this set is to contain initially * @ param e2 another element that this set is to contain initially * @ throws NullPointerException if any parameters are null * @ return an enum set initially containing the specified elements */public static <E extends Enum <E> EnumSet <E> of (E e1, E e2) {EnumSet <E> result = noneOf (e1.getDeclaringClass (); result. add (e1); result. add (e2); return result ;} /*** three element enumeration sets ** @ param e1 an element that this set is to contain initially * @ param e2 another element that this set is to contain initially * @ param e3 another element that this set is to contain initially * @ throws NullPointerException if any parameters ar E null * @ return an enum set initially containing the specified elements */public static <E extends Enum <E> EnumSet <E> of (E e1, E e2, E e3) {EnumSet <E> result = noneOf (e1.getDeclaringClass (); result. add (e1); result. add (e2); result. add (e3); return result ;} /*** four element enumeration sets * @ param e1 an element that this set is to contain initially * @ param e2 another element that this set is to contain initia Lly * @ param e3 another element that this set is to contain initially * @ param e4 another element that this set is to contain initially * @ throws NullPointerException if any parameters are null * @ return enum set initially containing the specified elements */public static <E extends Enum <E> EnumSet <E> of (E e1, E e2, E e3, E e4) {EnumSet <E> result = noneOf (e1.getDeclaringClass (); result. add (E1); result. add (e2); result. add (e3); result. add (e4); return result ;} /*** five element enumeration sets ** @ param e1 an element that this set is to contain initially * @ param e2 another element that this set is to contain initially * @ param e3 another element that this set is to contain initially * @ param e4 another element that this set is to contain initially * @ param e5 another element that this set is to conta In initially * @ throws NullPointerException if any parameters are null * @ return an enum set initially containing the specified elements */public static <E extends Enum <E> EnumSet <E> (E e1, E e2, E e3, E e4, E e5) {EnumSet <E> result = noneOf (e1.getDeclaringClass (); result. add (e1); result. add (e2); result. add (e3); result. add (e4); result. add (e5); return result;}/*** n element enumeration sets ** @ param firs T an element that the set is to contain initially * @ param rest the remaining elements the set is to contain initially * @ throws NullPointerException if any of the specified elements are null, * or if <tt> rest </tt> is null * @ return an enum set initially containing the specified elements */@ SafeVarargs public static <E extends Enum <E> EnumSet <e> of (E first, e... rest) {EnumSet <E> result = noneO F (first. getDeclaringClass (); result. add (first); for (E e: rest) result. add (e); return result ;} /*** enumeration set of elements in the interval ** @ param from the first element in the range * @ param to the last element in the range * @ throws NullPointerException if {@ code from} or {@ code to} are null * @ throws IllegalArgumentException if {@ code from. compareTo (to)> 0} * @ return an enum set initially containing all of the el Ements in the * range defined by the two specified endpoints */public static <E extends Enum <E> EnumSet <E> range (E from, E to) {if (from. compareTo (to)> 0) throw new IllegalArgumentException (from + ">" + to); EnumSet <E> result = noneOf (from. getDeclaringClass (); result. addRange (from, to); return result;}/*** Adds the specified range to this enum set, which is empty prior * to the call. */ Abstract void addRange (E from, E to);/*** Returns a copy of this set. ** @ return a copy of this set */public EnumSet <E> clone () {try {return (EnumSet <E>) super. clone ();} catch (CloneNotSupportedException e) {throw new AssertionError (e) ;}/ *** Complements the contents of this enum set. */abstract void complement ();/*** Throws an exception if e is not of the correct type for this enum Set. */final void typeCheck (E e) {Class eClass = e. getClass (); if (eClass! = ElementType & eClass. getSuperclass ()! = ElementType) throw new ClassCastException (eClass + "! = "+ ElementType);}/*** Returns all of the values comprising E. * The result is uncloned, cached, and shared by all callers. */private static <E extends Enum <E> E [] getUniverse (Class <E> elementType) {return SharedSecrets. getJavaLangAccess (). getEnumConstantsShared (elementType);}/*** This class is used to serialize all EnumSet instances, regardless of * implementation type. it captures their "logical contents" and they * are reconstructed using public static factories. this is necessary * to ensure that the existence of a particle implementation type is * an implementation detail. ** @ serial include */private static class SerializationProxy <E extends Enum <E> implements java. io. serializable {/*** The element type of this enum set. ** @ serial */private final Class <E> elementType;/*** The elements contained in this enum set. ** @ serial */private final Enum [] elements; SerializationProxy (EnumSet <E> set) {elementType = set. elementType; elements = set. toArray (ZERO_LENGTH_ENUM_ARRAY);} private Object readResolve () {EnumSet <E> result = EnumSet. noneOf (elementType); for (Enum e: elements) result. add (E) e); return result;} private static final long serialVersionUID = 362491234563181265L;} Object writeReplace () {return new SerializationProxy <> (this );} // readObject method for the serialization proxy pattern // See valid Java, Second Ed ., item 78. private void readObject (java. io. objectInputStream stream) throws java. io. invalidObjectException {throw new java. io. invalidObjectException ("Proxy required ");}}

Summary

The above is all about the source code of the EnumSet abstract class. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.