--- old/src/java.base/share/classes/java/util/Set.java 2015-11-23 22:03:12.000000000 -0800
+++ new/src/java.base/share/classes/java/util/Set.java 2015-11-23 22:03:12.000000000 -0800
@@ -63,6 +63,23 @@
* Such exceptions are marked as "optional" in the specification for this
* interface.
*
+ *
+ * The {@link Set#of(Object...) Set.of()} static factory methods
+ * provide a convenient way to create immutable sets. The {@code Set}
+ * instances created by these methods have the following characteristics:
+ *
+ *
+ * - They cannot be modified. Attempts to modify them result in
+ * an {@code UnsupportedOperationException}.
+ *
- They are truly immutable only if the contained elements are themselves
+ * immutable. If an element is mutated, the behavior of the set is unspecified.
+ *
- They disallow null elements. Attempts to create them with
+ * null elements result in {@code NullPointerException}.
+ *
- They are serializable if all elements are serializable.
+ *
- They reject duplicate elements at creation time. Duplicate elements
+ * passed to a static factory method result in {@code IllegalArgumentException}.
+ *
+ *
* This interface is a member of the
*
* Java Collections Framework.
@@ -410,4 +427,342 @@
default Spliterator spliterator() {
return Spliterators.spliterator(this, Spliterator.DISTINCT);
}
+
+ /**
+ * Creates an immutable set containing zero elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @return the newly created set
+ *
+ * @since 9
+ */
+ static Set of() {
+ return Collections.emptySet();
+ }
+
+ /**
+ * Creates an immutable set containing one element.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the single set element
+ * @return the newly created set
+ * @throws NullPointerException if the element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1) {
+ return Collections.singleton(Objects.requireNonNull(e1));
+ }
+
+ /**
+ * Creates an immutable set containing two elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if the elements are duplicates
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2)));
+ if (set.size() != 2) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing three elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3)));
+ if (set.size() != 3) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing four elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @param e4 the fourth set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3, E e4) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3),
+ Objects.requireNonNull(e4)));
+ if (set.size() != 4) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing five elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @param e4 the fourth set element
+ * @param e5 the fifth set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3, E e4, E e5) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3),
+ Objects.requireNonNull(e4),
+ Objects.requireNonNull(e5)));
+ if (set.size() != 5) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing six elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @param e4 the fourth set element
+ * @param e5 the fifth set element
+ * @param e6 the sixth set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3, E e4, E e5, E e6) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3),
+ Objects.requireNonNull(e4),
+ Objects.requireNonNull(e5),
+ Objects.requireNonNull(e6)));
+ if (set.size() != 6) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing seven elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @param e4 the fourth set element
+ * @param e5 the fifth set element
+ * @param e6 the sixth set element
+ * @param e7 the seventh set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3),
+ Objects.requireNonNull(e4),
+ Objects.requireNonNull(e5),
+ Objects.requireNonNull(e6),
+ Objects.requireNonNull(e7)));
+ if (set.size() != 7) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing eight elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @param e4 the fourth set element
+ * @param e5 the fifth set element
+ * @param e6 the sixth set element
+ * @param e7 the seventh set element
+ * @param e8 the eighth set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3),
+ Objects.requireNonNull(e4),
+ Objects.requireNonNull(e5),
+ Objects.requireNonNull(e6),
+ Objects.requireNonNull(e7),
+ Objects.requireNonNull(e8)));
+ if (set.size() != 8) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing nine elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @param e4 the fourth set element
+ * @param e5 the fifth set element
+ * @param e6 the sixth set element
+ * @param e7 the seventh set element
+ * @param e8 the eighth set element
+ * @param e9 the ninth set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3),
+ Objects.requireNonNull(e4),
+ Objects.requireNonNull(e5),
+ Objects.requireNonNull(e6),
+ Objects.requireNonNull(e7),
+ Objects.requireNonNull(e8),
+ Objects.requireNonNull(e9)));
+ if (set.size() != 9) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing ten elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @param the set's element type
+ * @param e1 the first set element
+ * @param e2 the second set element
+ * @param e3 the third set element
+ * @param e4 the fourth set element
+ * @param e5 the fifth set element
+ * @param e6 the sixth set element
+ * @param e7 the seventh set element
+ * @param e8 the eighth set element
+ * @param e9 the ninth set element
+ * @param e10 the tenth set element
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null
+ *
+ * @since 9
+ */
+ static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
+ Set set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+ Objects.requireNonNull(e2),
+ Objects.requireNonNull(e3),
+ Objects.requireNonNull(e4),
+ Objects.requireNonNull(e5),
+ Objects.requireNonNull(e6),
+ Objects.requireNonNull(e7),
+ Objects.requireNonNull(e8),
+ Objects.requireNonNull(e9),
+ Objects.requireNonNull(e10)));
+ if (set.size() != 10) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
+
+ /**
+ * Creates an immutable set containing an arbitrary number of elements.
+ * See Immutable Set Static Factory Methods for details.
+ *
+ * @apiNote
+ * This method also accepts a single array as an argument. The element type of
+ * the resulting set will be the component type of the array, and the size of
+ * the set will be equal to the length of the array. To create a set with
+ * a single element that is an array, do the following:
+ *
+ * {@code
+ * String[] array = ... ;
+ * Set list = Set.of(array);
+ * }
+ *
+ * This will cause the {@link Set#of(Object) Set.of(E)} method
+ * to be invoked instead.
+ *
+ * @param the set's element type
+ * @param es the elements to be contained in the set
+ * @return the newly created set
+ * @throws IllegalArgumentException if there are any duplicate elements
+ * @throws NullPointerException if an element is null or if the array is null
+ *
+ * @since 9
+ */
+ @SafeVarargs
+ @SuppressWarnings("varargs")
+ static Set of(E... es) {
+ for (E e : es) { // throws NPE if es is null
+ Objects.requireNonNull(e);
+ }
+ // NOTE: this can allow a null element to slip through
+ Set set = new HashSet<>(Arrays.asList(es));
+ if (set.size() != es.length) {
+ throw new IllegalArgumentException("duplicate elements");
+ }
+ return Collections.unmodifiableSet(set);
+ }
}