11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27 import java.io.Serializable;
28 import java.io.ObjectOutputStream;
29 import java.io.IOException;
30 import java.lang.reflect.Array;
31
32 /**
33 * This class consists exclusively of static methods that operate on or return
34 * collections. It contains polymorphic algorithms that operate on
35 * collections, "wrappers", which return a new collection backed by a
36 * specified collection, and a few other odds and ends.
37 *
38 * <p>The methods of this class all throw a <tt>NullPointerException</tt>
39 * if the collections or class objects provided to them are null.
40 *
41 * <p>The documentation for the polymorphic algorithms contained in this class
42 * generally includes a brief description of the <i>implementation</i>. Such
43 * descriptions should be regarded as <i>implementation notes</i>, rather than
44 * parts of the <i>specification</i>. Implementors should feel free to
45 * substitute other algorithms, so long as the specification itself is adhered
46 * to. (For example, the algorithm used by <tt>sort</tt> does not have to be
47 * a mergesort, but it does have to be <i>stable</i>.)
48 *
49 * <p>The "destructive" algorithms contained in this class, that is, the
50 * algorithms that modify the collection on which they operate, are specified
1374 keySet = unmodifiableSet(m.keySet());
1375 return keySet;
1376 }
1377
1378 public Set<Map.Entry<K,V>> entrySet() {
1379 if (entrySet==null)
1380 entrySet = new UnmodifiableEntrySet<>(m.entrySet());
1381 return entrySet;
1382 }
1383
1384 public Collection<V> values() {
1385 if (values==null)
1386 values = unmodifiableCollection(m.values());
1387 return values;
1388 }
1389
1390 public boolean equals(Object o) {return o == this || m.equals(o);}
1391 public int hashCode() {return m.hashCode();}
1392 public String toString() {return m.toString();}
1393
1394 /**
1395 * We need this class in addition to UnmodifiableSet as
1396 * Map.Entries themselves permit modification of the backing Map
1397 * via their setValue operation. This class is subtle: there are
1398 * many possible attacks that must be thwarted.
1399 *
1400 * @serial include
1401 */
1402 static class UnmodifiableEntrySet<K,V>
1403 extends UnmodifiableSet<Map.Entry<K,V>> {
1404 private static final long serialVersionUID = 7854390611657943733L;
1405
1406 @SuppressWarnings({"unchecked", "rawtypes"})
1407 UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
1408 // Need to cast to raw in order to work around a limitation in the type system
1409 super((Set)s);
1410 }
1411 public Iterator<Map.Entry<K,V>> iterator() {
1412 return new Iterator<Map.Entry<K,V>>() {
1413 private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
1573
1574 /**
1575 * Returns a synchronized (thread-safe) collection backed by the specified
1576 * collection. In order to guarantee serial access, it is critical that
1577 * <strong>all</strong> access to the backing collection is accomplished
1578 * through the returned collection.<p>
1579 *
1580 * It is imperative that the user manually synchronize on the returned
1581 * collection when iterating over it:
1582 * <pre>
1583 * Collection c = Collections.synchronizedCollection(myCollection);
1584 * ...
1585 * synchronized (c) {
1586 * Iterator i = c.iterator(); // Must be in the synchronized block
1587 * while (i.hasNext())
1588 * foo(i.next());
1589 * }
1590 * </pre>
1591 * Failure to follow this advice may result in non-deterministic behavior.
1592 *
1593 * <p>The returned collection does <i>not</i> pass the <tt>hashCode</tt>
1594 * and <tt>equals</tt> operations through to the backing collection, but
1595 * relies on <tt>Object</tt>'s equals and hashCode methods. This is
1596 * necessary to preserve the contracts of these operations in the case
1597 * that the backing collection is a set or a list.<p>
1598 *
1599 * The returned collection will be serializable if the specified collection
1600 * is serializable.
1601 *
1602 * @param c the collection to be "wrapped" in a synchronized collection.
1603 * @return a synchronized view of the specified collection.
1604 */
1605 public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
1606 return new SynchronizedCollection<>(c);
1607 }
1608
1609 static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
1610 return new SynchronizedCollection<>(c, mutex);
1611 }
1612
1613 /**
1614 * @serial include
1615 */
2090
2091 public Collection<V> values() {
2092 synchronized (mutex) {
2093 if (values==null)
2094 values = new SynchronizedCollection<>(m.values(), mutex);
2095 return values;
2096 }
2097 }
2098
2099 public boolean equals(Object o) {
2100 if (this == o)
2101 return true;
2102 synchronized (mutex) {return m.equals(o);}
2103 }
2104 public int hashCode() {
2105 synchronized (mutex) {return m.hashCode();}
2106 }
2107 public String toString() {
2108 synchronized (mutex) {return m.toString();}
2109 }
2110 private void writeObject(ObjectOutputStream s) throws IOException {
2111 synchronized (mutex) {s.defaultWriteObject();}
2112 }
2113 }
2114
2115 /**
2116 * Returns a synchronized (thread-safe) sorted map backed by the specified
2117 * sorted map. In order to guarantee serial access, it is critical that
2118 * <strong>all</strong> access to the backing sorted map is accomplished
2119 * through the returned sorted map (or its views).<p>
2120 *
2121 * It is imperative that the user manually synchronize on the returned
2122 * sorted map when iterating over any of its collection views, or the
2123 * collections views of any of its <tt>subMap</tt>, <tt>headMap</tt> or
2124 * <tt>tailMap</tt> views.
2125 * <pre>
2126 * SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
2127 * ...
2128 * Set s = m.keySet(); // Needn't be in synchronized block
2129 * ...
2700 /**
2701 * @serial include
2702 */
2703 private static class CheckedMap<K,V>
2704 implements Map<K,V>, Serializable
2705 {
2706 private static final long serialVersionUID = 5742860141034234728L;
2707
2708 private final Map<K, V> m;
2709 final Class<K> keyType;
2710 final Class<V> valueType;
2711
2712 private void typeCheck(Object key, Object value) {
2713 if (key != null && !keyType.isInstance(key))
2714 throw new ClassCastException(badKeyMsg(key));
2715
2716 if (value != null && !valueType.isInstance(value))
2717 throw new ClassCastException(badValueMsg(value));
2718 }
2719
2720 private String badKeyMsg(Object key) {
2721 return "Attempt to insert " + key.getClass() +
2722 " key into map with key type " + keyType;
2723 }
2724
2725 private String badValueMsg(Object value) {
2726 return "Attempt to insert " + value.getClass() +
2727 " value into map with value type " + valueType;
2728 }
2729
2730 CheckedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) {
2731 if (m == null || keyType == null || valueType == null)
2732 throw new NullPointerException();
2733 this.m = m;
2734 this.keyType = keyType;
2735 this.valueType = valueType;
2736 }
2737
2738 public int size() { return m.size(); }
2739 public boolean isEmpty() { return m.isEmpty(); }
2765 for (Object o : entries) {
2766 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2767 Object k = e.getKey();
2768 Object v = e.getValue();
2769 typeCheck(k, v);
2770 checked.add(
2771 new AbstractMap.SimpleImmutableEntry<>((K) k, (V) v));
2772 }
2773 for (Map.Entry<K,V> e : checked)
2774 m.put(e.getKey(), e.getValue());
2775 }
2776
2777 private transient Set<Map.Entry<K,V>> entrySet = null;
2778
2779 public Set<Map.Entry<K,V>> entrySet() {
2780 if (entrySet==null)
2781 entrySet = new CheckedEntrySet<>(m.entrySet(), valueType);
2782 return entrySet;
2783 }
2784
2785 /**
2786 * We need this class in addition to CheckedSet as Map.Entry permits
2787 * modification of the backing Map via the setValue operation. This
2788 * class is subtle: there are many possible attacks that must be
2789 * thwarted.
2790 *
2791 * @serial exclude
2792 */
2793 static class CheckedEntrySet<K,V> implements Set<Map.Entry<K,V>> {
2794 private final Set<Map.Entry<K,V>> s;
2795 private final Class<V> valueType;
2796
2797 CheckedEntrySet(Set<Map.Entry<K, V>> s, Class<V> valueType) {
2798 this.s = s;
2799 this.valueType = valueType;
2800 }
2801
2802 public int size() { return s.size(); }
2803 public boolean isEmpty() { return s.isEmpty(); }
2804 public String toString() { return s.toString(); }
3439 extends AbstractMap<K,V>
3440 implements Serializable
3441 {
3442 private static final long serialVersionUID = 6428348081105594320L;
3443
3444 public int size() {return 0;}
3445 public boolean isEmpty() {return true;}
3446 public boolean containsKey(Object key) {return false;}
3447 public boolean containsValue(Object value) {return false;}
3448 public V get(Object key) {return null;}
3449 public Set<K> keySet() {return emptySet();}
3450 public Collection<V> values() {return emptySet();}
3451 public Set<Map.Entry<K,V>> entrySet() {return emptySet();}
3452
3453 public boolean equals(Object o) {
3454 return (o instanceof Map) && ((Map<?,?>)o).isEmpty();
3455 }
3456
3457 public int hashCode() {return 0;}
3458
3459 // Preserves singleton property
3460 private Object readResolve() {
3461 return EMPTY_MAP;
3462 }
3463 }
3464
3465 // Singleton collections
3466
3467 /**
3468 * Returns an immutable set containing only the specified object.
3469 * The returned set is serializable.
3470 *
3471 * @param o the sole object to be stored in the returned set.
3472 * @return an immutable set containing only the specified object.
3473 */
3474 public static <T> Set<T> singleton(T o) {
3475 return new SingletonSet<>(o);
3476 }
3477
3478 static <E> Iterator<E> singletonIterator(final E e) {
3602
3603 public Set<K> keySet() {
3604 if (keySet==null)
3605 keySet = singleton(k);
3606 return keySet;
3607 }
3608
3609 public Set<Map.Entry<K,V>> entrySet() {
3610 if (entrySet==null)
3611 entrySet = Collections.<Map.Entry<K,V>>singleton(
3612 new SimpleImmutableEntry<>(k, v));
3613 return entrySet;
3614 }
3615
3616 public Collection<V> values() {
3617 if (values==null)
3618 values = singleton(v);
3619 return values;
3620 }
3621
3622 }
3623
3624 // Miscellaneous
3625
3626 /**
3627 * Returns an immutable list consisting of <tt>n</tt> copies of the
3628 * specified object. The newly allocated data object is tiny (it contains
3629 * a single reference to the data object). This method is useful in
3630 * combination with the <tt>List.addAll</tt> method to grow lists.
3631 * The returned list is serializable.
3632 *
3633 * @param n the number of elements in the returned list.
3634 * @param o the element to appear repeatedly in the returned list.
3635 * @return an immutable list consisting of <tt>n</tt> copies of the
3636 * specified object.
3637 * @throws IllegalArgumentException if {@code n < 0}
3638 * @see List#addAll(Collection)
3639 * @see List#addAll(int, Collection)
3640 */
3641 public static <T> List<T> nCopies(int n, T o) {
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27 import java.io.Serializable;
28 import java.io.ObjectOutputStream;
29 import java.io.IOException;
30 import java.lang.reflect.Array;
31 import java.util.function.BiConsumer;
32 import java.util.function.BiFunction;
33 import java.util.function.Function;
34
35 /**
36 * This class consists exclusively of static methods that operate on or return
37 * collections. It contains polymorphic algorithms that operate on
38 * collections, "wrappers", which return a new collection backed by a
39 * specified collection, and a few other odds and ends.
40 *
41 * <p>The methods of this class all throw a <tt>NullPointerException</tt>
42 * if the collections or class objects provided to them are null.
43 *
44 * <p>The documentation for the polymorphic algorithms contained in this class
45 * generally includes a brief description of the <i>implementation</i>. Such
46 * descriptions should be regarded as <i>implementation notes</i>, rather than
47 * parts of the <i>specification</i>. Implementors should feel free to
48 * substitute other algorithms, so long as the specification itself is adhered
49 * to. (For example, the algorithm used by <tt>sort</tt> does not have to be
50 * a mergesort, but it does have to be <i>stable</i>.)
51 *
52 * <p>The "destructive" algorithms contained in this class, that is, the
53 * algorithms that modify the collection on which they operate, are specified
1377 keySet = unmodifiableSet(m.keySet());
1378 return keySet;
1379 }
1380
1381 public Set<Map.Entry<K,V>> entrySet() {
1382 if (entrySet==null)
1383 entrySet = new UnmodifiableEntrySet<>(m.entrySet());
1384 return entrySet;
1385 }
1386
1387 public Collection<V> values() {
1388 if (values==null)
1389 values = unmodifiableCollection(m.values());
1390 return values;
1391 }
1392
1393 public boolean equals(Object o) {return o == this || m.equals(o);}
1394 public int hashCode() {return m.hashCode();}
1395 public String toString() {return m.toString();}
1396
1397 // Override default methods in Map
1398 @Override
1399 @SuppressWarnings("unchecked")
1400 public V getOrDefault(Object k, V defaultValue) {
1401 // Safe cast as we don't change the value
1402 return ((Map<K, V>) m).getOrDefault(k, defaultValue);
1403 }
1404
1405 @Override
1406 public void forEach(BiConsumer<? super K, ? super V> action) {
1407 m.forEach(action);
1408 }
1409
1410 @Override
1411 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
1412 throw new UnsupportedOperationException();
1413 }
1414
1415 @Override
1416 public V putIfAbsent(K key, V value) {
1417 throw new UnsupportedOperationException();
1418 }
1419
1420 @Override
1421 public boolean remove(Object key, Object value) {
1422 throw new UnsupportedOperationException();
1423 }
1424
1425 @Override
1426 public boolean replace(K key, V oldValue, V newValue) {
1427 throw new UnsupportedOperationException();
1428 }
1429
1430 @Override
1431 public V replace(K key, V value) {
1432 throw new UnsupportedOperationException();
1433 }
1434
1435 @Override
1436 public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
1437 throw new UnsupportedOperationException();
1438 }
1439
1440 @Override
1441 public V computeIfPresent(K key,
1442 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1443 throw new UnsupportedOperationException();
1444 }
1445
1446 @Override
1447 public V compute(K key,
1448 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1449 throw new UnsupportedOperationException();
1450 }
1451
1452 @Override
1453 public V merge(K key, V value,
1454 BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1455 throw new UnsupportedOperationException();
1456 }
1457
1458 /**
1459 * We need this class in addition to UnmodifiableSet as
1460 * Map.Entries themselves permit modification of the backing Map
1461 * via their setValue operation. This class is subtle: there are
1462 * many possible attacks that must be thwarted.
1463 *
1464 * @serial include
1465 */
1466 static class UnmodifiableEntrySet<K,V>
1467 extends UnmodifiableSet<Map.Entry<K,V>> {
1468 private static final long serialVersionUID = 7854390611657943733L;
1469
1470 @SuppressWarnings({"unchecked", "rawtypes"})
1471 UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
1472 // Need to cast to raw in order to work around a limitation in the type system
1473 super((Set)s);
1474 }
1475 public Iterator<Map.Entry<K,V>> iterator() {
1476 return new Iterator<Map.Entry<K,V>>() {
1477 private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
1637
1638 /**
1639 * Returns a synchronized (thread-safe) collection backed by the specified
1640 * collection. In order to guarantee serial access, it is critical that
1641 * <strong>all</strong> access to the backing collection is accomplished
1642 * through the returned collection.<p>
1643 *
1644 * It is imperative that the user manually synchronize on the returned
1645 * collection when iterating over it:
1646 * <pre>
1647 * Collection c = Collections.synchronizedCollection(myCollection);
1648 * ...
1649 * synchronized (c) {
1650 * Iterator i = c.iterator(); // Must be in the synchronized block
1651 * while (i.hasNext())
1652 * foo(i.next());
1653 * }
1654 * </pre>
1655 * Failure to follow this advice may result in non-deterministic behavior.
1656 *
1657 * <p>The returned collection does <i>not</i> pass the {@code hashCode}
1658 * and {@code equals} operations through to the backing collection, but
1659 * relies on {@code Object}'s equals and hashCode methods. This is
1660 * necessary to preserve the contracts of these operations in the case
1661 * that the backing collection is a set or a list.<p>
1662 *
1663 * The returned collection will be serializable if the specified collection
1664 * is serializable.
1665 *
1666 * @param c the collection to be "wrapped" in a synchronized collection.
1667 * @return a synchronized view of the specified collection.
1668 */
1669 public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
1670 return new SynchronizedCollection<>(c);
1671 }
1672
1673 static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
1674 return new SynchronizedCollection<>(c, mutex);
1675 }
1676
1677 /**
1678 * @serial include
1679 */
2154
2155 public Collection<V> values() {
2156 synchronized (mutex) {
2157 if (values==null)
2158 values = new SynchronizedCollection<>(m.values(), mutex);
2159 return values;
2160 }
2161 }
2162
2163 public boolean equals(Object o) {
2164 if (this == o)
2165 return true;
2166 synchronized (mutex) {return m.equals(o);}
2167 }
2168 public int hashCode() {
2169 synchronized (mutex) {return m.hashCode();}
2170 }
2171 public String toString() {
2172 synchronized (mutex) {return m.toString();}
2173 }
2174
2175 // Override default methods in Map
2176 @Override
2177 public V getOrDefault(Object k, V defaultValue) {
2178 synchronized (mutex) {return m.getOrDefault(k, defaultValue);}
2179 }
2180 @Override
2181 public void forEach(BiConsumer<? super K, ? super V> action) {
2182 synchronized (mutex) {m.forEach(action);}
2183 }
2184 @Override
2185 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
2186 synchronized (mutex) {m.replaceAll(function);}
2187 }
2188 @Override
2189 public V putIfAbsent(K key, V value) {
2190 synchronized (mutex) {return m.putIfAbsent(key, value);}
2191 }
2192 @Override
2193 public boolean remove(Object key, Object value) {
2194 synchronized (mutex) {return m.remove(key, value);}
2195 }
2196 @Override
2197 public boolean replace(K key, V oldValue, V newValue) {
2198 synchronized (mutex) {return m.replace(key, oldValue, newValue);}
2199 }
2200 @Override
2201 public V replace(K key, V value) {
2202 synchronized (mutex) {return m.replace(key, value);}
2203 }
2204 @Override
2205 public V computeIfAbsent(K key,
2206 Function<? super K, ? extends V> mappingFunction) {
2207 synchronized (mutex) {return m.computeIfAbsent(key, mappingFunction);}
2208 }
2209 @Override
2210 public V computeIfPresent(K key,
2211 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
2212 synchronized (mutex) {return m.computeIfPresent(key, remappingFunction);}
2213 }
2214 @Override
2215 public V compute(K key,
2216 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
2217 synchronized (mutex) {return m.compute(key, remappingFunction);}
2218 }
2219 @Override
2220 public V merge(K key, V value,
2221 BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
2222 synchronized (mutex) {return m.merge(key, value, remappingFunction);}
2223 }
2224
2225 private void writeObject(ObjectOutputStream s) throws IOException {
2226 synchronized (mutex) {s.defaultWriteObject();}
2227 }
2228 }
2229
2230 /**
2231 * Returns a synchronized (thread-safe) sorted map backed by the specified
2232 * sorted map. In order to guarantee serial access, it is critical that
2233 * <strong>all</strong> access to the backing sorted map is accomplished
2234 * through the returned sorted map (or its views).<p>
2235 *
2236 * It is imperative that the user manually synchronize on the returned
2237 * sorted map when iterating over any of its collection views, or the
2238 * collections views of any of its <tt>subMap</tt>, <tt>headMap</tt> or
2239 * <tt>tailMap</tt> views.
2240 * <pre>
2241 * SortedMap m = Collections.synchronizedSortedMap(new TreeMap());
2242 * ...
2243 * Set s = m.keySet(); // Needn't be in synchronized block
2244 * ...
2815 /**
2816 * @serial include
2817 */
2818 private static class CheckedMap<K,V>
2819 implements Map<K,V>, Serializable
2820 {
2821 private static final long serialVersionUID = 5742860141034234728L;
2822
2823 private final Map<K, V> m;
2824 final Class<K> keyType;
2825 final Class<V> valueType;
2826
2827 private void typeCheck(Object key, Object value) {
2828 if (key != null && !keyType.isInstance(key))
2829 throw new ClassCastException(badKeyMsg(key));
2830
2831 if (value != null && !valueType.isInstance(value))
2832 throw new ClassCastException(badValueMsg(value));
2833 }
2834
2835 private BiFunction<? super K, ? super V, ? extends V> typeCheck(
2836 BiFunction<? super K, ? super V, ? extends V> func)
2837 {
2838 Objects.requireNonNull(func);
2839 return (k, v) -> {
2840 V newValue = func.apply(k, v);
2841 typeCheck(k, newValue);
2842 return newValue;
2843 };
2844 }
2845
2846 private String badKeyMsg(Object key) {
2847 return "Attempt to insert " + key.getClass() +
2848 " key into map with key type " + keyType;
2849 }
2850
2851 private String badValueMsg(Object value) {
2852 return "Attempt to insert " + value.getClass() +
2853 " value into map with value type " + valueType;
2854 }
2855
2856 CheckedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType) {
2857 if (m == null || keyType == null || valueType == null)
2858 throw new NullPointerException();
2859 this.m = m;
2860 this.keyType = keyType;
2861 this.valueType = valueType;
2862 }
2863
2864 public int size() { return m.size(); }
2865 public boolean isEmpty() { return m.isEmpty(); }
2891 for (Object o : entries) {
2892 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2893 Object k = e.getKey();
2894 Object v = e.getValue();
2895 typeCheck(k, v);
2896 checked.add(
2897 new AbstractMap.SimpleImmutableEntry<>((K) k, (V) v));
2898 }
2899 for (Map.Entry<K,V> e : checked)
2900 m.put(e.getKey(), e.getValue());
2901 }
2902
2903 private transient Set<Map.Entry<K,V>> entrySet = null;
2904
2905 public Set<Map.Entry<K,V>> entrySet() {
2906 if (entrySet==null)
2907 entrySet = new CheckedEntrySet<>(m.entrySet(), valueType);
2908 return entrySet;
2909 }
2910
2911 // Override default methods in Map
2912 @Override
2913 public void forEach(BiConsumer<? super K, ? super V> action) {
2914 m.forEach(action);
2915 }
2916
2917 @Override
2918 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
2919 m.replaceAll(typeCheck(function));
2920 }
2921
2922 @Override
2923 public V putIfAbsent(K key, V value) {
2924 typeCheck(key, value);
2925 return m.putIfAbsent(key, value);
2926 }
2927
2928 @Override
2929 public boolean remove(Object key, Object value) {
2930 return m.remove(key, value);
2931 }
2932
2933 @Override
2934 public boolean replace(K key, V oldValue, V newValue) {
2935 typeCheck(key, newValue);
2936 return m.replace(key, oldValue, newValue);
2937 }
2938
2939 @Override
2940 public V replace(K key, V value) {
2941 typeCheck(key, value);
2942 return m.replace(key, value);
2943 }
2944
2945 @Override
2946 public V computeIfAbsent(K key,
2947 Function<? super K, ? extends V> mappingFunction) {
2948 Objects.requireNonNull(mappingFunction);
2949 return m.computeIfAbsent(key, k -> {
2950 V value = mappingFunction.apply(k);
2951 typeCheck(k, value);
2952 return value;
2953 });
2954 }
2955
2956 @Override
2957 public V computeIfPresent(K key,
2958 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
2959 return m.computeIfPresent(key, typeCheck(remappingFunction));
2960 }
2961
2962 @Override
2963 public V compute(K key,
2964 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
2965 return m.compute(key, typeCheck(remappingFunction));
2966 }
2967
2968 @Override
2969 public V merge(K key, V value,
2970 BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
2971 Objects.requireNonNull(remappingFunction);
2972 return m.merge(key, value, (v1, v2) -> {
2973 V newValue = remappingFunction.apply(v1, v2);
2974 typeCheck(null, newValue);
2975 return newValue;
2976 });
2977 }
2978
2979 /**
2980 * We need this class in addition to CheckedSet as Map.Entry permits
2981 * modification of the backing Map via the setValue operation. This
2982 * class is subtle: there are many possible attacks that must be
2983 * thwarted.
2984 *
2985 * @serial exclude
2986 */
2987 static class CheckedEntrySet<K,V> implements Set<Map.Entry<K,V>> {
2988 private final Set<Map.Entry<K,V>> s;
2989 private final Class<V> valueType;
2990
2991 CheckedEntrySet(Set<Map.Entry<K, V>> s, Class<V> valueType) {
2992 this.s = s;
2993 this.valueType = valueType;
2994 }
2995
2996 public int size() { return s.size(); }
2997 public boolean isEmpty() { return s.isEmpty(); }
2998 public String toString() { return s.toString(); }
3633 extends AbstractMap<K,V>
3634 implements Serializable
3635 {
3636 private static final long serialVersionUID = 6428348081105594320L;
3637
3638 public int size() {return 0;}
3639 public boolean isEmpty() {return true;}
3640 public boolean containsKey(Object key) {return false;}
3641 public boolean containsValue(Object value) {return false;}
3642 public V get(Object key) {return null;}
3643 public Set<K> keySet() {return emptySet();}
3644 public Collection<V> values() {return emptySet();}
3645 public Set<Map.Entry<K,V>> entrySet() {return emptySet();}
3646
3647 public boolean equals(Object o) {
3648 return (o instanceof Map) && ((Map<?,?>)o).isEmpty();
3649 }
3650
3651 public int hashCode() {return 0;}
3652
3653 // Override default methods in Map
3654 @Override
3655 @SuppressWarnings("unchecked")
3656 public V getOrDefault(Object k, V defaultValue) {
3657 return defaultValue;
3658 }
3659
3660 @Override
3661 public void forEach(BiConsumer<? super K, ? super V> action) {
3662 Objects.requireNonNull(action);
3663 }
3664
3665 @Override
3666 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
3667 Objects.requireNonNull(function);
3668 }
3669
3670 @Override
3671 public V putIfAbsent(K key, V value) {
3672 throw new UnsupportedOperationException();
3673 }
3674
3675 @Override
3676 public boolean remove(Object key, Object value) {
3677 throw new UnsupportedOperationException();
3678 }
3679
3680 @Override
3681 public boolean replace(K key, V oldValue, V newValue) {
3682 throw new UnsupportedOperationException();
3683 }
3684
3685 @Override
3686 public V replace(K key, V value) {
3687 throw new UnsupportedOperationException();
3688 }
3689
3690 @Override
3691 public V computeIfAbsent(K key,
3692 Function<? super K, ? extends V> mappingFunction) {
3693 throw new UnsupportedOperationException();
3694 }
3695
3696 @Override
3697 public V computeIfPresent(K key,
3698 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
3699 throw new UnsupportedOperationException();
3700 }
3701
3702 @Override
3703 public V compute(K key,
3704 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
3705 throw new UnsupportedOperationException();
3706 }
3707
3708 @Override
3709 public V merge(K key, V value,
3710 BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
3711 throw new UnsupportedOperationException();
3712 }
3713
3714 // Preserves singleton property
3715 private Object readResolve() {
3716 return EMPTY_MAP;
3717 }
3718 }
3719
3720 // Singleton collections
3721
3722 /**
3723 * Returns an immutable set containing only the specified object.
3724 * The returned set is serializable.
3725 *
3726 * @param o the sole object to be stored in the returned set.
3727 * @return an immutable set containing only the specified object.
3728 */
3729 public static <T> Set<T> singleton(T o) {
3730 return new SingletonSet<>(o);
3731 }
3732
3733 static <E> Iterator<E> singletonIterator(final E e) {
3857
3858 public Set<K> keySet() {
3859 if (keySet==null)
3860 keySet = singleton(k);
3861 return keySet;
3862 }
3863
3864 public Set<Map.Entry<K,V>> entrySet() {
3865 if (entrySet==null)
3866 entrySet = Collections.<Map.Entry<K,V>>singleton(
3867 new SimpleImmutableEntry<>(k, v));
3868 return entrySet;
3869 }
3870
3871 public Collection<V> values() {
3872 if (values==null)
3873 values = singleton(v);
3874 return values;
3875 }
3876
3877 // Override default methods in Map
3878 @Override
3879 public V getOrDefault(Object key, V defaultValue) {
3880 return eq(key, k) ? v : defaultValue;
3881 }
3882
3883 @Override
3884 public void forEach(BiConsumer<? super K, ? super V> action) {
3885 action.accept(k, v);
3886 }
3887
3888 @Override
3889 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
3890 throw new UnsupportedOperationException();
3891 }
3892
3893 @Override
3894 public V putIfAbsent(K key, V value) {
3895 throw new UnsupportedOperationException();
3896 }
3897
3898 @Override
3899 public boolean remove(Object key, Object value) {
3900 throw new UnsupportedOperationException();
3901 }
3902
3903 @Override
3904 public boolean replace(K key, V oldValue, V newValue) {
3905 throw new UnsupportedOperationException();
3906 }
3907
3908 @Override
3909 public V replace(K key, V value) {
3910 throw new UnsupportedOperationException();
3911 }
3912
3913 @Override
3914 public V computeIfAbsent(K key,
3915 Function<? super K, ? extends V> mappingFunction) {
3916 throw new UnsupportedOperationException();
3917 }
3918
3919 @Override
3920 public V computeIfPresent(K key,
3921 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
3922 throw new UnsupportedOperationException();
3923 }
3924
3925 @Override
3926 public V compute(K key,
3927 BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
3928 throw new UnsupportedOperationException();
3929 }
3930
3931 @Override
3932 public V merge(K key, V value,
3933 BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
3934 throw new UnsupportedOperationException();
3935 }
3936 }
3937
3938 // Miscellaneous
3939
3940 /**
3941 * Returns an immutable list consisting of <tt>n</tt> copies of the
3942 * specified object. The newly allocated data object is tiny (it contains
3943 * a single reference to the data object). This method is useful in
3944 * combination with the <tt>List.addAll</tt> method to grow lists.
3945 * The returned list is serializable.
3946 *
3947 * @param n the number of elements in the returned list.
3948 * @param o the element to appear repeatedly in the returned list.
3949 * @return an immutable list consisting of <tt>n</tt> copies of the
3950 * specified object.
3951 * @throws IllegalArgumentException if {@code n < 0}
3952 * @see List#addAll(Collection)
3953 * @see List#addAll(int, Collection)
3954 */
3955 public static <T> List<T> nCopies(int n, T o) {
|