Created
September 22, 2013 12:37
-
-
Save jodastephen/6659515 to your computer and use it in GitHub Desktop.
Patch proposing additing of() static methods to the collection interfaces.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -r b7f9218d0f37 src/share/classes/java/util/Collection.java | |
--- a/src/share/classes/java/util/Collection.java Sat Sep 14 22:54:38 2013 +0100 | |
+++ b/src/share/classes/java/util/Collection.java Sun Sep 22 05:35:55 2013 -0700 | |
@@ -135,6 +135,30 @@ | |
*/ | |
public interface Collection<E> extends Iterable<E> { | |
+ | |
+ /** | |
+ * Returns an immutable collection consisting of the specified elements. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create a collection. | |
+ * One common use case is creating a collection within a test case. | |
+ * The returned collection is serializable and provides iterators that | |
+ * return the elements in the order specified. | |
+ * | |
+ * @param <R> the type of set elements | |
+ * @param elements the elements of the returned collection, not null | |
+ * @return a collection consisting of the specified elements, not null | |
+ * @throws NullPointerException if the elements varargs array is null | |
+ * @since 1.8 | |
+ */ | |
+ public static <R> Collection<R> of(R... elements) { | |
+ if (elements.length == 0) { | |
+ return Collections.emptyList(); | |
+ } else if (elements.length == 1) { | |
+ return Collections.singletonList(elements[0]); | |
+ } | |
+ return Collections.unmodifiableList(Arrays.asList(elements.clone())); | |
+ } | |
+ | |
// Query Operations | |
/** | |
diff -r b7f9218d0f37 src/share/classes/java/util/List.java | |
--- a/src/share/classes/java/util/List.java Sat Sep 14 22:54:38 2013 +0100 | |
+++ b/src/share/classes/java/util/List.java Sun Sep 22 05:35:55 2013 -0700 | |
@@ -109,6 +109,29 @@ | |
*/ | |
public interface List<E> extends Collection<E> { | |
+ | |
+ /** | |
+ * Returns an immutable list consisting of the specified elements. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create a list. | |
+ * One common use case is creating a list within a test case. | |
+ * The returned list is serializable. | |
+ * | |
+ * @param <R> the type of list elements | |
+ * @param elements the elements of the returned list, not null | |
+ * @return a list consisting of the specified elements, not null | |
+ * @throws NullPointerException if the elements varargs array is null | |
+ * @since 1.8 | |
+ */ | |
+ public static <R> List<R> of(R... elements) { | |
+ if (elements.length == 0) { | |
+ return Collections.emptyList(); | |
+ } else if (elements.length == 1) { | |
+ return Collections.singletonList(elements[0]); | |
+ } | |
+ return Collections.unmodifiableList(Arrays.asList(elements.clone())); | |
+ } | |
+ | |
// Query Operations | |
/** | |
diff -r b7f9218d0f37 src/share/classes/java/util/Map.java | |
--- a/src/share/classes/java/util/Map.java Sat Sep 14 22:54:38 2013 +0100 | |
+++ b/src/share/classes/java/util/Map.java Sun Sep 22 05:35:55 2013 -0700 | |
@@ -120,6 +120,71 @@ | |
* @since 1.2 | |
*/ | |
public interface Map<K,V> { | |
+ | |
+ /** | |
+ * Returns an immutable map consisting of the specified elements. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create a map. | |
+ * One common use case is creating a map within a test case. | |
+ * The returned map is serializable. | |
+ * | |
+ * <p>The first key-value pair has fully defined types. The rest of the | |
+ * key-value pairs are untyped and are placed into the resulting map | |
+ * using an unsafe cast. | |
+ * | |
+ * @param <K> the type of the key | |
+ * @param <K> the type of the value | |
+ * @param firstKey the first key | |
+ * @param firstValue the first value | |
+ * @param keysAndValues the second and subsequent key-value pairs | |
+ * with each pair of values forming a kay-value pair; the key | |
+ * and value types must be valid for the map generics, but this | |
+ * is not checked, not null | |
+ * @return a map consisting of the specified entries, not null | |
+ * @throws NullPointerException if the entries varargs array is null | |
+ * @throws IndexOutOfBoundsException if the array has an odd number | |
+ * of elements | |
+ * @since 1.8 | |
+ */ | |
+ public static <K, V> Map<K, V> of(K firstKey, V firstValue, Object... keysAndValues) { | |
+ if (keysAndValues.length == 0) { | |
+ return Collections.singletonMap(firstKey, firstValue); | |
+ } | |
+ Map<K, V> map = new HashMap<>(); | |
+ map.put(firstKey, firstValue); | |
+ for (int i = 0; i < keysAndValues.length; i += 2) { | |
+ map.put((K) keysAndValues[i], (V) keysAndValues[i + 1]); // unsafe cast | |
+ } | |
+ return Collections.unmodifiableMap(map); | |
+ } | |
+ | |
+ /** | |
+ * Returns an immutable map consisting of the specified entries. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create a map. | |
+ * One common use case is creating a map within a test case. | |
+ * The returned map is serializable. | |
+ * | |
+ * @param <K> the type of the key | |
+ * @param <K> the type of the value | |
+ * @param entries the entries of the returned map, not null | |
+ * @return a map consisting of the specified entries, not null | |
+ * @throws NullPointerException if the entries varargs array is null | |
+ * @since 1.8 | |
+ */ | |
+ public static <K, V> Map<K, V> ofEntries(Entry<K, V>... entries) { | |
+ if (entries.length == 0) { | |
+ return Collections.emptyMap(); | |
+ } else if (entries.length == 1) { | |
+ return Collections.singletonMap(entries[0].getKey(), entries[0].getValue()); | |
+ } | |
+ Map<K, V> map = new HashMap<>(); | |
+ for (Entry<K, V> entry : entries) { | |
+ map.put(entry.getKey(), entry.getValue()); | |
+ } | |
+ return Collections.unmodifiableMap(map); | |
+ } | |
+ | |
// Query Operations | |
/** | |
@@ -367,6 +432,24 @@ | |
*/ | |
interface Entry<K,V> { | |
/** | |
+ * Returns an immutable map entry consisting of the specified key | |
+ * and value. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create an entry. | |
+ * The returned entry is serializable. | |
+ * | |
+ * @param <K> the type of entry key | |
+ * @param <V> the type of entry value | |
+ * @param key the key of the returned entry, may be null | |
+ * @param value the value of the returned entry, may be null | |
+ * @return an entry consisting of the specified key and value, not null | |
+ * @since 1.8 | |
+ */ | |
+ public static <K, V> Entry<K, V> of(K key, V value) { | |
+ return new AbstractMap.SimpleImmutableEntry<>(key, value); | |
+ } | |
+ | |
+ /** | |
* Returns the key corresponding to this entry. | |
* | |
* @return the key corresponding to this entry | |
diff -r b7f9218d0f37 src/share/classes/java/util/NavigableSet.java | |
--- a/src/share/classes/java/util/NavigableSet.java Sat Sep 14 22:54:38 2013 +0100 | |
+++ b/src/share/classes/java/util/NavigableSet.java Sun Sep 22 05:35:55 2013 -0700 | |
@@ -84,6 +84,29 @@ | |
* @since 1.6 | |
*/ | |
public interface NavigableSet<E> extends SortedSet<E> { | |
+ | |
+ /** | |
+ * Returns an immutable navigable set consisting of the specified elements. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create a set. | |
+ * One common use case is creating a set within a test case. | |
+ * All input elements must be able to be compared using the comparator. | |
+ * The returned set is serializable. | |
+ * | |
+ * @param <R> the type of set elements | |
+ * @param comparator the comparator to use, not null | |
+ * @param elements the elements of the returned set, not null | |
+ * @return a set consisting of the specified elements, not null | |
+ * @throws NullPointerException if the comparator or elements varargs array is null | |
+ * @since 1.8 | |
+ */ | |
+ public static <R> NavigableSet<R> of(Comparator<? super R> comparator, R... elements) { | |
+ if (elements.length == 0) { | |
+ return Collections.emptyNavigableSet(); | |
+ } | |
+ return Collections.unmodifiableNavigableSet(new TreeSet(Arrays.asList(elements.clone()), comparator)); | |
+ } | |
+ | |
/** | |
* Returns the greatest element in this set strictly less than the | |
* given element, or {@code null} if there is no such element. | |
diff -r b7f9218d0f37 src/share/classes/java/util/Set.java | |
--- a/src/share/classes/java/util/Set.java Sat Sep 14 22:54:38 2013 +0100 | |
+++ b/src/share/classes/java/util/Set.java Sun Sep 22 05:35:55 2013 -0700 | |
@@ -83,6 +83,29 @@ | |
*/ | |
public interface Set<E> extends Collection<E> { | |
+ | |
+ /** | |
+ * Returns an immutable set consisting of the specified elements. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create a set. | |
+ * One common use case is creating a set within a test case. | |
+ * The returned set is serializable. | |
+ * | |
+ * @param <R> the type of set elements | |
+ * @param elements the elements of the returned set, not null | |
+ * @return a set consisting of the specified elements, not null | |
+ * @throws NullPointerException if the elements varargs array is null | |
+ * @since 1.8 | |
+ */ | |
+ public static <R> Set<R> of(R... elements) { | |
+ if (elements.length == 0) { | |
+ return Collections.emptySet(); | |
+ } else if (elements.length == 1) { | |
+ return Collections.singleton(elements[0]); | |
+ } | |
+ return Collections.unmodifiableSet(new HashSet(Arrays.asList(elements.clone()))); | |
+ } | |
+ | |
// Query Operations | |
/** | |
diff -r b7f9218d0f37 src/share/classes/java/util/SortedSet.java | |
--- a/src/share/classes/java/util/SortedSet.java Sat Sep 14 22:54:38 2013 +0100 | |
+++ b/src/share/classes/java/util/SortedSet.java Sun Sep 22 05:35:55 2013 -0700 | |
@@ -106,6 +106,29 @@ | |
*/ | |
public interface SortedSet<E> extends Set<E> { | |
+ | |
+ /** | |
+ * Returns an immutable sorted set consisting of the specified elements. | |
+ * | |
+ * <p>This provides a simple and effective mechanism to create a set. | |
+ * One common use case is creating a set within a test case. | |
+ * All input elements must be able to be compared using the comparator. | |
+ * The returned set is serializable. | |
+ * | |
+ * @param <R> the type of set elements | |
+ * @param comparator the comparator to use, not null | |
+ * @param elements the elements of the returned set, not null | |
+ * @return a set consisting of the specified elements, not null | |
+ * @throws NullPointerException if the comparator or elements varargs array is null | |
+ * @since 1.8 | |
+ */ | |
+ public static <R> SortedSet<R> of(Comparator<? super R> comparator, R... elements) { | |
+ if (elements.length == 0) { | |
+ return Collections.emptySortedSet(); | |
+ } | |
+ return Collections.unmodifiableSortedSet(new TreeSet(Arrays.asList(elements.clone()), comparator)); | |
+ } | |
+ | |
/** | |
* Returns the comparator used to order the elements in this set, | |
* or <tt>null</tt> if this set uses the {@linkplain Comparable | |
diff -r b7f9218d0f37 src/share/classes/java/util/TreeSet.java | |
--- a/src/share/classes/java/util/TreeSet.java Sat Sep 14 22:54:38 2013 +0100 | |
+++ b/src/share/classes/java/util/TreeSet.java Sun Sep 22 05:35:55 2013 -0700 | |
@@ -161,6 +161,30 @@ | |
} | |
/** | |
+ * Constructs a new tree set containing the elements in the specified | |
+ * collection, sorted according to specified comparator. All elements | |
+ * in the input collection must be <i>mutually comparable</i> by the | |
+ * specified comparator: {@code comparator.compare(e1, e2)} must not | |
+ * throw a {@code ClassCastException} for any elements {@code e1} | |
+ * and {@code e2} in the set. If the user attempts to add an element | |
+ * to the set that violates this constraint, the {@code add} call will | |
+ * throw a {@code ClassCastException}. | |
+ * | |
+ * @param c collection whose elements will comprise the new set | |
+ * @param comparator the comparator that will be used to order this set. | |
+ * If {@code null}, the {@linkplain Comparable natural | |
+ * ordering} of the elements will be used. | |
+ * @throws ClassCastException if the elements in {@code c} are | |
+ * not {@link Comparable}, or are not mutually comparable | |
+ * @throws NullPointerException if the specified comparator or collection | |
+ * is null | |
+ */ | |
+ public TreeSet(Collection<? extends E> c, Comparator<? super E> comparator) { | |
+ this(comparator); | |
+ addAll(c); | |
+ } | |
+ | |
+ /** | |
* Constructs a new tree set containing the same elements and | |
* using the same ordering as the specified sorted set. | |
* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple yet awesome proposal