1007 *
1008 * The returned collection will be serializable if the specified collection
1009 * is serializable.
1010 *
1011 * @param <T> the class of the objects in the collection
1012 * @param c the collection for which an unmodifiable view is to be
1013 * returned.
1014 * @return an unmodifiable view of the specified collection.
1015 */
1016 public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
1017 return new UnmodifiableCollection<>(c);
1018 }
1019
1020 /**
1021 * @serial include
1022 */
1023 static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
1024 @java.io.Serial
1025 private static final long serialVersionUID = 1820017752578914078L;
1026
1027 final Collection<? extends E> c;
1028
1029 UnmodifiableCollection(Collection<? extends E> c) {
1030 if (c==null)
1031 throw new NullPointerException();
1032 this.c = c;
1033 }
1034
1035 public int size() {return c.size();}
1036 public boolean isEmpty() {return c.isEmpty();}
1037 public boolean contains(Object o) {return c.contains(o);}
1038 public Object[] toArray() {return c.toArray();}
1039 public <T> T[] toArray(T[] a) {return c.toArray(a);}
1040 public <T> T[] toArray(IntFunction<T[]> f) {return c.toArray(f);}
1041 public String toString() {return c.toString();}
1042
1043 public Iterator<E> iterator() {
1044 return new Iterator<E>() {
1045 private final Iterator<? extends E> i = c.iterator();
1046
1147 * The returned sorted set will be serializable if the specified sorted set
1148 * is serializable.
1149 *
1150 * @param <T> the class of the objects in the set
1151 * @param s the sorted set for which an unmodifiable view is to be
1152 * returned.
1153 * @return an unmodifiable view of the specified sorted set.
1154 */
1155 public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
1156 return new UnmodifiableSortedSet<>(s);
1157 }
1158
1159 /**
1160 * @serial include
1161 */
1162 static class UnmodifiableSortedSet<E>
1163 extends UnmodifiableSet<E>
1164 implements SortedSet<E>, Serializable {
1165 @java.io.Serial
1166 private static final long serialVersionUID = -4929149591599911165L;
1167 private final SortedSet<E> ss;
1168
1169 UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
1170
1171 public Comparator<? super E> comparator() {return ss.comparator();}
1172
1173 public SortedSet<E> subSet(E fromElement, E toElement) {
1174 return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement));
1175 }
1176 public SortedSet<E> headSet(E toElement) {
1177 return new UnmodifiableSortedSet<>(ss.headSet(toElement));
1178 }
1179 public SortedSet<E> tailSet(E fromElement) {
1180 return new UnmodifiableSortedSet<>(ss.tailSet(fromElement));
1181 }
1182
1183 public E first() {return ss.first();}
1184 public E last() {return ss.last();}
1185 }
1186
1227 private static class EmptyNavigableSet<E> extends UnmodifiableNavigableSet<E>
1228 implements Serializable {
1229 @java.io.Serial
1230 private static final long serialVersionUID = -6291252904449939134L;
1231
1232 public EmptyNavigableSet() {
1233 super(new TreeSet<>());
1234 }
1235
1236 @java.io.Serial
1237 private Object readResolve() { return EMPTY_NAVIGABLE_SET; }
1238 }
1239
1240 @SuppressWarnings("rawtypes")
1241 private static final NavigableSet<?> EMPTY_NAVIGABLE_SET =
1242 new EmptyNavigableSet<>();
1243
1244 /**
1245 * The instance we are protecting.
1246 */
1247 private final NavigableSet<E> ns;
1248
1249 UnmodifiableNavigableSet(NavigableSet<E> s) {super(s); ns = s;}
1250
1251 public E lower(E e) { return ns.lower(e); }
1252 public E floor(E e) { return ns.floor(e); }
1253 public E ceiling(E e) { return ns.ceiling(e); }
1254 public E higher(E e) { return ns.higher(e); }
1255 public E pollFirst() { throw new UnsupportedOperationException(); }
1256 public E pollLast() { throw new UnsupportedOperationException(); }
1257 public NavigableSet<E> descendingSet()
1258 { return new UnmodifiableNavigableSet<>(ns.descendingSet()); }
1259 public Iterator<E> descendingIterator()
1260 { return descendingSet().iterator(); }
1261
1262 public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
1263 return new UnmodifiableNavigableSet<>(
1264 ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
1265 }
1266
1287 * {@link RandomAccess} if the specified list does.
1288 *
1289 * @param <T> the class of the objects in the list
1290 * @param list the list for which an unmodifiable view is to be returned.
1291 * @return an unmodifiable view of the specified list.
1292 */
1293 public static <T> List<T> unmodifiableList(List<? extends T> list) {
1294 return (list instanceof RandomAccess ?
1295 new UnmodifiableRandomAccessList<>(list) :
1296 new UnmodifiableList<>(list));
1297 }
1298
1299 /**
1300 * @serial include
1301 */
1302 static class UnmodifiableList<E> extends UnmodifiableCollection<E>
1303 implements List<E> {
1304 @java.io.Serial
1305 private static final long serialVersionUID = -283967356065247728L;
1306
1307 final List<? extends E> list;
1308
1309 UnmodifiableList(List<? extends E> list) {
1310 super(list);
1311 this.list = list;
1312 }
1313
1314 public boolean equals(Object o) {return o == this || list.equals(o);}
1315 public int hashCode() {return list.hashCode();}
1316
1317 public E get(int index) {return list.get(index);}
1318 public E set(int index, E element) {
1319 throw new UnsupportedOperationException();
1320 }
1321 public void add(int index, E element) {
1322 throw new UnsupportedOperationException();
1323 }
1324 public E remove(int index) {
1325 throw new UnsupportedOperationException();
1326 }
1433 *
1434 * The returned map will be serializable if the specified map
1435 * is serializable.
1436 *
1437 * @param <K> the class of the map keys
1438 * @param <V> the class of the map values
1439 * @param m the map for which an unmodifiable view is to be returned.
1440 * @return an unmodifiable view of the specified map.
1441 */
1442 public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
1443 return new UnmodifiableMap<>(m);
1444 }
1445
1446 /**
1447 * @serial include
1448 */
1449 private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable {
1450 @java.io.Serial
1451 private static final long serialVersionUID = -1034234728574286014L;
1452
1453 private final Map<? extends K, ? extends V> m;
1454
1455 UnmodifiableMap(Map<? extends K, ? extends V> m) {
1456 if (m==null)
1457 throw new NullPointerException();
1458 this.m = m;
1459 }
1460
1461 public int size() {return m.size();}
1462 public boolean isEmpty() {return m.isEmpty();}
1463 public boolean containsKey(Object key) {return m.containsKey(key);}
1464 public boolean containsValue(Object val) {return m.containsValue(val);}
1465 public V get(Object key) {return m.get(key);}
1466
1467 public V put(K key, V value) {
1468 throw new UnsupportedOperationException();
1469 }
1470 public V remove(Object key) {
1471 throw new UnsupportedOperationException();
1472 }
1792 *
1793 * @param <K> the class of the map keys
1794 * @param <V> the class of the map values
1795 * @param m the sorted map for which an unmodifiable view is to be
1796 * returned.
1797 * @return an unmodifiable view of the specified sorted map.
1798 */
1799 public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
1800 return new UnmodifiableSortedMap<>(m);
1801 }
1802
1803 /**
1804 * @serial include
1805 */
1806 static class UnmodifiableSortedMap<K,V>
1807 extends UnmodifiableMap<K,V>
1808 implements SortedMap<K,V>, Serializable {
1809 @java.io.Serial
1810 private static final long serialVersionUID = -8806743815996713206L;
1811
1812 private final SortedMap<K, ? extends V> sm;
1813
1814 UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; }
1815 public Comparator<? super K> comparator() { return sm.comparator(); }
1816 public SortedMap<K,V> subMap(K fromKey, K toKey)
1817 { return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); }
1818 public SortedMap<K,V> headMap(K toKey)
1819 { return new UnmodifiableSortedMap<>(sm.headMap(toKey)); }
1820 public SortedMap<K,V> tailMap(K fromKey)
1821 { return new UnmodifiableSortedMap<>(sm.tailMap(fromKey)); }
1822 public K firstKey() { return sm.firstKey(); }
1823 public K lastKey() { return sm.lastKey(); }
1824 }
1825
1826 /**
1827 * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1828 * specified navigable map. Query operations on the returned navigable map "read
1829 * through" to the specified navigable map. Attempts to modify the returned
1830 * navigable map, whether direct, via its collection views, or via its
1831 * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
1869
1870 EmptyNavigableMap() { super(new TreeMap<>()); }
1871
1872 @Override
1873 public NavigableSet<K> navigableKeySet()
1874 { return emptyNavigableSet(); }
1875
1876 @java.io.Serial
1877 private Object readResolve() { return EMPTY_NAVIGABLE_MAP; }
1878 }
1879
1880 /**
1881 * Singleton for {@link emptyNavigableMap()} which is also immutable.
1882 */
1883 private static final EmptyNavigableMap<?,?> EMPTY_NAVIGABLE_MAP =
1884 new EmptyNavigableMap<>();
1885
1886 /**
1887 * The instance we wrap and protect.
1888 */
1889 private final NavigableMap<K, ? extends V> nm;
1890
1891 UnmodifiableNavigableMap(NavigableMap<K, ? extends V> m)
1892 {super(m); nm = m;}
1893
1894 public K lowerKey(K key) { return nm.lowerKey(key); }
1895 public K floorKey(K key) { return nm.floorKey(key); }
1896 public K ceilingKey(K key) { return nm.ceilingKey(key); }
1897 public K higherKey(K key) { return nm.higherKey(key); }
1898
1899 @SuppressWarnings("unchecked")
1900 public Entry<K, V> lowerEntry(K key) {
1901 Entry<K,V> lower = (Entry<K, V>) nm.lowerEntry(key);
1902 return (null != lower)
1903 ? new UnmodifiableEntrySet.UnmodifiableEntry<>(lower)
1904 : null;
1905 }
1906
1907 @SuppressWarnings("unchecked")
1908 public Entry<K, V> floorEntry(K key) {
2000 *
2001 * @param <T> the class of the objects in the collection
2002 * @param c the collection to be "wrapped" in a synchronized collection.
2003 * @return a synchronized view of the specified collection.
2004 */
2005 public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
2006 return new SynchronizedCollection<>(c);
2007 }
2008
2009 static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
2010 return new SynchronizedCollection<>(c, mutex);
2011 }
2012
2013 /**
2014 * @serial include
2015 */
2016 static class SynchronizedCollection<E> implements Collection<E>, Serializable {
2017 @java.io.Serial
2018 private static final long serialVersionUID = 3053995032091335093L;
2019
2020 final Collection<E> c; // Backing Collection
2021 final Object mutex; // Object on which to synchronize
2022
2023 SynchronizedCollection(Collection<E> c) {
2024 this.c = Objects.requireNonNull(c);
2025 mutex = this;
2026 }
2027
2028 SynchronizedCollection(Collection<E> c, Object mutex) {
2029 this.c = Objects.requireNonNull(c);
2030 this.mutex = Objects.requireNonNull(mutex);
2031 }
2032
2033 public int size() {
2034 synchronized (mutex) {return c.size();}
2035 }
2036 public boolean isEmpty() {
2037 synchronized (mutex) {return c.isEmpty();}
2038 }
2039 public boolean contains(Object o) {
2040 synchronized (mutex) {return c.contains(o);}
2202 * sorted set is serializable.
2203 *
2204 * @param <T> the class of the objects in the set
2205 * @param s the sorted set to be "wrapped" in a synchronized sorted set.
2206 * @return a synchronized view of the specified sorted set.
2207 */
2208 public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
2209 return new SynchronizedSortedSet<>(s);
2210 }
2211
2212 /**
2213 * @serial include
2214 */
2215 static class SynchronizedSortedSet<E>
2216 extends SynchronizedSet<E>
2217 implements SortedSet<E>
2218 {
2219 @java.io.Serial
2220 private static final long serialVersionUID = 8695801310862127406L;
2221
2222 private final SortedSet<E> ss;
2223
2224 SynchronizedSortedSet(SortedSet<E> s) {
2225 super(s);
2226 ss = s;
2227 }
2228 SynchronizedSortedSet(SortedSet<E> s, Object mutex) {
2229 super(s, mutex);
2230 ss = s;
2231 }
2232
2233 public Comparator<? super E> comparator() {
2234 synchronized (mutex) {return ss.comparator();}
2235 }
2236
2237 public SortedSet<E> subSet(E fromElement, E toElement) {
2238 synchronized (mutex) {
2239 return new SynchronizedSortedSet<>(
2240 ss.subSet(fromElement, toElement), mutex);
2241 }
2297 * @param <T> the class of the objects in the set
2298 * @param s the navigable set to be "wrapped" in a synchronized navigable
2299 * set
2300 * @return a synchronized view of the specified navigable set
2301 * @since 1.8
2302 */
2303 public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) {
2304 return new SynchronizedNavigableSet<>(s);
2305 }
2306
2307 /**
2308 * @serial include
2309 */
2310 static class SynchronizedNavigableSet<E>
2311 extends SynchronizedSortedSet<E>
2312 implements NavigableSet<E>
2313 {
2314 @java.io.Serial
2315 private static final long serialVersionUID = -5505529816273629798L;
2316
2317 private final NavigableSet<E> ns;
2318
2319 SynchronizedNavigableSet(NavigableSet<E> s) {
2320 super(s);
2321 ns = s;
2322 }
2323
2324 SynchronizedNavigableSet(NavigableSet<E> s, Object mutex) {
2325 super(s, mutex);
2326 ns = s;
2327 }
2328 public E lower(E e) { synchronized (mutex) {return ns.lower(e);} }
2329 public E floor(E e) { synchronized (mutex) {return ns.floor(e);} }
2330 public E ceiling(E e) { synchronized (mutex) {return ns.ceiling(e);} }
2331 public E higher(E e) { synchronized (mutex) {return ns.higher(e);} }
2332 public E pollFirst() { synchronized (mutex) {return ns.pollFirst();} }
2333 public E pollLast() { synchronized (mutex) {return ns.pollLast();} }
2334
2335 public NavigableSet<E> descendingSet() {
2336 synchronized (mutex) {
2407 return (list instanceof RandomAccess ?
2408 new SynchronizedRandomAccessList<>(list) :
2409 new SynchronizedList<>(list));
2410 }
2411
2412 static <T> List<T> synchronizedList(List<T> list, Object mutex) {
2413 return (list instanceof RandomAccess ?
2414 new SynchronizedRandomAccessList<>(list, mutex) :
2415 new SynchronizedList<>(list, mutex));
2416 }
2417
2418 /**
2419 * @serial include
2420 */
2421 static class SynchronizedList<E>
2422 extends SynchronizedCollection<E>
2423 implements List<E> {
2424 @java.io.Serial
2425 private static final long serialVersionUID = -7754090372962971524L;
2426
2427 final List<E> list;
2428
2429 SynchronizedList(List<E> list) {
2430 super(list);
2431 this.list = list;
2432 }
2433 SynchronizedList(List<E> list, Object mutex) {
2434 super(list, mutex);
2435 this.list = list;
2436 }
2437
2438 public boolean equals(Object o) {
2439 if (this == o)
2440 return true;
2441 synchronized (mutex) {return list.equals(o);}
2442 }
2443 public int hashCode() {
2444 synchronized (mutex) {return list.hashCode();}
2445 }
2446
2574 * <p>The returned map will be serializable if the specified map is
2575 * serializable.
2576 *
2577 * @param <K> the class of the map keys
2578 * @param <V> the class of the map values
2579 * @param m the map to be "wrapped" in a synchronized map.
2580 * @return a synchronized view of the specified map.
2581 */
2582 public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
2583 return new SynchronizedMap<>(m);
2584 }
2585
2586 /**
2587 * @serial include
2588 */
2589 private static class SynchronizedMap<K,V>
2590 implements Map<K,V>, Serializable {
2591 @java.io.Serial
2592 private static final long serialVersionUID = 1978198479659022715L;
2593
2594 private final Map<K,V> m; // Backing Map
2595 final Object mutex; // Object on which to synchronize
2596
2597 SynchronizedMap(Map<K,V> m) {
2598 this.m = Objects.requireNonNull(m);
2599 mutex = this;
2600 }
2601
2602 SynchronizedMap(Map<K,V> m, Object mutex) {
2603 this.m = m;
2604 this.mutex = mutex;
2605 }
2606
2607 public int size() {
2608 synchronized (mutex) {return m.size();}
2609 }
2610 public boolean isEmpty() {
2611 synchronized (mutex) {return m.isEmpty();}
2612 }
2613 public boolean containsKey(Object key) {
2614 synchronized (mutex) {return m.containsKey(key);}
2771 *
2772 * @param <K> the class of the map keys
2773 * @param <V> the class of the map values
2774 * @param m the sorted map to be "wrapped" in a synchronized sorted map.
2775 * @return a synchronized view of the specified sorted map.
2776 */
2777 public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
2778 return new SynchronizedSortedMap<>(m);
2779 }
2780
2781 /**
2782 * @serial include
2783 */
2784 static class SynchronizedSortedMap<K,V>
2785 extends SynchronizedMap<K,V>
2786 implements SortedMap<K,V>
2787 {
2788 @java.io.Serial
2789 private static final long serialVersionUID = -8798146769416483793L;
2790
2791 private final SortedMap<K,V> sm;
2792
2793 SynchronizedSortedMap(SortedMap<K,V> m) {
2794 super(m);
2795 sm = m;
2796 }
2797 SynchronizedSortedMap(SortedMap<K,V> m, Object mutex) {
2798 super(m, mutex);
2799 sm = m;
2800 }
2801
2802 public Comparator<? super K> comparator() {
2803 synchronized (mutex) {return sm.comparator();}
2804 }
2805
2806 public SortedMap<K,V> subMap(K fromKey, K toKey) {
2807 synchronized (mutex) {
2808 return new SynchronizedSortedMap<>(
2809 sm.subMap(fromKey, toKey), mutex);
2810 }
2874 * map
2875 * @return a synchronized view of the specified navigable map.
2876 * @since 1.8
2877 */
2878 public static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) {
2879 return new SynchronizedNavigableMap<>(m);
2880 }
2881
2882 /**
2883 * A synchronized NavigableMap.
2884 *
2885 * @serial include
2886 */
2887 static class SynchronizedNavigableMap<K,V>
2888 extends SynchronizedSortedMap<K,V>
2889 implements NavigableMap<K,V>
2890 {
2891 @java.io.Serial
2892 private static final long serialVersionUID = 699392247599746807L;
2893
2894 private final NavigableMap<K,V> nm;
2895
2896 SynchronizedNavigableMap(NavigableMap<K,V> m) {
2897 super(m);
2898 nm = m;
2899 }
2900 SynchronizedNavigableMap(NavigableMap<K,V> m, Object mutex) {
2901 super(m, mutex);
2902 nm = m;
2903 }
2904
2905 public Entry<K, V> lowerEntry(K key)
2906 { synchronized (mutex) { return nm.lowerEntry(key); } }
2907 public K lowerKey(K key)
2908 { synchronized (mutex) { return nm.lowerKey(key); } }
2909 public Entry<K, V> floorEntry(K key)
2910 { synchronized (mutex) { return nm.floorEntry(key); } }
2911 public K floorKey(K key)
2912 { synchronized (mutex) { return nm.floorKey(key); } }
2913 public Entry<K, V> ceilingEntry(K key)
3053 * @return a dynamically typesafe view of the specified collection
3054 * @since 1.5
3055 */
3056 public static <E> Collection<E> checkedCollection(Collection<E> c,
3057 Class<E> type) {
3058 return new CheckedCollection<>(c, type);
3059 }
3060
3061 @SuppressWarnings("unchecked")
3062 static <T> T[] zeroLengthArray(Class<T> type) {
3063 return (T[]) Array.newInstance(type, 0);
3064 }
3065
3066 /**
3067 * @serial include
3068 */
3069 static class CheckedCollection<E> implements Collection<E>, Serializable {
3070 @java.io.Serial
3071 private static final long serialVersionUID = 1578914078182001775L;
3072
3073 final Collection<E> c;
3074 final Class<E> type;
3075
3076 @SuppressWarnings("unchecked")
3077 E typeCheck(Object o) {
3078 if (o != null && !type.isInstance(o))
3079 throw new ClassCastException(badElementMsg(o));
3080 return (E) o;
3081 }
3082
3083 private String badElementMsg(Object o) {
3084 return "Attempt to insert " + o.getClass() +
3085 " element into collection with element type " + type;
3086 }
3087
3088 CheckedCollection(Collection<E> c, Class<E> type) {
3089 this.c = Objects.requireNonNull(c, "c");
3090 this.type = Objects.requireNonNull(type, "type");
3091 }
3092
3093 public int size() { return c.size(); }
3109 public boolean retainAll(Collection<?> coll) {
3110 return c.retainAll(coll);
3111 }
3112
3113 public Iterator<E> iterator() {
3114 // JDK-6363904 - unwrapped iterator could be typecast to
3115 // ListIterator with unsafe set()
3116 final Iterator<E> it = c.iterator();
3117 return new Iterator<E>() {
3118 public boolean hasNext() { return it.hasNext(); }
3119 public E next() { return it.next(); }
3120 public void remove() { it.remove(); }
3121 public void forEachRemaining(Consumer<? super E> action) {
3122 it.forEachRemaining(action);
3123 }
3124 };
3125 }
3126
3127 public boolean add(E e) { return c.add(typeCheck(e)); }
3128
3129 private E[] zeroLengthElementArray; // Lazily initialized
3130
3131 private E[] zeroLengthElementArray() {
3132 return zeroLengthElementArray != null ? zeroLengthElementArray :
3133 (zeroLengthElementArray = zeroLengthArray(type));
3134 }
3135
3136 @SuppressWarnings("unchecked")
3137 Collection<E> checkedCopyOf(Collection<? extends E> coll) {
3138 Object[] a;
3139 try {
3140 E[] z = zeroLengthElementArray();
3141 a = coll.toArray(z);
3142 // Defend against coll violating the toArray contract
3143 if (a.getClass() != z.getClass())
3144 a = Arrays.copyOf(a, a.length, z.getClass());
3145 } catch (ArrayStoreException ignore) {
3146 // To get better and consistent diagnostics,
3147 // we call typeCheck explicitly on each element.
3148 // We call clone() to defend against coll retaining a
3202 * @param <E> the class of the objects in the queue
3203 * @param queue the queue for which a dynamically typesafe view is to be
3204 * returned
3205 * @param type the type of element that {@code queue} is permitted to hold
3206 * @return a dynamically typesafe view of the specified queue
3207 * @since 1.8
3208 */
3209 public static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type) {
3210 return new CheckedQueue<>(queue, type);
3211 }
3212
3213 /**
3214 * @serial include
3215 */
3216 static class CheckedQueue<E>
3217 extends CheckedCollection<E>
3218 implements Queue<E>, Serializable
3219 {
3220 @java.io.Serial
3221 private static final long serialVersionUID = 1433151992604707767L;
3222 final Queue<E> queue;
3223
3224 CheckedQueue(Queue<E> queue, Class<E> elementType) {
3225 super(queue, elementType);
3226 this.queue = queue;
3227 }
3228
3229 public E element() {return queue.element();}
3230 public boolean equals(Object o) {return o == this || c.equals(o);}
3231 public int hashCode() {return c.hashCode();}
3232 public E peek() {return queue.peek();}
3233 public E poll() {return queue.poll();}
3234 public E remove() {return queue.remove();}
3235 public boolean offer(E e) {return queue.offer(typeCheck(e));}
3236 }
3237
3238 /**
3239 * Returns a dynamically typesafe view of the specified set.
3240 * Any attempt to insert an element of the wrong type will result in
3241 * an immediate {@link ClassCastException}. Assuming a set contains
3306 * @param s the sorted set for which a dynamically typesafe view is to be
3307 * returned
3308 * @param type the type of element that {@code s} is permitted to hold
3309 * @return a dynamically typesafe view of the specified sorted set
3310 * @since 1.5
3311 */
3312 public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
3313 Class<E> type) {
3314 return new CheckedSortedSet<>(s, type);
3315 }
3316
3317 /**
3318 * @serial include
3319 */
3320 static class CheckedSortedSet<E> extends CheckedSet<E>
3321 implements SortedSet<E>, Serializable
3322 {
3323 @java.io.Serial
3324 private static final long serialVersionUID = 1599911165492914959L;
3325
3326 private final SortedSet<E> ss;
3327
3328 CheckedSortedSet(SortedSet<E> s, Class<E> type) {
3329 super(s, type);
3330 ss = s;
3331 }
3332
3333 public Comparator<? super E> comparator() { return ss.comparator(); }
3334 public E first() { return ss.first(); }
3335 public E last() { return ss.last(); }
3336
3337 public SortedSet<E> subSet(E fromElement, E toElement) {
3338 return checkedSortedSet(ss.subSet(fromElement,toElement), type);
3339 }
3340 public SortedSet<E> headSet(E toElement) {
3341 return checkedSortedSet(ss.headSet(toElement), type);
3342 }
3343 public SortedSet<E> tailSet(E fromElement) {
3344 return checkedSortedSet(ss.tailSet(fromElement), type);
3345 }
3370 * @param s the navigable set for which a dynamically typesafe view is to be
3371 * returned
3372 * @param type the type of element that {@code s} is permitted to hold
3373 * @return a dynamically typesafe view of the specified navigable set
3374 * @since 1.8
3375 */
3376 public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s,
3377 Class<E> type) {
3378 return new CheckedNavigableSet<>(s, type);
3379 }
3380
3381 /**
3382 * @serial include
3383 */
3384 static class CheckedNavigableSet<E> extends CheckedSortedSet<E>
3385 implements NavigableSet<E>, Serializable
3386 {
3387 @java.io.Serial
3388 private static final long serialVersionUID = -5429120189805438922L;
3389
3390 private final NavigableSet<E> ns;
3391
3392 CheckedNavigableSet(NavigableSet<E> s, Class<E> type) {
3393 super(s, type);
3394 ns = s;
3395 }
3396
3397 public E lower(E e) { return ns.lower(e); }
3398 public E floor(E e) { return ns.floor(e); }
3399 public E ceiling(E e) { return ns.ceiling(e); }
3400 public E higher(E e) { return ns.higher(e); }
3401 public E pollFirst() { return ns.pollFirst(); }
3402 public E pollLast() {return ns.pollLast(); }
3403 public NavigableSet<E> descendingSet()
3404 { return checkedNavigableSet(ns.descendingSet(), type); }
3405 public Iterator<E> descendingIterator()
3406 {return checkedNavigableSet(ns.descendingSet(), type).iterator(); }
3407
3408 public NavigableSet<E> subSet(E fromElement, E toElement) {
3409 return checkedNavigableSet(ns.subSet(fromElement, true, toElement, false), type);
3453 * returned
3454 * @param type the type of element that {@code list} is permitted to hold
3455 * @return a dynamically typesafe view of the specified list
3456 * @since 1.5
3457 */
3458 public static <E> List<E> checkedList(List<E> list, Class<E> type) {
3459 return (list instanceof RandomAccess ?
3460 new CheckedRandomAccessList<>(list, type) :
3461 new CheckedList<>(list, type));
3462 }
3463
3464 /**
3465 * @serial include
3466 */
3467 static class CheckedList<E>
3468 extends CheckedCollection<E>
3469 implements List<E>
3470 {
3471 @java.io.Serial
3472 private static final long serialVersionUID = 65247728283967356L;
3473 final List<E> list;
3474
3475 CheckedList(List<E> list, Class<E> type) {
3476 super(list, type);
3477 this.list = list;
3478 }
3479
3480 public boolean equals(Object o) { return o == this || list.equals(o); }
3481 public int hashCode() { return list.hashCode(); }
3482 public E get(int index) { return list.get(index); }
3483 public E remove(int index) { return list.remove(index); }
3484 public int indexOf(Object o) { return list.indexOf(o); }
3485 public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
3486
3487 public E set(int index, E element) {
3488 return list.set(index, typeCheck(element));
3489 }
3490
3491 public void add(int index, E element) {
3492 list.add(index, typeCheck(element));
3602 * @param valueType the type of value that {@code m} is permitted to hold
3603 * @return a dynamically typesafe view of the specified map
3604 * @since 1.5
3605 */
3606 public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
3607 Class<K> keyType,
3608 Class<V> valueType) {
3609 return new CheckedMap<>(m, keyType, valueType);
3610 }
3611
3612
3613 /**
3614 * @serial include
3615 */
3616 private static class CheckedMap<K,V>
3617 implements Map<K,V>, Serializable
3618 {
3619 @java.io.Serial
3620 private static final long serialVersionUID = 5742860141034234728L;
3621
3622 private final Map<K, V> m;
3623 final Class<K> keyType;
3624 final Class<V> valueType;
3625
3626 private void typeCheck(Object key, Object value) {
3627 if (key != null && !keyType.isInstance(key))
3628 throw new ClassCastException(badKeyMsg(key));
3629
3630 if (value != null && !valueType.isInstance(value))
3631 throw new ClassCastException(badValueMsg(value));
3632 }
3633
3634 private BiFunction<? super K, ? super V, ? extends V> typeCheck(
3635 BiFunction<? super K, ? super V, ? extends V> func) {
3636 Objects.requireNonNull(func);
3637 return (k, v) -> {
3638 V newValue = func.apply(k, v);
3639 typeCheck(k, newValue);
3640 return newValue;
3641 };
3642 }
3643
4002 * @param keyType the type of key that {@code m} is permitted to hold
4003 * @param valueType the type of value that {@code m} is permitted to hold
4004 * @return a dynamically typesafe view of the specified map
4005 * @since 1.5
4006 */
4007 public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
4008 Class<K> keyType,
4009 Class<V> valueType) {
4010 return new CheckedSortedMap<>(m, keyType, valueType);
4011 }
4012
4013 /**
4014 * @serial include
4015 */
4016 static class CheckedSortedMap<K,V> extends CheckedMap<K,V>
4017 implements SortedMap<K,V>, Serializable
4018 {
4019 @java.io.Serial
4020 private static final long serialVersionUID = 1599671320688067438L;
4021
4022 private final SortedMap<K, V> sm;
4023
4024 CheckedSortedMap(SortedMap<K, V> m,
4025 Class<K> keyType, Class<V> valueType) {
4026 super(m, keyType, valueType);
4027 sm = m;
4028 }
4029
4030 public Comparator<? super K> comparator() { return sm.comparator(); }
4031 public K firstKey() { return sm.firstKey(); }
4032 public K lastKey() { return sm.lastKey(); }
4033
4034 public SortedMap<K,V> subMap(K fromKey, K toKey) {
4035 return checkedSortedMap(sm.subMap(fromKey, toKey),
4036 keyType, valueType);
4037 }
4038 public SortedMap<K,V> headMap(K toKey) {
4039 return checkedSortedMap(sm.headMap(toKey), keyType, valueType);
4040 }
4041 public SortedMap<K,V> tailMap(K fromKey) {
4077 * @param keyType the type of key that {@code m} is permitted to hold
4078 * @param valueType the type of value that {@code m} is permitted to hold
4079 * @return a dynamically typesafe view of the specified map
4080 * @since 1.8
4081 */
4082 public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K, V> m,
4083 Class<K> keyType,
4084 Class<V> valueType) {
4085 return new CheckedNavigableMap<>(m, keyType, valueType);
4086 }
4087
4088 /**
4089 * @serial include
4090 */
4091 static class CheckedNavigableMap<K,V> extends CheckedSortedMap<K,V>
4092 implements NavigableMap<K,V>, Serializable
4093 {
4094 @java.io.Serial
4095 private static final long serialVersionUID = -4852462692372534096L;
4096
4097 private final NavigableMap<K, V> nm;
4098
4099 CheckedNavigableMap(NavigableMap<K, V> m,
4100 Class<K> keyType, Class<V> valueType) {
4101 super(m, keyType, valueType);
4102 nm = m;
4103 }
4104
4105 public Comparator<? super K> comparator() { return nm.comparator(); }
4106 public K firstKey() { return nm.firstKey(); }
4107 public K lastKey() { return nm.lastKey(); }
4108
4109 public Entry<K, V> lowerEntry(K key) {
4110 Entry<K,V> lower = nm.lowerEntry(key);
4111 return (null != lower)
4112 ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(lower, valueType)
4113 : null;
4114 }
4115
4116 public K lowerKey(K key) { return nm.lowerKey(key); }
4808 @Override
4809 public int characteristics() {
4810 int value = (element != null) ? Spliterator.NONNULL : 0;
4811
4812 return value | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE |
4813 Spliterator.DISTINCT | Spliterator.ORDERED;
4814 }
4815 };
4816 }
4817
4818 /**
4819 * @serial include
4820 */
4821 private static class SingletonSet<E>
4822 extends AbstractSet<E>
4823 implements Serializable
4824 {
4825 @java.io.Serial
4826 private static final long serialVersionUID = 3193687207550431679L;
4827
4828 private final E element;
4829
4830 SingletonSet(E e) {element = e;}
4831
4832 public Iterator<E> iterator() {
4833 return singletonIterator(element);
4834 }
4835
4836 public int size() {return 1;}
4837
4838 public boolean contains(Object o) {return eq(o, element);}
4839
4840 // Override default methods for Collection
4841 @Override
4842 public void forEach(Consumer<? super E> action) {
4843 action.accept(element);
4844 }
4845 @Override
4846 public Spliterator<E> spliterator() {
4847 return singletonSpliterator(element);
4862 *
4863 * @param <T> the class of the objects in the list
4864 * @param o the sole object to be stored in the returned list.
4865 * @return an immutable list containing only the specified object.
4866 * @since 1.3
4867 */
4868 public static <T> List<T> singletonList(T o) {
4869 return new SingletonList<>(o);
4870 }
4871
4872 /**
4873 * @serial include
4874 */
4875 private static class SingletonList<E>
4876 extends AbstractList<E>
4877 implements RandomAccess, Serializable {
4878
4879 @java.io.Serial
4880 private static final long serialVersionUID = 3093736618740652951L;
4881
4882 private final E element;
4883
4884 SingletonList(E obj) {element = obj;}
4885
4886 public Iterator<E> iterator() {
4887 return singletonIterator(element);
4888 }
4889
4890 public int size() {return 1;}
4891
4892 public boolean contains(Object obj) {return eq(obj, element);}
4893
4894 public E get(int index) {
4895 if (index != 0)
4896 throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
4897 return element;
4898 }
4899
4900 // Override default methods for Collection
4901 @Override
4931 * @param <V> the class of the map values
4932 * @param key the sole key to be stored in the returned map.
4933 * @param value the value to which the returned map maps {@code key}.
4934 * @return an immutable map containing only the specified key-value
4935 * mapping.
4936 * @since 1.3
4937 */
4938 public static <K,V> Map<K,V> singletonMap(K key, V value) {
4939 return new SingletonMap<>(key, value);
4940 }
4941
4942 /**
4943 * @serial include
4944 */
4945 private static class SingletonMap<K,V>
4946 extends AbstractMap<K,V>
4947 implements Serializable {
4948 @java.io.Serial
4949 private static final long serialVersionUID = -6979724477215052911L;
4950
4951 private final K k;
4952 private final V v;
4953
4954 SingletonMap(K key, V value) {
4955 k = key;
4956 v = value;
4957 }
4958
4959 public int size() {return 1;}
4960 public boolean isEmpty() {return false;}
4961 public boolean containsKey(Object key) {return eq(key, k);}
4962 public boolean containsValue(Object value) {return eq(value, v);}
4963 public V get(Object key) {return (eq(key, k) ? v : null);}
4964
4965 private transient Set<K> keySet;
4966 private transient Set<Map.Entry<K,V>> entrySet;
4967 private transient Collection<V> values;
4968
4969 public Set<K> keySet() {
4970 if (keySet==null)
4971 keySet = singleton(k);
5070 * @see List#addAll(Collection)
5071 * @see List#addAll(int, Collection)
5072 */
5073 public static <T> List<T> nCopies(int n, T o) {
5074 if (n < 0)
5075 throw new IllegalArgumentException("List length = " + n);
5076 return new CopiesList<>(n, o);
5077 }
5078
5079 /**
5080 * @serial include
5081 */
5082 private static class CopiesList<E>
5083 extends AbstractList<E>
5084 implements RandomAccess, Serializable
5085 {
5086 @java.io.Serial
5087 private static final long serialVersionUID = 2739099268398711800L;
5088
5089 final int n;
5090 final E element;
5091
5092 CopiesList(int n, E e) {
5093 assert n >= 0;
5094 this.n = n;
5095 element = e;
5096 }
5097
5098 public int size() {
5099 return n;
5100 }
5101
5102 public boolean contains(Object obj) {
5103 return n != 0 && eq(obj, element);
5104 }
5105
5106 public int indexOf(Object o) {
5107 return contains(o) ? 0 : -1;
5108 }
5109
5303 return new ReverseComparator2<>(cmp);
5304 }
5305 }
5306
5307 /**
5308 * @serial include
5309 */
5310 private static class ReverseComparator2<T> implements Comparator<T>,
5311 Serializable
5312 {
5313 @java.io.Serial
5314 private static final long serialVersionUID = 4374092139857L;
5315
5316 /**
5317 * The comparator specified in the static factory. This will never
5318 * be null, as the static factory returns a ReverseComparator
5319 * instance if its argument is null.
5320 *
5321 * @serial
5322 */
5323 final Comparator<T> cmp;
5324
5325 ReverseComparator2(Comparator<T> cmp) {
5326 assert cmp != null;
5327 this.cmp = cmp;
5328 }
5329
5330 public int compare(T t1, T t2) {
5331 return cmp.compare(t2, t1);
5332 }
5333
5334 public boolean equals(Object o) {
5335 return (o == this) ||
5336 (o instanceof ReverseComparator2 &&
5337 cmp.equals(((ReverseComparator2)o).cmp));
5338 }
5339
5340 public int hashCode() {
5341 return cmp.hashCode() ^ Integer.MIN_VALUE;
5342 }
5584 * new WeakHashMap<Object, Boolean>());
5585 * </pre>
5586 *
5587 * @param <E> the class of the map keys and of the objects in the
5588 * returned set
5589 * @param map the backing map
5590 * @return the set backed by the map
5591 * @throws IllegalArgumentException if {@code map} is not empty
5592 * @since 1.6
5593 */
5594 public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
5595 return new SetFromMap<>(map);
5596 }
5597
5598 /**
5599 * @serial include
5600 */
5601 private static class SetFromMap<E> extends AbstractSet<E>
5602 implements Set<E>, Serializable
5603 {
5604 private final Map<E, Boolean> m; // The backing map
5605 private transient Set<E> s; // Its keySet
5606
5607 SetFromMap(Map<E, Boolean> map) {
5608 if (!map.isEmpty())
5609 throw new IllegalArgumentException("Map is non-empty");
5610 m = map;
5611 s = map.keySet();
5612 }
5613
5614 public void clear() { m.clear(); }
5615 public int size() { return m.size(); }
5616 public boolean isEmpty() { return m.isEmpty(); }
5617 public boolean contains(Object o) { return m.containsKey(o); }
5618 public boolean remove(Object o) { return m.remove(o) != null; }
5619 public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
5620 public Iterator<E> iterator() { return s.iterator(); }
5621 public Object[] toArray() { return s.toArray(); }
5622 public <T> T[] toArray(T[] a) { return s.toArray(a); }
5623 public String toString() { return s.toString(); }
5669 * one exception. The {@link Queue#addAll addAll} method is
5670 * implemented as a sequence of {@link Deque#addFirst addFirst}
5671 * invocations on the backing deque.
5672 *
5673 * @param <T> the class of the objects in the deque
5674 * @param deque the deque
5675 * @return the queue
5676 * @since 1.6
5677 */
5678 public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
5679 return new AsLIFOQueue<>(Objects.requireNonNull(deque));
5680 }
5681
5682 /**
5683 * @serial include
5684 */
5685 static class AsLIFOQueue<E> extends AbstractQueue<E>
5686 implements Queue<E>, Serializable {
5687 @java.io.Serial
5688 private static final long serialVersionUID = 1802017725587941708L;
5689 private final Deque<E> q;
5690 AsLIFOQueue(Deque<E> q) { this.q = q; }
5691 public boolean add(E e) { q.addFirst(e); return true; }
5692 public boolean offer(E e) { return q.offerFirst(e); }
5693 public E poll() { return q.pollFirst(); }
5694 public E remove() { return q.removeFirst(); }
5695 public E peek() { return q.peekFirst(); }
5696 public E element() { return q.getFirst(); }
5697 public void clear() { q.clear(); }
5698 public int size() { return q.size(); }
5699 public boolean isEmpty() { return q.isEmpty(); }
5700 public boolean contains(Object o) { return q.contains(o); }
5701 public boolean remove(Object o) { return q.remove(o); }
5702 public Iterator<E> iterator() { return q.iterator(); }
5703 public Object[] toArray() { return q.toArray(); }
5704 public <T> T[] toArray(T[] a) { return q.toArray(a); }
5705 public <T> T[] toArray(IntFunction<T[]> f) { return q.toArray(f); }
5706 public String toString() { return q.toString(); }
5707 public boolean containsAll(Collection<?> c) { return q.containsAll(c); }
5708 public boolean removeAll(Collection<?> c) { return q.removeAll(c); }
|
1007 *
1008 * The returned collection will be serializable if the specified collection
1009 * is serializable.
1010 *
1011 * @param <T> the class of the objects in the collection
1012 * @param c the collection for which an unmodifiable view is to be
1013 * returned.
1014 * @return an unmodifiable view of the specified collection.
1015 */
1016 public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
1017 return new UnmodifiableCollection<>(c);
1018 }
1019
1020 /**
1021 * @serial include
1022 */
1023 static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
1024 @java.io.Serial
1025 private static final long serialVersionUID = 1820017752578914078L;
1026
1027 @SuppressWarnings("serial") // Not statically typed as Serializable
1028 final Collection<? extends E> c;
1029
1030 UnmodifiableCollection(Collection<? extends E> c) {
1031 if (c==null)
1032 throw new NullPointerException();
1033 this.c = c;
1034 }
1035
1036 public int size() {return c.size();}
1037 public boolean isEmpty() {return c.isEmpty();}
1038 public boolean contains(Object o) {return c.contains(o);}
1039 public Object[] toArray() {return c.toArray();}
1040 public <T> T[] toArray(T[] a) {return c.toArray(a);}
1041 public <T> T[] toArray(IntFunction<T[]> f) {return c.toArray(f);}
1042 public String toString() {return c.toString();}
1043
1044 public Iterator<E> iterator() {
1045 return new Iterator<E>() {
1046 private final Iterator<? extends E> i = c.iterator();
1047
1148 * The returned sorted set will be serializable if the specified sorted set
1149 * is serializable.
1150 *
1151 * @param <T> the class of the objects in the set
1152 * @param s the sorted set for which an unmodifiable view is to be
1153 * returned.
1154 * @return an unmodifiable view of the specified sorted set.
1155 */
1156 public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
1157 return new UnmodifiableSortedSet<>(s);
1158 }
1159
1160 /**
1161 * @serial include
1162 */
1163 static class UnmodifiableSortedSet<E>
1164 extends UnmodifiableSet<E>
1165 implements SortedSet<E>, Serializable {
1166 @java.io.Serial
1167 private static final long serialVersionUID = -4929149591599911165L;
1168 @SuppressWarnings("serial") // Not statically typed as Serializable
1169 private final SortedSet<E> ss;
1170
1171 UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
1172
1173 public Comparator<? super E> comparator() {return ss.comparator();}
1174
1175 public SortedSet<E> subSet(E fromElement, E toElement) {
1176 return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement));
1177 }
1178 public SortedSet<E> headSet(E toElement) {
1179 return new UnmodifiableSortedSet<>(ss.headSet(toElement));
1180 }
1181 public SortedSet<E> tailSet(E fromElement) {
1182 return new UnmodifiableSortedSet<>(ss.tailSet(fromElement));
1183 }
1184
1185 public E first() {return ss.first();}
1186 public E last() {return ss.last();}
1187 }
1188
1229 private static class EmptyNavigableSet<E> extends UnmodifiableNavigableSet<E>
1230 implements Serializable {
1231 @java.io.Serial
1232 private static final long serialVersionUID = -6291252904449939134L;
1233
1234 public EmptyNavigableSet() {
1235 super(new TreeSet<>());
1236 }
1237
1238 @java.io.Serial
1239 private Object readResolve() { return EMPTY_NAVIGABLE_SET; }
1240 }
1241
1242 @SuppressWarnings("rawtypes")
1243 private static final NavigableSet<?> EMPTY_NAVIGABLE_SET =
1244 new EmptyNavigableSet<>();
1245
1246 /**
1247 * The instance we are protecting.
1248 */
1249 @SuppressWarnings("serial") // Not statically typed as Serializable
1250 private final NavigableSet<E> ns;
1251
1252 UnmodifiableNavigableSet(NavigableSet<E> s) {super(s); ns = s;}
1253
1254 public E lower(E e) { return ns.lower(e); }
1255 public E floor(E e) { return ns.floor(e); }
1256 public E ceiling(E e) { return ns.ceiling(e); }
1257 public E higher(E e) { return ns.higher(e); }
1258 public E pollFirst() { throw new UnsupportedOperationException(); }
1259 public E pollLast() { throw new UnsupportedOperationException(); }
1260 public NavigableSet<E> descendingSet()
1261 { return new UnmodifiableNavigableSet<>(ns.descendingSet()); }
1262 public Iterator<E> descendingIterator()
1263 { return descendingSet().iterator(); }
1264
1265 public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
1266 return new UnmodifiableNavigableSet<>(
1267 ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
1268 }
1269
1290 * {@link RandomAccess} if the specified list does.
1291 *
1292 * @param <T> the class of the objects in the list
1293 * @param list the list for which an unmodifiable view is to be returned.
1294 * @return an unmodifiable view of the specified list.
1295 */
1296 public static <T> List<T> unmodifiableList(List<? extends T> list) {
1297 return (list instanceof RandomAccess ?
1298 new UnmodifiableRandomAccessList<>(list) :
1299 new UnmodifiableList<>(list));
1300 }
1301
1302 /**
1303 * @serial include
1304 */
1305 static class UnmodifiableList<E> extends UnmodifiableCollection<E>
1306 implements List<E> {
1307 @java.io.Serial
1308 private static final long serialVersionUID = -283967356065247728L;
1309
1310 @SuppressWarnings("serial") // Not statically typed as Serializable
1311 final List<? extends E> list;
1312
1313 UnmodifiableList(List<? extends E> list) {
1314 super(list);
1315 this.list = list;
1316 }
1317
1318 public boolean equals(Object o) {return o == this || list.equals(o);}
1319 public int hashCode() {return list.hashCode();}
1320
1321 public E get(int index) {return list.get(index);}
1322 public E set(int index, E element) {
1323 throw new UnsupportedOperationException();
1324 }
1325 public void add(int index, E element) {
1326 throw new UnsupportedOperationException();
1327 }
1328 public E remove(int index) {
1329 throw new UnsupportedOperationException();
1330 }
1437 *
1438 * The returned map will be serializable if the specified map
1439 * is serializable.
1440 *
1441 * @param <K> the class of the map keys
1442 * @param <V> the class of the map values
1443 * @param m the map for which an unmodifiable view is to be returned.
1444 * @return an unmodifiable view of the specified map.
1445 */
1446 public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
1447 return new UnmodifiableMap<>(m);
1448 }
1449
1450 /**
1451 * @serial include
1452 */
1453 private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable {
1454 @java.io.Serial
1455 private static final long serialVersionUID = -1034234728574286014L;
1456
1457 @SuppressWarnings("serial") // Not statically typed as Serializable
1458 private final Map<? extends K, ? extends V> m;
1459
1460 UnmodifiableMap(Map<? extends K, ? extends V> m) {
1461 if (m==null)
1462 throw new NullPointerException();
1463 this.m = m;
1464 }
1465
1466 public int size() {return m.size();}
1467 public boolean isEmpty() {return m.isEmpty();}
1468 public boolean containsKey(Object key) {return m.containsKey(key);}
1469 public boolean containsValue(Object val) {return m.containsValue(val);}
1470 public V get(Object key) {return m.get(key);}
1471
1472 public V put(K key, V value) {
1473 throw new UnsupportedOperationException();
1474 }
1475 public V remove(Object key) {
1476 throw new UnsupportedOperationException();
1477 }
1797 *
1798 * @param <K> the class of the map keys
1799 * @param <V> the class of the map values
1800 * @param m the sorted map for which an unmodifiable view is to be
1801 * returned.
1802 * @return an unmodifiable view of the specified sorted map.
1803 */
1804 public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
1805 return new UnmodifiableSortedMap<>(m);
1806 }
1807
1808 /**
1809 * @serial include
1810 */
1811 static class UnmodifiableSortedMap<K,V>
1812 extends UnmodifiableMap<K,V>
1813 implements SortedMap<K,V>, Serializable {
1814 @java.io.Serial
1815 private static final long serialVersionUID = -8806743815996713206L;
1816
1817 @SuppressWarnings("serial") // Not statically typed as Serializable
1818 private final SortedMap<K, ? extends V> sm;
1819
1820 UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; }
1821 public Comparator<? super K> comparator() { return sm.comparator(); }
1822 public SortedMap<K,V> subMap(K fromKey, K toKey)
1823 { return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); }
1824 public SortedMap<K,V> headMap(K toKey)
1825 { return new UnmodifiableSortedMap<>(sm.headMap(toKey)); }
1826 public SortedMap<K,V> tailMap(K fromKey)
1827 { return new UnmodifiableSortedMap<>(sm.tailMap(fromKey)); }
1828 public K firstKey() { return sm.firstKey(); }
1829 public K lastKey() { return sm.lastKey(); }
1830 }
1831
1832 /**
1833 * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1834 * specified navigable map. Query operations on the returned navigable map "read
1835 * through" to the specified navigable map. Attempts to modify the returned
1836 * navigable map, whether direct, via its collection views, or via its
1837 * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
1875
1876 EmptyNavigableMap() { super(new TreeMap<>()); }
1877
1878 @Override
1879 public NavigableSet<K> navigableKeySet()
1880 { return emptyNavigableSet(); }
1881
1882 @java.io.Serial
1883 private Object readResolve() { return EMPTY_NAVIGABLE_MAP; }
1884 }
1885
1886 /**
1887 * Singleton for {@link emptyNavigableMap()} which is also immutable.
1888 */
1889 private static final EmptyNavigableMap<?,?> EMPTY_NAVIGABLE_MAP =
1890 new EmptyNavigableMap<>();
1891
1892 /**
1893 * The instance we wrap and protect.
1894 */
1895 @SuppressWarnings("serial") // Not statically typed as Serializable
1896 private final NavigableMap<K, ? extends V> nm;
1897
1898 UnmodifiableNavigableMap(NavigableMap<K, ? extends V> m)
1899 {super(m); nm = m;}
1900
1901 public K lowerKey(K key) { return nm.lowerKey(key); }
1902 public K floorKey(K key) { return nm.floorKey(key); }
1903 public K ceilingKey(K key) { return nm.ceilingKey(key); }
1904 public K higherKey(K key) { return nm.higherKey(key); }
1905
1906 @SuppressWarnings("unchecked")
1907 public Entry<K, V> lowerEntry(K key) {
1908 Entry<K,V> lower = (Entry<K, V>) nm.lowerEntry(key);
1909 return (null != lower)
1910 ? new UnmodifiableEntrySet.UnmodifiableEntry<>(lower)
1911 : null;
1912 }
1913
1914 @SuppressWarnings("unchecked")
1915 public Entry<K, V> floorEntry(K key) {
2007 *
2008 * @param <T> the class of the objects in the collection
2009 * @param c the collection to be "wrapped" in a synchronized collection.
2010 * @return a synchronized view of the specified collection.
2011 */
2012 public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
2013 return new SynchronizedCollection<>(c);
2014 }
2015
2016 static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
2017 return new SynchronizedCollection<>(c, mutex);
2018 }
2019
2020 /**
2021 * @serial include
2022 */
2023 static class SynchronizedCollection<E> implements Collection<E>, Serializable {
2024 @java.io.Serial
2025 private static final long serialVersionUID = 3053995032091335093L;
2026
2027 @SuppressWarnings("serial") // Not statically typed as Serializable
2028 final Collection<E> c; // Backing Collection
2029 @SuppressWarnings("serial") // Not statically typed as Serializable
2030 final Object mutex; // Object on which to synchronize
2031
2032 SynchronizedCollection(Collection<E> c) {
2033 this.c = Objects.requireNonNull(c);
2034 mutex = this;
2035 }
2036
2037 SynchronizedCollection(Collection<E> c, Object mutex) {
2038 this.c = Objects.requireNonNull(c);
2039 this.mutex = Objects.requireNonNull(mutex);
2040 }
2041
2042 public int size() {
2043 synchronized (mutex) {return c.size();}
2044 }
2045 public boolean isEmpty() {
2046 synchronized (mutex) {return c.isEmpty();}
2047 }
2048 public boolean contains(Object o) {
2049 synchronized (mutex) {return c.contains(o);}
2211 * sorted set is serializable.
2212 *
2213 * @param <T> the class of the objects in the set
2214 * @param s the sorted set to be "wrapped" in a synchronized sorted set.
2215 * @return a synchronized view of the specified sorted set.
2216 */
2217 public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
2218 return new SynchronizedSortedSet<>(s);
2219 }
2220
2221 /**
2222 * @serial include
2223 */
2224 static class SynchronizedSortedSet<E>
2225 extends SynchronizedSet<E>
2226 implements SortedSet<E>
2227 {
2228 @java.io.Serial
2229 private static final long serialVersionUID = 8695801310862127406L;
2230
2231 @SuppressWarnings("serial") // Not statically typed as Serializable
2232 private final SortedSet<E> ss;
2233
2234 SynchronizedSortedSet(SortedSet<E> s) {
2235 super(s);
2236 ss = s;
2237 }
2238 SynchronizedSortedSet(SortedSet<E> s, Object mutex) {
2239 super(s, mutex);
2240 ss = s;
2241 }
2242
2243 public Comparator<? super E> comparator() {
2244 synchronized (mutex) {return ss.comparator();}
2245 }
2246
2247 public SortedSet<E> subSet(E fromElement, E toElement) {
2248 synchronized (mutex) {
2249 return new SynchronizedSortedSet<>(
2250 ss.subSet(fromElement, toElement), mutex);
2251 }
2307 * @param <T> the class of the objects in the set
2308 * @param s the navigable set to be "wrapped" in a synchronized navigable
2309 * set
2310 * @return a synchronized view of the specified navigable set
2311 * @since 1.8
2312 */
2313 public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) {
2314 return new SynchronizedNavigableSet<>(s);
2315 }
2316
2317 /**
2318 * @serial include
2319 */
2320 static class SynchronizedNavigableSet<E>
2321 extends SynchronizedSortedSet<E>
2322 implements NavigableSet<E>
2323 {
2324 @java.io.Serial
2325 private static final long serialVersionUID = -5505529816273629798L;
2326
2327 @SuppressWarnings("serial") // Not statically typed as Serializable
2328 private final NavigableSet<E> ns;
2329
2330 SynchronizedNavigableSet(NavigableSet<E> s) {
2331 super(s);
2332 ns = s;
2333 }
2334
2335 SynchronizedNavigableSet(NavigableSet<E> s, Object mutex) {
2336 super(s, mutex);
2337 ns = s;
2338 }
2339 public E lower(E e) { synchronized (mutex) {return ns.lower(e);} }
2340 public E floor(E e) { synchronized (mutex) {return ns.floor(e);} }
2341 public E ceiling(E e) { synchronized (mutex) {return ns.ceiling(e);} }
2342 public E higher(E e) { synchronized (mutex) {return ns.higher(e);} }
2343 public E pollFirst() { synchronized (mutex) {return ns.pollFirst();} }
2344 public E pollLast() { synchronized (mutex) {return ns.pollLast();} }
2345
2346 public NavigableSet<E> descendingSet() {
2347 synchronized (mutex) {
2418 return (list instanceof RandomAccess ?
2419 new SynchronizedRandomAccessList<>(list) :
2420 new SynchronizedList<>(list));
2421 }
2422
2423 static <T> List<T> synchronizedList(List<T> list, Object mutex) {
2424 return (list instanceof RandomAccess ?
2425 new SynchronizedRandomAccessList<>(list, mutex) :
2426 new SynchronizedList<>(list, mutex));
2427 }
2428
2429 /**
2430 * @serial include
2431 */
2432 static class SynchronizedList<E>
2433 extends SynchronizedCollection<E>
2434 implements List<E> {
2435 @java.io.Serial
2436 private static final long serialVersionUID = -7754090372962971524L;
2437
2438 @SuppressWarnings("serial") // Not statically typed as Serializable
2439 final List<E> list;
2440
2441 SynchronizedList(List<E> list) {
2442 super(list);
2443 this.list = list;
2444 }
2445 SynchronizedList(List<E> list, Object mutex) {
2446 super(list, mutex);
2447 this.list = list;
2448 }
2449
2450 public boolean equals(Object o) {
2451 if (this == o)
2452 return true;
2453 synchronized (mutex) {return list.equals(o);}
2454 }
2455 public int hashCode() {
2456 synchronized (mutex) {return list.hashCode();}
2457 }
2458
2586 * <p>The returned map will be serializable if the specified map is
2587 * serializable.
2588 *
2589 * @param <K> the class of the map keys
2590 * @param <V> the class of the map values
2591 * @param m the map to be "wrapped" in a synchronized map.
2592 * @return a synchronized view of the specified map.
2593 */
2594 public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
2595 return new SynchronizedMap<>(m);
2596 }
2597
2598 /**
2599 * @serial include
2600 */
2601 private static class SynchronizedMap<K,V>
2602 implements Map<K,V>, Serializable {
2603 @java.io.Serial
2604 private static final long serialVersionUID = 1978198479659022715L;
2605
2606 @SuppressWarnings("serial") // Not statically typed as Serializable
2607 private final Map<K,V> m; // Backing Map
2608 @SuppressWarnings("serial") // Not statically typed as Serializable
2609 final Object mutex; // Object on which to synchronize
2610
2611 SynchronizedMap(Map<K,V> m) {
2612 this.m = Objects.requireNonNull(m);
2613 mutex = this;
2614 }
2615
2616 SynchronizedMap(Map<K,V> m, Object mutex) {
2617 this.m = m;
2618 this.mutex = mutex;
2619 }
2620
2621 public int size() {
2622 synchronized (mutex) {return m.size();}
2623 }
2624 public boolean isEmpty() {
2625 synchronized (mutex) {return m.isEmpty();}
2626 }
2627 public boolean containsKey(Object key) {
2628 synchronized (mutex) {return m.containsKey(key);}
2785 *
2786 * @param <K> the class of the map keys
2787 * @param <V> the class of the map values
2788 * @param m the sorted map to be "wrapped" in a synchronized sorted map.
2789 * @return a synchronized view of the specified sorted map.
2790 */
2791 public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
2792 return new SynchronizedSortedMap<>(m);
2793 }
2794
2795 /**
2796 * @serial include
2797 */
2798 static class SynchronizedSortedMap<K,V>
2799 extends SynchronizedMap<K,V>
2800 implements SortedMap<K,V>
2801 {
2802 @java.io.Serial
2803 private static final long serialVersionUID = -8798146769416483793L;
2804
2805 @SuppressWarnings("serial") // Not statically typed as Serializable
2806 private final SortedMap<K,V> sm;
2807
2808 SynchronizedSortedMap(SortedMap<K,V> m) {
2809 super(m);
2810 sm = m;
2811 }
2812 SynchronizedSortedMap(SortedMap<K,V> m, Object mutex) {
2813 super(m, mutex);
2814 sm = m;
2815 }
2816
2817 public Comparator<? super K> comparator() {
2818 synchronized (mutex) {return sm.comparator();}
2819 }
2820
2821 public SortedMap<K,V> subMap(K fromKey, K toKey) {
2822 synchronized (mutex) {
2823 return new SynchronizedSortedMap<>(
2824 sm.subMap(fromKey, toKey), mutex);
2825 }
2889 * map
2890 * @return a synchronized view of the specified navigable map.
2891 * @since 1.8
2892 */
2893 public static <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) {
2894 return new SynchronizedNavigableMap<>(m);
2895 }
2896
2897 /**
2898 * A synchronized NavigableMap.
2899 *
2900 * @serial include
2901 */
2902 static class SynchronizedNavigableMap<K,V>
2903 extends SynchronizedSortedMap<K,V>
2904 implements NavigableMap<K,V>
2905 {
2906 @java.io.Serial
2907 private static final long serialVersionUID = 699392247599746807L;
2908
2909 @SuppressWarnings("serial") // Not statically typed as Serializable
2910 private final NavigableMap<K,V> nm;
2911
2912 SynchronizedNavigableMap(NavigableMap<K,V> m) {
2913 super(m);
2914 nm = m;
2915 }
2916 SynchronizedNavigableMap(NavigableMap<K,V> m, Object mutex) {
2917 super(m, mutex);
2918 nm = m;
2919 }
2920
2921 public Entry<K, V> lowerEntry(K key)
2922 { synchronized (mutex) { return nm.lowerEntry(key); } }
2923 public K lowerKey(K key)
2924 { synchronized (mutex) { return nm.lowerKey(key); } }
2925 public Entry<K, V> floorEntry(K key)
2926 { synchronized (mutex) { return nm.floorEntry(key); } }
2927 public K floorKey(K key)
2928 { synchronized (mutex) { return nm.floorKey(key); } }
2929 public Entry<K, V> ceilingEntry(K key)
3069 * @return a dynamically typesafe view of the specified collection
3070 * @since 1.5
3071 */
3072 public static <E> Collection<E> checkedCollection(Collection<E> c,
3073 Class<E> type) {
3074 return new CheckedCollection<>(c, type);
3075 }
3076
3077 @SuppressWarnings("unchecked")
3078 static <T> T[] zeroLengthArray(Class<T> type) {
3079 return (T[]) Array.newInstance(type, 0);
3080 }
3081
3082 /**
3083 * @serial include
3084 */
3085 static class CheckedCollection<E> implements Collection<E>, Serializable {
3086 @java.io.Serial
3087 private static final long serialVersionUID = 1578914078182001775L;
3088
3089 @SuppressWarnings("serial") // Not statically typed as Serializable
3090 final Collection<E> c;
3091 @SuppressWarnings("serial") // Not statically typed as Serializable
3092 final Class<E> type;
3093
3094 @SuppressWarnings("unchecked")
3095 E typeCheck(Object o) {
3096 if (o != null && !type.isInstance(o))
3097 throw new ClassCastException(badElementMsg(o));
3098 return (E) o;
3099 }
3100
3101 private String badElementMsg(Object o) {
3102 return "Attempt to insert " + o.getClass() +
3103 " element into collection with element type " + type;
3104 }
3105
3106 CheckedCollection(Collection<E> c, Class<E> type) {
3107 this.c = Objects.requireNonNull(c, "c");
3108 this.type = Objects.requireNonNull(type, "type");
3109 }
3110
3111 public int size() { return c.size(); }
3127 public boolean retainAll(Collection<?> coll) {
3128 return c.retainAll(coll);
3129 }
3130
3131 public Iterator<E> iterator() {
3132 // JDK-6363904 - unwrapped iterator could be typecast to
3133 // ListIterator with unsafe set()
3134 final Iterator<E> it = c.iterator();
3135 return new Iterator<E>() {
3136 public boolean hasNext() { return it.hasNext(); }
3137 public E next() { return it.next(); }
3138 public void remove() { it.remove(); }
3139 public void forEachRemaining(Consumer<? super E> action) {
3140 it.forEachRemaining(action);
3141 }
3142 };
3143 }
3144
3145 public boolean add(E e) { return c.add(typeCheck(e)); }
3146
3147 @SuppressWarnings("serial") // Not statically typed as Serializable
3148 private E[] zeroLengthElementArray; // Lazily initialized
3149
3150 private E[] zeroLengthElementArray() {
3151 return zeroLengthElementArray != null ? zeroLengthElementArray :
3152 (zeroLengthElementArray = zeroLengthArray(type));
3153 }
3154
3155 @SuppressWarnings("unchecked")
3156 Collection<E> checkedCopyOf(Collection<? extends E> coll) {
3157 Object[] a;
3158 try {
3159 E[] z = zeroLengthElementArray();
3160 a = coll.toArray(z);
3161 // Defend against coll violating the toArray contract
3162 if (a.getClass() != z.getClass())
3163 a = Arrays.copyOf(a, a.length, z.getClass());
3164 } catch (ArrayStoreException ignore) {
3165 // To get better and consistent diagnostics,
3166 // we call typeCheck explicitly on each element.
3167 // We call clone() to defend against coll retaining a
3221 * @param <E> the class of the objects in the queue
3222 * @param queue the queue for which a dynamically typesafe view is to be
3223 * returned
3224 * @param type the type of element that {@code queue} is permitted to hold
3225 * @return a dynamically typesafe view of the specified queue
3226 * @since 1.8
3227 */
3228 public static <E> Queue<E> checkedQueue(Queue<E> queue, Class<E> type) {
3229 return new CheckedQueue<>(queue, type);
3230 }
3231
3232 /**
3233 * @serial include
3234 */
3235 static class CheckedQueue<E>
3236 extends CheckedCollection<E>
3237 implements Queue<E>, Serializable
3238 {
3239 @java.io.Serial
3240 private static final long serialVersionUID = 1433151992604707767L;
3241 @SuppressWarnings("serial") // Not statically typed as Serializable
3242 final Queue<E> queue;
3243
3244 CheckedQueue(Queue<E> queue, Class<E> elementType) {
3245 super(queue, elementType);
3246 this.queue = queue;
3247 }
3248
3249 public E element() {return queue.element();}
3250 public boolean equals(Object o) {return o == this || c.equals(o);}
3251 public int hashCode() {return c.hashCode();}
3252 public E peek() {return queue.peek();}
3253 public E poll() {return queue.poll();}
3254 public E remove() {return queue.remove();}
3255 public boolean offer(E e) {return queue.offer(typeCheck(e));}
3256 }
3257
3258 /**
3259 * Returns a dynamically typesafe view of the specified set.
3260 * Any attempt to insert an element of the wrong type will result in
3261 * an immediate {@link ClassCastException}. Assuming a set contains
3326 * @param s the sorted set for which a dynamically typesafe view is to be
3327 * returned
3328 * @param type the type of element that {@code s} is permitted to hold
3329 * @return a dynamically typesafe view of the specified sorted set
3330 * @since 1.5
3331 */
3332 public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
3333 Class<E> type) {
3334 return new CheckedSortedSet<>(s, type);
3335 }
3336
3337 /**
3338 * @serial include
3339 */
3340 static class CheckedSortedSet<E> extends CheckedSet<E>
3341 implements SortedSet<E>, Serializable
3342 {
3343 @java.io.Serial
3344 private static final long serialVersionUID = 1599911165492914959L;
3345
3346 @SuppressWarnings("serial") // Not statically typed as Serializable
3347 private final SortedSet<E> ss;
3348
3349 CheckedSortedSet(SortedSet<E> s, Class<E> type) {
3350 super(s, type);
3351 ss = s;
3352 }
3353
3354 public Comparator<? super E> comparator() { return ss.comparator(); }
3355 public E first() { return ss.first(); }
3356 public E last() { return ss.last(); }
3357
3358 public SortedSet<E> subSet(E fromElement, E toElement) {
3359 return checkedSortedSet(ss.subSet(fromElement,toElement), type);
3360 }
3361 public SortedSet<E> headSet(E toElement) {
3362 return checkedSortedSet(ss.headSet(toElement), type);
3363 }
3364 public SortedSet<E> tailSet(E fromElement) {
3365 return checkedSortedSet(ss.tailSet(fromElement), type);
3366 }
3391 * @param s the navigable set for which a dynamically typesafe view is to be
3392 * returned
3393 * @param type the type of element that {@code s} is permitted to hold
3394 * @return a dynamically typesafe view of the specified navigable set
3395 * @since 1.8
3396 */
3397 public static <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s,
3398 Class<E> type) {
3399 return new CheckedNavigableSet<>(s, type);
3400 }
3401
3402 /**
3403 * @serial include
3404 */
3405 static class CheckedNavigableSet<E> extends CheckedSortedSet<E>
3406 implements NavigableSet<E>, Serializable
3407 {
3408 @java.io.Serial
3409 private static final long serialVersionUID = -5429120189805438922L;
3410
3411 @SuppressWarnings("serial") // Not statically typed as Serializable
3412 private final NavigableSet<E> ns;
3413
3414 CheckedNavigableSet(NavigableSet<E> s, Class<E> type) {
3415 super(s, type);
3416 ns = s;
3417 }
3418
3419 public E lower(E e) { return ns.lower(e); }
3420 public E floor(E e) { return ns.floor(e); }
3421 public E ceiling(E e) { return ns.ceiling(e); }
3422 public E higher(E e) { return ns.higher(e); }
3423 public E pollFirst() { return ns.pollFirst(); }
3424 public E pollLast() {return ns.pollLast(); }
3425 public NavigableSet<E> descendingSet()
3426 { return checkedNavigableSet(ns.descendingSet(), type); }
3427 public Iterator<E> descendingIterator()
3428 {return checkedNavigableSet(ns.descendingSet(), type).iterator(); }
3429
3430 public NavigableSet<E> subSet(E fromElement, E toElement) {
3431 return checkedNavigableSet(ns.subSet(fromElement, true, toElement, false), type);
3475 * returned
3476 * @param type the type of element that {@code list} is permitted to hold
3477 * @return a dynamically typesafe view of the specified list
3478 * @since 1.5
3479 */
3480 public static <E> List<E> checkedList(List<E> list, Class<E> type) {
3481 return (list instanceof RandomAccess ?
3482 new CheckedRandomAccessList<>(list, type) :
3483 new CheckedList<>(list, type));
3484 }
3485
3486 /**
3487 * @serial include
3488 */
3489 static class CheckedList<E>
3490 extends CheckedCollection<E>
3491 implements List<E>
3492 {
3493 @java.io.Serial
3494 private static final long serialVersionUID = 65247728283967356L;
3495 @SuppressWarnings("serial") // Not statically typed as Serializable
3496 final List<E> list;
3497
3498 CheckedList(List<E> list, Class<E> type) {
3499 super(list, type);
3500 this.list = list;
3501 }
3502
3503 public boolean equals(Object o) { return o == this || list.equals(o); }
3504 public int hashCode() { return list.hashCode(); }
3505 public E get(int index) { return list.get(index); }
3506 public E remove(int index) { return list.remove(index); }
3507 public int indexOf(Object o) { return list.indexOf(o); }
3508 public int lastIndexOf(Object o) { return list.lastIndexOf(o); }
3509
3510 public E set(int index, E element) {
3511 return list.set(index, typeCheck(element));
3512 }
3513
3514 public void add(int index, E element) {
3515 list.add(index, typeCheck(element));
3625 * @param valueType the type of value that {@code m} is permitted to hold
3626 * @return a dynamically typesafe view of the specified map
3627 * @since 1.5
3628 */
3629 public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
3630 Class<K> keyType,
3631 Class<V> valueType) {
3632 return new CheckedMap<>(m, keyType, valueType);
3633 }
3634
3635
3636 /**
3637 * @serial include
3638 */
3639 private static class CheckedMap<K,V>
3640 implements Map<K,V>, Serializable
3641 {
3642 @java.io.Serial
3643 private static final long serialVersionUID = 5742860141034234728L;
3644
3645 @SuppressWarnings("serial") // Not statically typed as Serializable
3646 private final Map<K, V> m;
3647 @SuppressWarnings("serial") // Not statically typed as Serializable
3648 final Class<K> keyType;
3649 @SuppressWarnings("serial") // Not statically typed as Serializable
3650 final Class<V> valueType;
3651
3652 private void typeCheck(Object key, Object value) {
3653 if (key != null && !keyType.isInstance(key))
3654 throw new ClassCastException(badKeyMsg(key));
3655
3656 if (value != null && !valueType.isInstance(value))
3657 throw new ClassCastException(badValueMsg(value));
3658 }
3659
3660 private BiFunction<? super K, ? super V, ? extends V> typeCheck(
3661 BiFunction<? super K, ? super V, ? extends V> func) {
3662 Objects.requireNonNull(func);
3663 return (k, v) -> {
3664 V newValue = func.apply(k, v);
3665 typeCheck(k, newValue);
3666 return newValue;
3667 };
3668 }
3669
4028 * @param keyType the type of key that {@code m} is permitted to hold
4029 * @param valueType the type of value that {@code m} is permitted to hold
4030 * @return a dynamically typesafe view of the specified map
4031 * @since 1.5
4032 */
4033 public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
4034 Class<K> keyType,
4035 Class<V> valueType) {
4036 return new CheckedSortedMap<>(m, keyType, valueType);
4037 }
4038
4039 /**
4040 * @serial include
4041 */
4042 static class CheckedSortedMap<K,V> extends CheckedMap<K,V>
4043 implements SortedMap<K,V>, Serializable
4044 {
4045 @java.io.Serial
4046 private static final long serialVersionUID = 1599671320688067438L;
4047
4048 @SuppressWarnings("serial") // Not statically typed as Serializable
4049 private final SortedMap<K, V> sm;
4050
4051 CheckedSortedMap(SortedMap<K, V> m,
4052 Class<K> keyType, Class<V> valueType) {
4053 super(m, keyType, valueType);
4054 sm = m;
4055 }
4056
4057 public Comparator<? super K> comparator() { return sm.comparator(); }
4058 public K firstKey() { return sm.firstKey(); }
4059 public K lastKey() { return sm.lastKey(); }
4060
4061 public SortedMap<K,V> subMap(K fromKey, K toKey) {
4062 return checkedSortedMap(sm.subMap(fromKey, toKey),
4063 keyType, valueType);
4064 }
4065 public SortedMap<K,V> headMap(K toKey) {
4066 return checkedSortedMap(sm.headMap(toKey), keyType, valueType);
4067 }
4068 public SortedMap<K,V> tailMap(K fromKey) {
4104 * @param keyType the type of key that {@code m} is permitted to hold
4105 * @param valueType the type of value that {@code m} is permitted to hold
4106 * @return a dynamically typesafe view of the specified map
4107 * @since 1.8
4108 */
4109 public static <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K, V> m,
4110 Class<K> keyType,
4111 Class<V> valueType) {
4112 return new CheckedNavigableMap<>(m, keyType, valueType);
4113 }
4114
4115 /**
4116 * @serial include
4117 */
4118 static class CheckedNavigableMap<K,V> extends CheckedSortedMap<K,V>
4119 implements NavigableMap<K,V>, Serializable
4120 {
4121 @java.io.Serial
4122 private static final long serialVersionUID = -4852462692372534096L;
4123
4124 @SuppressWarnings("serial") // Not statically typed as Serializable
4125 private final NavigableMap<K, V> nm;
4126
4127 CheckedNavigableMap(NavigableMap<K, V> m,
4128 Class<K> keyType, Class<V> valueType) {
4129 super(m, keyType, valueType);
4130 nm = m;
4131 }
4132
4133 public Comparator<? super K> comparator() { return nm.comparator(); }
4134 public K firstKey() { return nm.firstKey(); }
4135 public K lastKey() { return nm.lastKey(); }
4136
4137 public Entry<K, V> lowerEntry(K key) {
4138 Entry<K,V> lower = nm.lowerEntry(key);
4139 return (null != lower)
4140 ? new CheckedMap.CheckedEntrySet.CheckedEntry<>(lower, valueType)
4141 : null;
4142 }
4143
4144 public K lowerKey(K key) { return nm.lowerKey(key); }
4836 @Override
4837 public int characteristics() {
4838 int value = (element != null) ? Spliterator.NONNULL : 0;
4839
4840 return value | Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.IMMUTABLE |
4841 Spliterator.DISTINCT | Spliterator.ORDERED;
4842 }
4843 };
4844 }
4845
4846 /**
4847 * @serial include
4848 */
4849 private static class SingletonSet<E>
4850 extends AbstractSet<E>
4851 implements Serializable
4852 {
4853 @java.io.Serial
4854 private static final long serialVersionUID = 3193687207550431679L;
4855
4856 @SuppressWarnings("serial") // Not statically typed as Serializable
4857 private final E element;
4858
4859 SingletonSet(E e) {element = e;}
4860
4861 public Iterator<E> iterator() {
4862 return singletonIterator(element);
4863 }
4864
4865 public int size() {return 1;}
4866
4867 public boolean contains(Object o) {return eq(o, element);}
4868
4869 // Override default methods for Collection
4870 @Override
4871 public void forEach(Consumer<? super E> action) {
4872 action.accept(element);
4873 }
4874 @Override
4875 public Spliterator<E> spliterator() {
4876 return singletonSpliterator(element);
4891 *
4892 * @param <T> the class of the objects in the list
4893 * @param o the sole object to be stored in the returned list.
4894 * @return an immutable list containing only the specified object.
4895 * @since 1.3
4896 */
4897 public static <T> List<T> singletonList(T o) {
4898 return new SingletonList<>(o);
4899 }
4900
4901 /**
4902 * @serial include
4903 */
4904 private static class SingletonList<E>
4905 extends AbstractList<E>
4906 implements RandomAccess, Serializable {
4907
4908 @java.io.Serial
4909 private static final long serialVersionUID = 3093736618740652951L;
4910
4911 @SuppressWarnings("serial") // Not statically typed as Serializable
4912 private final E element;
4913
4914 SingletonList(E obj) {element = obj;}
4915
4916 public Iterator<E> iterator() {
4917 return singletonIterator(element);
4918 }
4919
4920 public int size() {return 1;}
4921
4922 public boolean contains(Object obj) {return eq(obj, element);}
4923
4924 public E get(int index) {
4925 if (index != 0)
4926 throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
4927 return element;
4928 }
4929
4930 // Override default methods for Collection
4931 @Override
4961 * @param <V> the class of the map values
4962 * @param key the sole key to be stored in the returned map.
4963 * @param value the value to which the returned map maps {@code key}.
4964 * @return an immutable map containing only the specified key-value
4965 * mapping.
4966 * @since 1.3
4967 */
4968 public static <K,V> Map<K,V> singletonMap(K key, V value) {
4969 return new SingletonMap<>(key, value);
4970 }
4971
4972 /**
4973 * @serial include
4974 */
4975 private static class SingletonMap<K,V>
4976 extends AbstractMap<K,V>
4977 implements Serializable {
4978 @java.io.Serial
4979 private static final long serialVersionUID = -6979724477215052911L;
4980
4981 @SuppressWarnings("serial") // Not statically typed as Serializable
4982 private final K k;
4983 @SuppressWarnings("serial") // Not statically typed as Serializable
4984 private final V v;
4985
4986 SingletonMap(K key, V value) {
4987 k = key;
4988 v = value;
4989 }
4990
4991 public int size() {return 1;}
4992 public boolean isEmpty() {return false;}
4993 public boolean containsKey(Object key) {return eq(key, k);}
4994 public boolean containsValue(Object value) {return eq(value, v);}
4995 public V get(Object key) {return (eq(key, k) ? v : null);}
4996
4997 private transient Set<K> keySet;
4998 private transient Set<Map.Entry<K,V>> entrySet;
4999 private transient Collection<V> values;
5000
5001 public Set<K> keySet() {
5002 if (keySet==null)
5003 keySet = singleton(k);
5102 * @see List#addAll(Collection)
5103 * @see List#addAll(int, Collection)
5104 */
5105 public static <T> List<T> nCopies(int n, T o) {
5106 if (n < 0)
5107 throw new IllegalArgumentException("List length = " + n);
5108 return new CopiesList<>(n, o);
5109 }
5110
5111 /**
5112 * @serial include
5113 */
5114 private static class CopiesList<E>
5115 extends AbstractList<E>
5116 implements RandomAccess, Serializable
5117 {
5118 @java.io.Serial
5119 private static final long serialVersionUID = 2739099268398711800L;
5120
5121 final int n;
5122 @SuppressWarnings("serial") // Not statically typed as Serializable
5123 final E element;
5124
5125 CopiesList(int n, E e) {
5126 assert n >= 0;
5127 this.n = n;
5128 element = e;
5129 }
5130
5131 public int size() {
5132 return n;
5133 }
5134
5135 public boolean contains(Object obj) {
5136 return n != 0 && eq(obj, element);
5137 }
5138
5139 public int indexOf(Object o) {
5140 return contains(o) ? 0 : -1;
5141 }
5142
5336 return new ReverseComparator2<>(cmp);
5337 }
5338 }
5339
5340 /**
5341 * @serial include
5342 */
5343 private static class ReverseComparator2<T> implements Comparator<T>,
5344 Serializable
5345 {
5346 @java.io.Serial
5347 private static final long serialVersionUID = 4374092139857L;
5348
5349 /**
5350 * The comparator specified in the static factory. This will never
5351 * be null, as the static factory returns a ReverseComparator
5352 * instance if its argument is null.
5353 *
5354 * @serial
5355 */
5356 @SuppressWarnings("serial") // Not statically typed as Serializable
5357 final Comparator<T> cmp;
5358
5359 ReverseComparator2(Comparator<T> cmp) {
5360 assert cmp != null;
5361 this.cmp = cmp;
5362 }
5363
5364 public int compare(T t1, T t2) {
5365 return cmp.compare(t2, t1);
5366 }
5367
5368 public boolean equals(Object o) {
5369 return (o == this) ||
5370 (o instanceof ReverseComparator2 &&
5371 cmp.equals(((ReverseComparator2)o).cmp));
5372 }
5373
5374 public int hashCode() {
5375 return cmp.hashCode() ^ Integer.MIN_VALUE;
5376 }
5618 * new WeakHashMap<Object, Boolean>());
5619 * </pre>
5620 *
5621 * @param <E> the class of the map keys and of the objects in the
5622 * returned set
5623 * @param map the backing map
5624 * @return the set backed by the map
5625 * @throws IllegalArgumentException if {@code map} is not empty
5626 * @since 1.6
5627 */
5628 public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
5629 return new SetFromMap<>(map);
5630 }
5631
5632 /**
5633 * @serial include
5634 */
5635 private static class SetFromMap<E> extends AbstractSet<E>
5636 implements Set<E>, Serializable
5637 {
5638 @SuppressWarnings("serial") // Not statically typed as Serializable
5639 private final Map<E, Boolean> m; // The backing map
5640 private transient Set<E> s; // Its keySet
5641
5642 SetFromMap(Map<E, Boolean> map) {
5643 if (!map.isEmpty())
5644 throw new IllegalArgumentException("Map is non-empty");
5645 m = map;
5646 s = map.keySet();
5647 }
5648
5649 public void clear() { m.clear(); }
5650 public int size() { return m.size(); }
5651 public boolean isEmpty() { return m.isEmpty(); }
5652 public boolean contains(Object o) { return m.containsKey(o); }
5653 public boolean remove(Object o) { return m.remove(o) != null; }
5654 public boolean add(E e) { return m.put(e, Boolean.TRUE) == null; }
5655 public Iterator<E> iterator() { return s.iterator(); }
5656 public Object[] toArray() { return s.toArray(); }
5657 public <T> T[] toArray(T[] a) { return s.toArray(a); }
5658 public String toString() { return s.toString(); }
5704 * one exception. The {@link Queue#addAll addAll} method is
5705 * implemented as a sequence of {@link Deque#addFirst addFirst}
5706 * invocations on the backing deque.
5707 *
5708 * @param <T> the class of the objects in the deque
5709 * @param deque the deque
5710 * @return the queue
5711 * @since 1.6
5712 */
5713 public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
5714 return new AsLIFOQueue<>(Objects.requireNonNull(deque));
5715 }
5716
5717 /**
5718 * @serial include
5719 */
5720 static class AsLIFOQueue<E> extends AbstractQueue<E>
5721 implements Queue<E>, Serializable {
5722 @java.io.Serial
5723 private static final long serialVersionUID = 1802017725587941708L;
5724 @SuppressWarnings("serial") // Not statically typed as Serializable
5725 private final Deque<E> q;
5726 AsLIFOQueue(Deque<E> q) { this.q = q; }
5727 public boolean add(E e) { q.addFirst(e); return true; }
5728 public boolean offer(E e) { return q.offerFirst(e); }
5729 public E poll() { return q.pollFirst(); }
5730 public E remove() { return q.removeFirst(); }
5731 public E peek() { return q.peekFirst(); }
5732 public E element() { return q.getFirst(); }
5733 public void clear() { q.clear(); }
5734 public int size() { return q.size(); }
5735 public boolean isEmpty() { return q.isEmpty(); }
5736 public boolean contains(Object o) { return q.contains(o); }
5737 public boolean remove(Object o) { return q.remove(o); }
5738 public Iterator<E> iterator() { return q.iterator(); }
5739 public Object[] toArray() { return q.toArray(); }
5740 public <T> T[] toArray(T[] a) { return q.toArray(a); }
5741 public <T> T[] toArray(IntFunction<T[]> f) { return q.toArray(f); }
5742 public String toString() { return q.toString(); }
5743 public boolean containsAll(Collection<?> c) { return q.containsAll(c); }
5744 public boolean removeAll(Collection<?> c) { return q.removeAll(c); }
|