--- 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]));
+ }
+ }
}