--- old/src/java.base/share/classes/java/util/Collection.java 2017-09-20 16:53:05.000000000 -0700
+++ new/src/java.base/share/classes/java/util/Collection.java 2017-09-20 16:53:05.000000000 -0700
@@ -54,19 +54,15 @@
* constructors) but all of the general-purpose {@code Collection}
* implementations in the Java platform libraries comply.
*
- *
The "destructive" methods contained in this interface, that is, the
- * methods that modify the collection on which they operate, are specified to
- * throw {@code UnsupportedOperationException} if this collection does not
- * support the operation. If this is the case, these methods may, but are not
- * required to, throw an {@code UnsupportedOperationException} if the
- * invocation would have no effect on the collection. For example, invoking
- * the {@link #addAll(Collection)} method on an unmodifiable collection may,
- * but is not required to, throw the exception if the collection to be added
- * is empty.
- *
- *
- * Some collection implementations have restrictions on the elements that
- * they may contain. For example, some implementations prohibit null elements,
+ *
Certain methods are specified to be
+ * optional. If a collection implementation doesn't implement a
+ * particular operation, it should define the corresponding method to throw
+ * {@code UnsupportedOperationException}. Such methods are marked "optional
+ * operation" in method specifications of the collections interfaces.
+ *
+ *
Some collection implementations
+ * have restrictions on the elements that they may contain.
+ * For example, some implementations prohibit null elements,
* and some have restrictions on the types of their elements. Attempting to
* add an ineligible element throws an unchecked exception, typically
* {@code NullPointerException} or {@code ClassCastException}. Attempting
@@ -111,6 +107,75 @@
* methods. Implementations may optionally handle the self-referential scenario,
* however most current implementations do not do so.
*
+ *
View collections.
+ * Most collections contain elements themselves. By contrast, view collections
+ * themselves do not contain elements, but instead rely on a backing collection to
+ * store the actual elements. Operations that are not handled by the view
+ * collection itself are delegated to the backing collection. Examples of
+ * view collections include the wrapper collections returned by methods such as
+ * {@link unmodifiableCollection},
+ * {@link checkedCollection}, and
+ * {@link synchronizedCollection}.
+ * Other examples of view collections include collections that provide a
+ * different representation of the same elements, for example, as
+ * provided by {@link List.subList}, {@link NavigableSet.subSet}, or
+ * {@link Map.entrySet}. Any changes made to the backing collection are
+ * visible in the view collection. Correspondingly, any changes made to
+ * the view collection are written through to the backing collection.
+ * Although they technically aren't collections, instances of
+ * {@link Iterator} and {@link ListIterator} can also allow modifications
+ * to be written through to the backing collection, and in some cases,
+ * modifications to the backing collection will be visible to the Iterator
+ * during iteration.
+ *
+ *
Unmodifiable collections.
+ * Certain methods of this interface are considered "destructive" and are called
+ * "mutator" methods in that they modify the group of objects contained within
+ * the collection on which they operate. They can be specified to throw
+ * {@code UnsupportedOperationException} if this collection implementation
+ * does not support the operation. Such methods should (but are not required
+ * to) throw an {@code UnsupportedOperationException} if the invocation would
+ * have no effect on the collection. For example, invoking the
+ * {@link #addAll addAll} method on a collection that does not support
+ * the {@link #add add} operation should throw the exception if
+ * the collection passed as an argument is empty. It is recommended
+ * that such methods always throw the exception unconditionally, as
+ * throwing only in certain cases can lead to programming errors.
+ *
+ *
An unmodifiable collection is a collection, all of whose
+ * mutator methods (as defined above) are specified to throw
+ * {@code UnsupportedOperationException}. Such a collection thus cannot be
+ * modified by calling any methods on it. For a collection to be properly
+ * unmodifiable, any view collections derived from it must also be unmodifiable.
+ * For example, if a List is unmodifiable, the List returned by
+ * {@link List.subList} should also be unmodifiable.
+ *
+ *
An unmodifiable collection is not necessarily immutable. If the
+ * contained elements are mutable, the entire collection is clearly
+ * mutable, even though it might be unmodifiable. For example, consider
+ * two unmodifiable lists containing mutable elements. The result of calling
+ * {@code list1.equals(list2)} might differ from one call to the next if
+ * the elements had been mutated, even though both lists are unmodifiable.
+ * However, if an unmodifiable collection contains all immutable elements,
+ * it can be considered effectively immutable.
+ *
+ *
Unmodifiable view collections.
+ * An unmodifiable view is a collection that is unmodifiable and that is
+ * a view onto a backing collection. Mutator methods throw
+ * {@code UnsupportedOperationException}, as described above, while
+ * reading and querying methods are delegated to the backing collection.
+ * The effect is to provide read-only access to the backing collection.
+ * This is useful for a component to provide users with read access to
+ * an internal collection, while preventing them from modifying such
+ * collections unexpectedly.
+ *
+ *
Note that changes to the backing collection might still be possible,
+ * and if they occur, they are visible through the unmodifiable view. As
+ * above, an unmodifiable view collection is not necessarily immutable.
+ * However, if the backing collection of an unmodifiable view is effectively
+ * immutable, or if the only reference to the backing collection is through
+ * an unmodifiable view, the view can be considered effectively immutable.
+ *
*
This interface is a member of the
*
* Java Collections Framework.
--- old/src/java.base/share/classes/java/util/Collections.java 2017-09-20 16:53:06.000000000 -0700
+++ new/src/java.base/share/classes/java/util/Collections.java 2017-09-20 16:53:06.000000000 -0700
@@ -989,9 +989,8 @@
// Unmodifiable Wrappers
/**
- * Returns an unmodifiable view of the specified collection. This method
- * allows modules to provide users with "read-only" access to internal
- * collections. Query operations on the returned collection "read through"
+ * Returns an unmodifiable view of the
+ * specified collection. Query operations on the returned collection "read through"
* to the specified collection, and attempts to modify the returned
* collection, whether direct or via its iterator, result in an
* {@code UnsupportedOperationException}.
@@ -1102,9 +1101,8 @@
}
/**
- * Returns an unmodifiable view of the specified set. This method allows
- * modules to provide users with "read-only" access to internal sets.
- * Query operations on the returned set "read through" to the specified
+ * Returns an unmodifiable view of the
+ * specified set. Query operations on the returned set "read through" to the specified
* set, and attempts to modify the returned set, whether direct or via its
* iterator, result in an {@code UnsupportedOperationException}.
*
@@ -1132,9 +1130,8 @@
}
/**
- * Returns an unmodifiable view of the specified sorted set. This method
- * allows modules to provide users with "read-only" access to internal
- * sorted sets. Query operations on the returned sorted set "read
+ * Returns an unmodifiable view of the
+ * specified sorted set. Query operations on the returned sorted set "read
* through" to the specified sorted set. Attempts to modify the returned
* sorted set, whether direct, via its iterator, or via its
* {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
@@ -1180,9 +1177,8 @@
}
/**
- * Returns an unmodifiable view of the specified navigable set. This method
- * allows modules to provide users with "read-only" access to internal
- * navigable sets. Query operations on the returned navigable set "read
+ * Returns an unmodifiable view of the
+ * specified navigable set. Query operations on the returned navigable set "read
* through" to the specified navigable set. Attempts to modify the returned
* navigable set, whether direct, via its iterator, or via its
* {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
@@ -1269,9 +1265,8 @@
}
/**
- * Returns an unmodifiable view of the specified list. This method allows
- * modules to provide users with "read-only" access to internal
- * lists. Query operations on the returned list "read through" to the
+ * Returns an unmodifiable view of the
+ * specified list. Query operations on the returned list "read through" to the
* specified list, and attempts to modify the returned list, whether
* direct or via its iterator, result in an
* {@code UnsupportedOperationException}.
@@ -1415,9 +1410,8 @@
}
/**
- * Returns an unmodifiable view of the specified map. This method
- * allows modules to provide users with "read-only" access to internal
- * maps. Query operations on the returned map "read through"
+ * Returns an unmodifiable view of the
+ * specified map. Query operations on the returned map "read through"
* to the specified map, and attempts to modify the returned
* map, whether direct or via its collection views, result in an
* {@code UnsupportedOperationException}.
@@ -1765,9 +1759,8 @@
}
/**
- * Returns an unmodifiable view of the specified sorted map. This method
- * allows modules to provide users with "read-only" access to internal
- * sorted maps. Query operations on the returned sorted map "read through"
+ * Returns an unmodifiable view of the
+ * specified sorted map. Query operations on the returned sorted map "read through"
* to the specified sorted map. Attempts to modify the returned
* sorted map, whether direct, via its collection views, or via its
* {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
@@ -1809,9 +1802,8 @@
}
/**
- * Returns an unmodifiable view of the specified navigable map. This method
- * allows modules to provide users with "read-only" access to internal
- * navigable maps. Query operations on the returned navigable map "read
+ * Returns an unmodifiable view of the
+ * specified navigable map. Query operations on the returned navigable map "read
* through" to the specified navigable map. Attempts to modify the returned
* navigable map, whether direct, via its collection views, or via its
* {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
--- old/src/java.base/share/classes/java/util/List.java 2017-09-20 16:53:08.000000000 -0700
+++ new/src/java.base/share/classes/java/util/List.java 2017-09-20 16:53:08.000000000 -0700
@@ -87,15 +87,16 @@
* Such exceptions are marked as "optional" in the specification for this
* interface.
*
- *
- * The {@link List#of(Object...) List.of()} static factory methods
- * provide a convenient way to create immutable lists. The {@code List}
+ *
+ * The {@link List#of(Object...) List.of} and
+ * {@link List#copyOf List.copyOf} static factory methods
+ * provide a convenient way to create unmodifiable lists. The {@code List}
* instances created by these methods have the following characteristics:
*
*
- * - They are structurally immutable. Elements cannot be added, removed,
- * or replaced. Calling any mutator method will always cause
- * {@code UnsupportedOperationException} to be thrown.
+ *
- They are unmodifiable. Elements cannot
+ * be added, removed, or replaced. Calling any mutator method on the List
+ * will always cause {@code UnsupportedOperationException} to be thrown.
* However, if the contained elements are themselves mutable,
* this may cause the List's contents to appear to change.
*
- They disallow {@code null} elements. Attempts to create them with
@@ -776,9 +777,9 @@
}
/**
- * Returns an immutable list containing zero elements.
+ * Returns an unmodifiable list containing zero elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @return an empty {@code List}
@@ -790,9 +791,9 @@
}
/**
- * Returns an immutable list containing one element.
+ * Returns an unmodifiable list containing one element.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the single element
@@ -806,9 +807,9 @@
}
/**
- * Returns an immutable list containing two elements.
+ * Returns an unmodifiable list containing two elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -823,9 +824,9 @@
}
/**
- * Returns an immutable list containing three elements.
+ * Returns an unmodifiable list containing three elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -841,9 +842,9 @@
}
/**
- * Returns an immutable list containing four elements.
+ * Returns an unmodifiable list containing four elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -860,9 +861,9 @@
}
/**
- * Returns an immutable list containing five elements.
+ * Returns an unmodifiable list containing five elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -880,9 +881,9 @@
}
/**
- * Returns an immutable list containing six elements.
+ * Returns an unmodifiable list containing six elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -902,9 +903,9 @@
}
/**
- * Returns an immutable list containing seven elements.
+ * Returns an unmodifiable list containing seven elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -925,9 +926,9 @@
}
/**
- * Returns an immutable list containing eight elements.
+ * Returns an unmodifiable list containing eight elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -949,9 +950,9 @@
}
/**
- * Returns an immutable list containing nine elements.
+ * Returns an unmodifiable list containing nine elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -974,9 +975,9 @@
}
/**
- * Returns an immutable list containing ten elements.
+ * Returns an unmodifiable list containing ten elements.
*
- * See Immutable List Static Factory Methods for details.
+ * See Unmodifiable Lists for details.
*
* @param the {@code List}'s element type
* @param e1 the first element
@@ -1000,8 +1001,8 @@
}
/**
- * Returns an immutable list containing an arbitrary number of elements.
- * See Immutable List Static Factory Methods for details.
+ * Returns an unmodifiable list containing an arbitrary number of elements.
+ * See Unmodifiable Lists for details.
*
* @apiNote
* This method also accepts a single array as an argument. The element type of
@@ -1038,4 +1039,23 @@
return new ImmutableCollections.ListN<>(elements);
}
}
+
+ /**
+ * Returns an unmodifiable List containing the elements of the given Collection,
+ * in iteration order. See Unmodifiable Lists for
+ * information about the characteristics of the returned List.
+ *
+ * @param the {@code List}'s element type
+ * @param coll the collection from which elements are drawn
+ * @return the new List
+ * @since 10
+ */
+ @SuppressWarnings("unchecked")
+ static List copyOf(Collection extends E> coll) {
+ if (coll instanceof ImmutableCollections.AbstractImmutableList) {
+ return (List)coll;
+ } else {
+ return (List)List.of(coll.toArray());
+ }
+ }
}
--- old/src/java.base/share/classes/java/util/Map.java 2017-09-20 16:53:09.000000000 -0700
+++ new/src/java.base/share/classes/java/util/Map.java 2017-09-20 16:53:09.000000000 -0700
@@ -110,17 +110,18 @@
* Implementations may optionally handle the self-referential scenario, however
* most current implementations do not do so.
*
- *
- *
The {@link Map#of() Map.of()} and
- * {@link Map#ofEntries(Map.Entry...) Map.ofEntries()}
- * static factory methods provide a convenient way to create immutable maps.
+ *
+ * The {@link Map#of() Map.of},
+ * {@link Map#ofEntries(Map.Entry...) Map.ofEntries}, and
+ * {@link Map#copyOf Map.copyOf}
+ * static factory methods provide a convenient way to create unmodifiable maps.
* The {@code Map}
* instances created by these methods have the following characteristics:
*
*
- * - They are structurally immutable. Keys and values cannot be added,
- * removed, or updated. Calling any mutator method will always cause
- * {@code UnsupportedOperationException} to be thrown.
+ *
- They are unmodifiable. Keys and values
+ * cannot be added, removed, or updated. Calling any mutator method on the Map
+ * will always cause {@code UnsupportedOperationException} to be thrown.
* However, if the contained keys or values are themselves mutable, this may cause the
* Map to behave inconsistently or its contents to appear to change.
*
- They disallow {@code null} keys and values. Attempts to create them with
@@ -1276,8 +1277,8 @@
}
/**
- * Returns an immutable map containing zero mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing zero mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1290,8 +1291,8 @@
}
/**
- * Returns an immutable map containing a single mapping.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing a single mapping.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1307,8 +1308,8 @@
}
/**
- * Returns an immutable map containing two mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing two mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1327,8 +1328,8 @@
}
/**
- * Returns an immutable map containing three mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing three mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1349,8 +1350,8 @@
}
/**
- * Returns an immutable map containing four mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing four mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1373,8 +1374,8 @@
}
/**
- * Returns an immutable map containing five mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing five mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1399,8 +1400,8 @@
}
/**
- * Returns an immutable map containing six mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing six mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1429,8 +1430,8 @@
}
/**
- * Returns an immutable map containing seven mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing seven mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1461,8 +1462,8 @@
}
/**
- * Returns an immutable map containing eight mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing eight mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1495,8 +1496,8 @@
}
/**
- * Returns an immutable map containing nine mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing nine mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1531,8 +1532,8 @@
}
/**
- * Returns an immutable map containing ten mappings.
- * See Immutable Map Static Factory Methods for details.
+ * Returns an unmodifiable map containing ten mappings.
+ * See Unmodifiable Maps for details.
*
* @param the {@code Map}'s key type
* @param the {@code Map}'s value type
@@ -1569,9 +1570,9 @@
}
/**
- * Returns an immutable map containing keys and values extracted from the given entries.
+ * Returns an unmodifiable map containing keys and values extracted from the given entries.
* The entries themselves are not stored in the map.
- * See Immutable Map Static Factory Methods for details.
+ * See Unmodifiable Maps for details.
*
* @apiNote
* It is convenient to create the map entries using the {@link Map#entry Map.entry()} method.
@@ -1619,7 +1620,7 @@
}
/**
- * Returns an immutable {@link Entry} containing the given key and value.
+ * Returns an unmodifiable {@link Entry} containing the given key and value.
* These entries are suitable for populating {@code Map} instances using the
* {@link Map#ofEntries Map.ofEntries()} method.
* The {@code Entry} instances created by this method have the following characteristics:
@@ -1627,7 +1628,7 @@
*
* - They disallow {@code null} keys and values. Attempts to create them using a {@code null}
* key or value result in {@code NullPointerException}.
- *
- They are immutable. Calls to {@link Entry#setValue Entry.setValue()}
+ *
- They are unmodifiable. Calls to {@link Entry#setValue Entry.setValue()}
* on a returned {@code Entry} result in {@code UnsupportedOperationException}.
*
- They are not serializable.
*
- They are value-based.
@@ -1655,4 +1656,24 @@
// KeyValueHolder checks for nulls
return new KeyValueHolder<>(k, v);
}
+
+ /**
+ * Returns an unmodifiable Map containing the entries of the given Map.
+ * See Unmodifiable Maps for
+ * information about the characteristics of the returned Map.
+ *
+ * @param the {@code Map}'s key type
+ * @param the {@code Map}'s value type
+ * @param map the map from which entries are drawn
+ * @return the new {@code Map}
+ * @since 10
+ */
+ @SuppressWarnings({"rawtypes","unchecked"})
+ static Map copyOf(Map extends K, ? extends V> map) {
+ if (map instanceof ImmutableCollections.AbstractImmutableMap) {
+ return (Map)map;
+ } else {
+ return (Map)Map.ofEntries(map.entrySet().toArray(new Entry[0]));
+ }
+ }
}
--- old/src/java.base/share/classes/java/util/Set.java 2017-09-20 16:53:10.000000000 -0700
+++ new/src/java.base/share/classes/java/util/Set.java 2017-09-20 16:53:10.000000000 -0700
@@ -63,15 +63,16 @@
* 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}
+ *
+ * The {@link Set#of(Object...) Set.of} and
+ * {@link Set#copyOf Set.copyOf} static factory methods
+ * provide a convenient way to create unmodifiable sets. The {@code Set}
* instances created by these methods have the following characteristics:
*
*
- * - They are structurally immutable. Elements cannot be added or
- * removed. Calling any mutator method will always cause
- * {@code UnsupportedOperationException} to be thrown.
+ *
- They are unmodifiable. Elements cannot
+ * be added or removed. Calling any mutator method on the Set
+ * will always cause {@code UnsupportedOperationException} to be thrown.
* However, if the contained elements are themselves mutable, this may cause the
* Set to behave inconsistently or its contents to appear to change.
*
- They disallow {@code null} elements. Attempts to create them with
@@ -439,8 +440,8 @@
}
/**
- * Returns an immutable set containing zero elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing zero elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @return an empty {@code Set}
@@ -452,8 +453,8 @@
}
/**
- * Returns an immutable set containing one element.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing one element.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the single element
@@ -467,8 +468,8 @@
}
/**
- * Returns an immutable set containing two elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing two elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -484,8 +485,8 @@
}
/**
- * Returns an immutable set containing three elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing three elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -502,8 +503,8 @@
}
/**
- * Returns an immutable set containing four elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing four elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -521,8 +522,8 @@
}
/**
- * Returns an immutable set containing five elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing five elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -541,8 +542,8 @@
}
/**
- * Returns an immutable set containing six elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing six elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -563,8 +564,8 @@
}
/**
- * Returns an immutable set containing seven elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing seven elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -586,8 +587,8 @@
}
/**
- * Returns an immutable set containing eight elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing eight elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -610,8 +611,8 @@
}
/**
- * Returns an immutable set containing nine elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing nine elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -635,8 +636,8 @@
}
/**
- * Returns an immutable set containing ten elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing ten elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -661,8 +662,8 @@
}
/**
- * Returns an immutable set containing an arbitrary number of elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing an arbitrary number of elements.
+ * See Unmodifiable Sets for details.
*
* @apiNote
* This method also accepts a single array as an argument. The element type of
@@ -700,4 +701,26 @@
return new ImmutableCollections.SetN<>(elements);
}
}
+
+ /**
+ * Returns an unmodifiable Set containing the elements of the given Collection.
+ * Duplicate elements are permitted. If the Collection has a defined iteration order,
+ * the first element of any duplicates is preserved, and the rest are discarded.
+ * Otherwise, an arbitrary element is preserved.
+ * See Unmodifiable Sets for
+ * information about the characteristics of the returned Set.
+ *
+ * @param the {@code Set}'s element type
+ * @param coll the collection from which elements are drawn
+ * @return the new Set
+ * @since 10
+ */
+ @SuppressWarnings("unchecked")
+ static Set copyOf(Collection extends E> coll) {
+ if (coll instanceof ImmutableCollections.AbstractImmutableSet) {
+ return (Set)coll;
+ } else {
+ return (Set)Set.of(coll.stream().distinct().toArray());
+ }
+ }
}
--- old/src/java.base/share/classes/java/util/stream/Collectors.java 2017-09-20 16:53:11.000000000 -0700
+++ new/src/java.base/share/classes/java/util/stream/Collectors.java 2017-09-20 16:53:11.000000000 -0700
@@ -116,6 +116,8 @@
= Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED,
Collector.Characteristics.IDENTITY_FINISH));
static final Set CH_NOID = Collections.emptySet();
+ static final Set CH_UNORDERED_NOID
+ = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED));
private Collectors() { }
@@ -279,6 +281,24 @@
}
/**
+ * Returns a {@code Collector} that accumulates the input elements into an
+ * unmodifiable {@code List}.
+ *
+ * @param the type of the input elements
+ * @return a {@code Collector} which collects all the input elements into a
+ * {@code List}, in encounter order
+ * @since 10
+ */
+ @SuppressWarnings("unchecked")
+ public static
+ Collector> toUnmodifiableList() {
+ return new CollectorImpl<>((Supplier
>) ArrayList::new, List::add,
+ (left, right) -> { left.addAll(right); return left; },
+ list -> (List)List.of(list.toArray()),
+ CH_NOID);
+ }
+
+ /**
* Returns a {@code Collector} that accumulates the input elements into a
* new {@code Set}. There are no guarantees on the type, mutability,
* serializability, or thread-safety of the {@code Set} returned; if more
@@ -306,6 +326,33 @@
}
/**
+ * Returns a {@code Collector} that accumulates the input elements into an
+ * unmodifiable {@code Set}.
+ *
+ * This is an {@link Collector.Characteristics#UNORDERED unordered}
+ * Collector.
+ *
+ * @param the type of the input elements
+ * @return a {@code Collector} which collects all the input elements into a
+ * {@code Set}
+ * @since 10
+ */
+ @SuppressWarnings("unchecked")
+ public static
+ Collector> toUnmodifiableSet() {
+ return new CollectorImpl<>((Supplier>) HashSet::new, Set::add,
+ (left, right) -> {
+ if (left.size() < right.size()) {
+ right.addAll(left); return right;
+ } else {
+ left.addAll(right); return left;
+ }
+ },
+ set -> (Set)Set.of(set.toArray()),
+ CH_UNORDERED_NOID);
+ }
+
+ /**
* Returns a {@code Collector} that concatenates the input elements into a
* {@code String}, in encounter order.
*
@@ -1411,6 +1458,41 @@
}
/**
+ * Returns a {@code Collector} that accumulates the input elements into an
+ * unmodifiable {@code Map},
+ * whose keys and values are the result of applying the provided
+ * mapping functions to the input elements.
+ *
+ * If the mapped keys contain duplicates (according to
+ * {@link Object#equals(Object)}), an {@code IllegalStateException} is
+ * thrown when the collection operation is performed. If the mapped keys
+ * may have duplicates, use {@link #toUnmodifiableMap(Function, Function, BinaryOperator)}
+ * instead.
+ *
+ * @param the type of the input elements
+ * @param the output type of the key mapping function
+ * @param the output type of the value mapping function
+ * @param keyMapper a mapping function to produce keys
+ * @param valueMapper a mapping function to produce values
+ * @return a {@code Collector} which collects elements into an unmodifiable {@code Map}
+ * whose keys and values are the result of applying mapping functions to
+ * the input elements
+ *
+ * @see #toUnmodifiableMap(Function, Function, BinaryOperator)
+ * @since 10
+ */
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ public static
+ Collector> toUnmodifiableMap(Function super T, ? extends K> keyMapper,
+ Function super T, ? extends U> valueMapper) {
+ return new CollectorImpl<>(() -> new ArrayList>(),
+ (list, t) -> list.add(Map.entry(keyMapper.apply(t), valueMapper.apply(t))),
+ (list1, list2) -> { list1.addAll(list2); return list1; },
+ list -> (Map)Map.ofEntries(list.toArray(new Map.Entry[0])),
+ CH_UNORDERED_NOID);
+ }
+
+ /**
* Returns a {@code Collector} that accumulates elements into a
* {@code Map} whose keys and values are the result of applying the provided
* mapping functions to the input elements.
@@ -1473,6 +1555,45 @@
return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}
+
+ /**
+ * Returns a {@code Collector} that accumulates the input elements into an
+ * unmodifiable {@code Map},
+ * whose keys and values are the result of applying the provided
+ * mapping functions to the input elements.
+ *
+ * If the mapped
+ * keys contain duplicates (according to {@link Object#equals(Object)}),
+ * the value mapping function is applied to each equal element, and the
+ * results are merged using the provided merging function.
+ *
+ * @param the type of the input elements
+ * @param the output type of the key mapping function
+ * @param the output type of the value mapping function
+ * @param keyMapper a mapping function to produce keys
+ * @param valueMapper a mapping function to produce values
+ * @param mergeFunction a merge function, used to resolve collisions between
+ * values associated with the same key, as supplied
+ * to {@link Map#merge(Object, Object, BiFunction)}
+ * @return a {@code Collector} which collects elements into an unmodifiable {@code Map}
+ * whose keys are the result of applying a key mapping function to the input
+ * elements, and whose values are the result of applying a value mapping
+ * function to all input elements equal to the key and combining them
+ * using the merge function
+ *
+ * @see #toUnmodifiableMap(Function, Function)
+ * @since 10
+ */
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ public static
+ Collector> toUnmodifiableMap(Function super T, ? extends K> keyMapper,
+ Function super T, ? extends U> valueMapper,
+ BinaryOperator mergeFunction) {
+ return collectingAndThen(
+ toMap(keyMapper, valueMapper, mergeFunction, HashMap::new),
+ hm -> (Map)Map.ofEntries(hm.entrySet().toArray(new Map.Entry[0])));
+ }
+
/**
* Returns a {@code Collector} that accumulates elements into a
* {@code Map} whose keys and values are the result of applying the provided