1. /*
  2. * @(#)EnumSet.java 1.10 04/05/28
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. /**
  9. * A specialized {@link Set} implementation for use with enum types. All of
  10. * the elements in an enum set must come from a single enum type that is
  11. * specified, explicitly or implicitly, when the set is created. Enum sets
  12. * are represented internally as bit vectors. This representation is
  13. * extremely compact and efficient. The space and time performance of this
  14. * class should be good enough to allow its use as a high-quality, typesafe
  15. * alternative to traditional <tt>int</tt>-based "bit flags." Even bulk
  16. * operations (such as <tt>containsAll</tt> and <tt>retainAll</tt>) should
  17. * run very quickly if the specified collection is also an enum set.
  18. *
  19. * <p>The iterator returned by the <tt>iterator</tt>method traverses the
  20. * elements in their <i>natural order</i> (the order in which the enum
  21. * constants are declared). The returned iterator is <i>weakly
  22. * consistent</i>: it will never throw {@link ConcurrentModificationException}
  23. * and it may or may not show the effects of any modifications to the set that
  24. * occur while the iteration is in progress.
  25. *
  26. * <p>Null elements are not permitted. Attempts to insert a null element
  27. * will throw {@link NullPointerException}. Attempts to test for the
  28. * presence of a null element or to remove one will, however, function
  29. * properly.
  30. *
  31. * <P>Like most collection implementations <tt>EnumSet</tt> is not
  32. * synchronized. If multiple threads access an enum set concurrently, and at
  33. * least one of the threads modifies the set, it should be synchronized
  34. * externally. This is typically accomplished by synchronizing on some
  35. * object that naturally encapsulates the enum set. If no such object exists,
  36. * the set should be "wrapped" using the {@link Collections#synchronizedSet}
  37. * method. This is best done at creation time, to prevent accidental
  38. * unsynchronized access:
  39. *
  40. * <pre>
  41. * Set<MyEnum> s = Collections.synchronizedSet(EnumSet.noneOf(Foo.class));
  42. * </pre>
  43. *
  44. * <p>Implementation note: All basic operations execute in constant time.
  45. * They are likely (though not guaranteed) to be much faster than their
  46. * {@link HashSet} counterparts. Even bulk operations, such as {@link
  47. * #addAll} and {@link #removeAll} execute in constant time if the
  48. * parameter is another <tt>EnumSet</tt> instance.
  49. *
  50. * <p>This class is a member of the
  51. * <a href="{@docRoot}/../guide/collections/index.html">
  52. * Java Collections Framework</a>.
  53. *
  54. * @author Josh Bloch
  55. * @version 1.10, 05/28/04
  56. * @since 1.5
  57. * @see EnumMap
  58. * @serial exclude
  59. */
  60. public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
  61. implements Cloneable, java.io.Serializable
  62. {
  63. /**
  64. * The class of all the elements of this set.
  65. */
  66. final Class<E> elementType;
  67. /**
  68. * All of the values comprising T. (Cached for performance.)
  69. */
  70. final Enum[] universe;
  71. private static Enum[] ZERO_LENGTH_ENUM_ARRAY = new Enum[0];
  72. EnumSet(Class<E>elementType, Enum[] universe) {
  73. this.elementType = elementType;
  74. this.universe = universe;
  75. }
  76. /**
  77. * Creates an empty enum set with the specified element type.
  78. *
  79. * @param elementType the class object of the element type for this enum
  80. * set
  81. * @throws NullPointerException if <tt>elementType</tt> is null
  82. */
  83. public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
  84. Enum[] universe = elementType.getEnumConstants();
  85. if (universe == null)
  86. throw new ClassCastException(elementType + " not an enum");
  87. if (universe.length <= 64)
  88. return new RegularEnumSet<E>(elementType, universe);
  89. else
  90. return new JumboEnumSet<E>(elementType, universe);
  91. }
  92. /**
  93. * Creates an enum set containing all of the elements in the specified
  94. * element type.
  95. *
  96. * @param elementType the class object of the element type for this enum
  97. * set
  98. * @throws NullPointerException if <tt>elementType</tt> is null
  99. */
  100. public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
  101. EnumSet<E> result = noneOf(elementType);
  102. result.addAll();
  103. return result;
  104. }
  105. /**
  106. * Adds all of the elements from the appropriate enum type to this enum
  107. * set, which is empty prior to the call.
  108. */
  109. abstract void addAll();
  110. /**
  111. * Creates an enum set with the same element type as the specified enum
  112. * set, initially containing the same elements (if any).
  113. *
  114. * @param s the enum set from which to initialize this enum set
  115. * @throws NullPointerException if <tt>s</tt> is null
  116. */
  117. public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {
  118. return s.clone();
  119. }
  120. /**
  121. * Creates an enum set initialized from the specified collection. If
  122. * the specified collection is an <tt>EnumSet</tt> instance, this static
  123. * factory method behaves identically to {@link #copyOf(EnumSet)}.
  124. * Otherwise, the specified collection must contain at least one element
  125. * (in order to determine the new enum set's element type).
  126. *
  127. * @param c the collection from which to initialize this enum set
  128. * @throws IllegalArgumentException if <tt>c</tt> is not an
  129. * <tt>EnumSet</tt> instance and contains no elements
  130. * @throws NullPointerException if <tt>c</tt> is null
  131. */
  132. public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) {
  133. if (c instanceof EnumSet) {
  134. return ((EnumSet<E>)c).clone();
  135. } else {
  136. if (c.isEmpty())
  137. throw new IllegalArgumentException("Collection is empty");
  138. Iterator<E> i = c.iterator();
  139. E first = i.next();
  140. EnumSet<E> result = EnumSet.of(first);
  141. while (i.hasNext())
  142. result.add(i.next());
  143. return result;
  144. }
  145. }
  146. /**
  147. * Creates an enum set with the same element type as the specified enum
  148. * set, initially containing all the elements of this type that are
  149. * <i>not</i> contained in the specified set.
  150. *
  151. * @param s the enum set from whose complement to initialize this enum set
  152. * @throws NullPointerException if <tt>s</tt> is null
  153. */
  154. public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) {
  155. EnumSet<E> result = copyOf(s);
  156. result.complement();
  157. return result;
  158. }
  159. /**
  160. * Creates an enum set initially containing the specified element.
  161. *
  162. * Overloadings of this method exist to initialize an enum set with
  163. * one through five elements. A sixth overloading is provided that
  164. * uses the varargs feature. This overloading may be used to create an
  165. * an enum set initially containing an arbitrary number of elements, but
  166. * is likely to run slower than the overloadings that do not use varargs.
  167. *
  168. * @param e the element that this set is to contain initially
  169. * @throws NullPointerException if <tt>e</tt> is null
  170. * @return an enum set initially containing the specified element
  171. */
  172. public static <E extends Enum<E>> EnumSet<E> of(E e) {
  173. EnumSet<E> result = noneOf(e.getDeclaringClass());
  174. result.add(e);
  175. return result;
  176. }
  177. /**
  178. * Creates an enum set initially containing the specified elements.
  179. *
  180. * Overloadings of this method exist to initialize an enum set with
  181. * one through five elements. A sixth overloading is provided that
  182. * uses the varargs feature. This overloading may be used to create an
  183. * an enum set initially containing an arbitrary number of elements, but
  184. * is likely to run slower than the overloadings that do not use varargs.
  185. *
  186. * @param e1 an element that this set is to contain initially
  187. * @param e2 another element that this set is to contain initially
  188. * @throws NullPointerException if any parameters are null
  189. * @return an enum set initially containing the specified elements
  190. */
  191. public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {
  192. EnumSet<E> result = noneOf(e1.getDeclaringClass());
  193. result.add(e1);
  194. result.add(e2);
  195. return result;
  196. }
  197. /**
  198. * Creates an enum set initially containing the specified elements.
  199. *
  200. * Overloadings of this method exist to initialize an enum set with
  201. * one through five elements. A sixth overloading is provided that
  202. * uses the varargs feature. This overloading may be used to create an
  203. * an enum set initially containing an arbitrary number of elements, but
  204. * is likely to run slower than the overloadings that do not use varargs.
  205. *
  206. * @param e1 an element that this set is to contain initially
  207. * @param e2 another element that this set is to contain initially
  208. * @param e3 another element that this set is to contain initially
  209. * @throws NullPointerException if any parameters are null
  210. * @return an enum set initially containing the specified elements
  211. */
  212. public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {
  213. EnumSet<E> result = noneOf(e1.getDeclaringClass());
  214. result.add(e1);
  215. result.add(e2);
  216. result.add(e3);
  217. return result;
  218. }
  219. /**
  220. * Creates an enum set initially containing the specified elements.
  221. *
  222. * Overloadings of this method exist to initialize an enum set with
  223. * one through five elements. A sixth overloading is provided that
  224. * uses the varargs feature. This overloading may be used to create an
  225. * an enum set initially containing an arbitrary number of elements, but
  226. * is likely to run slower than the overloadings that do not use varargs.
  227. *
  228. * @param e1 an element that this set is to contain initially
  229. * @param e2 another element that this set is to contain initially
  230. * @param e3 another element that this set is to contain initially
  231. * @param e4 another element that this set is to contain initially
  232. * @throws NullPointerException if any parameters are null
  233. * @return an enum set initially containing the specified elements
  234. */
  235. public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {
  236. EnumSet<E> result = noneOf(e1.getDeclaringClass());
  237. result.add(e1);
  238. result.add(e2);
  239. result.add(e3);
  240. result.add(e4);
  241. return result;
  242. }
  243. /**
  244. * Creates an enum set initially containing the specified elements.
  245. *
  246. * Overloadings of this method exist to initialize an enum set with
  247. * one through five elements. A sixth overloading is provided that
  248. * uses the varargs feature. This overloading may be used to create an
  249. * an enum set initially containing an arbitrary number of elements, but
  250. * is likely to run slower than the overloadings that do not use varargs.
  251. *
  252. * @param e1 an element that this set is to contain initially
  253. * @param e2 another element that this set is to contain initially
  254. * @param e3 another element that this set is to contain initially
  255. * @param e4 another element that this set is to contain initially
  256. * @param e5 another element that this set is to contain initially
  257. * @throws NullPointerException if any parameters are null
  258. * @return an enum set initially containing the specified elements
  259. */
  260. public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4,
  261. E e5)
  262. {
  263. EnumSet<E> result = noneOf(e1.getDeclaringClass());
  264. result.add(e1);
  265. result.add(e2);
  266. result.add(e3);
  267. result.add(e4);
  268. result.add(e5);
  269. return result;
  270. }
  271. /**
  272. * Creates an enum set initially containing the specified elements.
  273. * This factory, whose parameter list uses the varargs feature, may
  274. * be used to create an enum set initially containing an arbitrary
  275. * number of elements, but it is likely to run slower than the overloadings
  276. * that do not use varargs.
  277. *
  278. * @param first an element that the set is to contain initially
  279. * @param rest the remaining elements the set is to contain initially
  280. * @throws NullPointerException if any of the specified elements are null,
  281. * or if <tt>rest</tt> is null
  282. * @return an enum set initially containing the specified elements
  283. */
  284. public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {
  285. EnumSet<E> result = noneOf(first.getDeclaringClass());
  286. result.add(first);
  287. for (E e : rest)
  288. result.add(e);
  289. return result;
  290. }
  291. /**
  292. * Creates an enum set initially containing all of the elements in the
  293. * range defined by the two specified endpoints. The returned set will
  294. * contain the endpoints themselves, which may be identical but must not
  295. * be out of order.
  296. *
  297. * @param from the first element in the range
  298. * @param to the last element in the range
  299. * @throws NullPointerException if <tt>first</tt> or <tt>last</tt> are
  300. * null
  301. * @throws IllegalArgumentException if <tt>first.compareTo(last) > 0</tt>
  302. * @return an enum set initially containing all of the elements in the
  303. * range defined by the two specified endpoints
  304. */
  305. public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {
  306. if (from.compareTo(to) > 0)
  307. throw new IllegalArgumentException(from + " > " + to);
  308. EnumSet<E> result = noneOf(from.getDeclaringClass());
  309. result.addRange(from, to);
  310. return result;
  311. }
  312. /**
  313. * Adds the specified range to this enum set, which is empty prior
  314. * to the call.
  315. */
  316. abstract void addRange(E from, E to);
  317. /**
  318. * Returns a copy of this set.
  319. *
  320. * @return a copy of this set.
  321. */
  322. public EnumSet<E> clone() {
  323. try {
  324. return (EnumSet<E>) super.clone();
  325. } catch(CloneNotSupportedException e) {
  326. throw new AssertionError(e);
  327. }
  328. }
  329. /**
  330. * Complements the contents of this enum set.
  331. */
  332. abstract void complement();
  333. /**
  334. * Throws an exception if e is not of the correct type for this enum set.
  335. */
  336. final void typeCheck(E e) {
  337. Class eClass = e.getClass();
  338. if (eClass != elementType && eClass.getSuperclass() != elementType)
  339. throw new ClassCastException(eClass + " != " + elementType);
  340. }
  341. /**
  342. * This class is used to serialize all EnumSet instances, regardless of
  343. * implementation type. It captures their "logical contents" and they
  344. * are reconstructed using public static factories. This is necessary
  345. * to ensure that the existence of a particular implementation type is
  346. * an implementation detail.
  347. *
  348. * @serial include
  349. */
  350. private static class SerializationProxy <E extends Enum<E>>
  351. implements java.io.Serializable
  352. {
  353. /**
  354. * The element type of this enum set.
  355. *
  356. * @serial
  357. */
  358. private final Class<E> elementType;
  359. /**
  360. * The elements contained in this enum set.
  361. *
  362. * @serial
  363. */
  364. private final Enum[] elements;
  365. SerializationProxy(EnumSet<E> set) {
  366. elementType = set.elementType;
  367. elements = (Enum[]) set.toArray(ZERO_LENGTH_ENUM_ARRAY);
  368. }
  369. private Object readResolve() {
  370. EnumSet<E> result = EnumSet.noneOf(elementType);
  371. for (Enum e : elements)
  372. result.add((E)e);
  373. return result;
  374. }
  375. private static final long serialVersionUID = 362491234563181265L;
  376. }
  377. Object writeReplace() {
  378. return new SerializationProxy<E>(this);
  379. }
  380. }