|
1 | 1 | /*
|
2 |
| - * Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
@@ -1008,12 +1008,17 @@ public static int lastIndexOfSubList(List<?> source, List<?> target) {
|
1008 | 1008 | * The returned collection will be serializable if the specified collection
|
1009 | 1009 | * is serializable.
|
1010 | 1010 | *
|
| 1011 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1011 | 1012 | * @param <T> the class of the objects in the collection
|
1012 | 1013 | * @param c the collection for which an unmodifiable view is to be
|
1013 | 1014 | * returned.
|
1014 | 1015 | * @return an unmodifiable view of the specified collection.
|
1015 | 1016 | */
|
| 1017 | + @SuppressWarnings("unchecked") |
1016 | 1018 | public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
|
| 1019 | + if (c.getClass() == UnmodifiableCollection.class) { |
| 1020 | + return (Collection<T>) c; |
| 1021 | + } |
1017 | 1022 | return new UnmodifiableCollection<>(c);
|
1018 | 1023 | }
|
1019 | 1024 |
|
@@ -1116,11 +1121,17 @@ public Stream<E> parallelStream() {
|
1116 | 1121 | * The returned set will be serializable if the specified set
|
1117 | 1122 | * is serializable.
|
1118 | 1123 | *
|
| 1124 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1119 | 1125 | * @param <T> the class of the objects in the set
|
1120 | 1126 | * @param s the set for which an unmodifiable view is to be returned.
|
1121 | 1127 | * @return an unmodifiable view of the specified set.
|
1122 | 1128 | */
|
| 1129 | + @SuppressWarnings("unchecked") |
1123 | 1130 | public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
|
| 1131 | + // Not checking for subclasses because of heap pollution and information leakage. |
| 1132 | + if (s.getClass() == UnmodifiableSet.class) { |
| 1133 | + return (Set<T>) s; |
| 1134 | + } |
1124 | 1135 | return new UnmodifiableSet<>(s);
|
1125 | 1136 | }
|
1126 | 1137 |
|
@@ -1148,12 +1159,17 @@ static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
|
1148 | 1159 | * The returned sorted set will be serializable if the specified sorted set
|
1149 | 1160 | * is serializable.
|
1150 | 1161 | *
|
| 1162 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1151 | 1163 | * @param <T> the class of the objects in the set
|
1152 | 1164 | * @param s the sorted set for which an unmodifiable view is to be
|
1153 | 1165 | * returned.
|
1154 | 1166 | * @return an unmodifiable view of the specified sorted set.
|
1155 | 1167 | */
|
1156 | 1168 | public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
|
| 1169 | + // Not checking for subclasses because of heap pollution and information leakage. |
| 1170 | + if (s.getClass() == UnmodifiableSortedSet.class) { |
| 1171 | + return s; |
| 1172 | + } |
1157 | 1173 | return new UnmodifiableSortedSet<>(s);
|
1158 | 1174 | }
|
1159 | 1175 |
|
@@ -1197,13 +1213,17 @@ public SortedSet<E> tailSet(E fromElement) {
|
1197 | 1213 | * The returned navigable set will be serializable if the specified
|
1198 | 1214 | * navigable set is serializable.
|
1199 | 1215 | *
|
| 1216 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1200 | 1217 | * @param <T> the class of the objects in the set
|
1201 | 1218 | * @param s the navigable set for which an unmodifiable view is to be
|
1202 | 1219 | * returned
|
1203 | 1220 | * @return an unmodifiable view of the specified navigable set
|
1204 | 1221 | * @since 1.8
|
1205 | 1222 | */
|
1206 | 1223 | public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) {
|
| 1224 | + if (s.getClass() == UnmodifiableNavigableSet.class) { |
| 1225 | + return s; |
| 1226 | + } |
1207 | 1227 | return new UnmodifiableNavigableSet<>(s);
|
1208 | 1228 | }
|
1209 | 1229 |
|
@@ -1289,11 +1309,17 @@ public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
|
1289 | 1309 | * is serializable. Similarly, the returned list will implement
|
1290 | 1310 | * {@link RandomAccess} if the specified list does.
|
1291 | 1311 | *
|
| 1312 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1292 | 1313 | * @param <T> the class of the objects in the list
|
1293 | 1314 | * @param list the list for which an unmodifiable view is to be returned.
|
1294 | 1315 | * @return an unmodifiable view of the specified list.
|
1295 | 1316 | */
|
| 1317 | + @SuppressWarnings("unchecked") |
1296 | 1318 | public static <T> List<T> unmodifiableList(List<? extends T> list) {
|
| 1319 | + if (list.getClass() == UnmodifiableList.class || list.getClass() == UnmodifiableRandomAccessList.class) { |
| 1320 | + return (List<T>) list; |
| 1321 | + } |
| 1322 | + |
1297 | 1323 | return (list instanceof RandomAccess ?
|
1298 | 1324 | new UnmodifiableRandomAccessList<>(list) :
|
1299 | 1325 | new UnmodifiableList<>(list));
|
@@ -1438,12 +1464,18 @@ private Object writeReplace() {
|
1438 | 1464 | * The returned map will be serializable if the specified map
|
1439 | 1465 | * is serializable.
|
1440 | 1466 | *
|
| 1467 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1441 | 1468 | * @param <K> the class of the map keys
|
1442 | 1469 | * @param <V> the class of the map values
|
1443 | 1470 | * @param m the map for which an unmodifiable view is to be returned.
|
1444 | 1471 | * @return an unmodifiable view of the specified map.
|
1445 | 1472 | */
|
| 1473 | + @SuppressWarnings("unchecked") |
1446 | 1474 | public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
|
| 1475 | + // Not checking for subclasses because of heap pollution and information leakage. |
| 1476 | + if (m.getClass() == UnmodifiableMap.class) { |
| 1477 | + return (Map<K,V>) m; |
| 1478 | + } |
1447 | 1479 | return new UnmodifiableMap<>(m);
|
1448 | 1480 | }
|
1449 | 1481 |
|
@@ -1795,13 +1827,19 @@ public boolean equals(Object o) {
|
1795 | 1827 | * The returned sorted map will be serializable if the specified sorted map
|
1796 | 1828 | * is serializable.
|
1797 | 1829 | *
|
| 1830 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1798 | 1831 | * @param <K> the class of the map keys
|
1799 | 1832 | * @param <V> the class of the map values
|
1800 | 1833 | * @param m the sorted map for which an unmodifiable view is to be
|
1801 | 1834 | * returned.
|
1802 | 1835 | * @return an unmodifiable view of the specified sorted map.
|
1803 | 1836 | */
|
| 1837 | + @SuppressWarnings("unchecked") |
1804 | 1838 | public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
|
| 1839 | + // Not checking for subclasses because of heap pollution and information leakage. |
| 1840 | + if (m.getClass() == UnmodifiableSortedMap.class) { |
| 1841 | + return (SortedMap<K,V>) m; |
| 1842 | + } |
1805 | 1843 | return new UnmodifiableSortedMap<>(m);
|
1806 | 1844 | }
|
1807 | 1845 |
|
@@ -1840,14 +1878,19 @@ public SortedMap<K,V> tailMap(K fromKey)
|
1840 | 1878 | * The returned navigable map will be serializable if the specified
|
1841 | 1879 | * navigable map is serializable.
|
1842 | 1880 | *
|
| 1881 | + * @implNote This method may return its argument if the argument is already unmodifiable. |
1843 | 1882 | * @param <K> the class of the map keys
|
1844 | 1883 | * @param <V> the class of the map values
|
1845 | 1884 | * @param m the navigable map for which an unmodifiable view is to be
|
1846 | 1885 | * returned
|
1847 | 1886 | * @return an unmodifiable view of the specified navigable map
|
1848 | 1887 | * @since 1.8
|
1849 | 1888 | */
|
| 1889 | + @SuppressWarnings("unchecked") |
1850 | 1890 | public static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K, ? extends V> m) {
|
| 1891 | + if (m.getClass() == UnmodifiableNavigableMap.class) { |
| 1892 | + return (NavigableMap<K,V>) m; |
| 1893 | + } |
1851 | 1894 | return new UnmodifiableNavigableMap<>(m);
|
1852 | 1895 | }
|
1853 | 1896 |
|
|
0 commit comments