//An enumeration-based Set, which is more efficient than HashSet most of the time. //This class is an abstract class whose implementation classes are RegularEnumSet and JumboEnumSet. //Create an enumeration that initially contains the specified elements (the methods with of are basically the same) public static <E extends Enum<E>> EnumSet<E> of(E e) { EnumSet<E> result = noneOf(e.getDeclaringClass()); result.add(e); return result; } public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) { //Get enumeration according to elementType Enum[] universe = getUniverse(elementType); if (universe == null) throw new ClassCastException(elementType + " not an enum"); // Call different implementations depending on the length if (universe.length <= 64) return new RegularEnumSet<>(elementType, universe); else return new JumboEnumSet<>(elementType, universe); } // Contains all enumerations of the specified element type public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) { EnumSet<E> result = noneOf(elementType); result.addAll(); return result; } //Constructs an EnumSet that is the same as the specified enumeration. public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) { return s.clone(); } //An EnumSet initialized from the specified collection 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; } } //Include all elements not included in the specified set public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) { EnumSet<E> result = copyOf(s); result.complement(); return result; }
Read EnumSet source code
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326468500&siteId=291194637
Recommended
Ranking