# HG changeset patch
# User mduigou
# Date 1371602745 25200
# Node ID f64e4960c78e7207b9de04a609f43b645d8ddc3f
# Parent 1f7cbe4829feb151a474c6aa4ff5c918737cc1ec
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set}
Reviewed-by: dmocek, martin, smarks
diff --git a/src/share/classes/java/util/AbstractMap.java b/src/share/classes/java/util/AbstractMap.java
--- a/src/share/classes/java/util/AbstractMap.java
+++ b/src/share/classes/java/util/AbstractMap.java
@@ -543,6 +543,8 @@
/**
* Utility method for SimpleEntry and SimpleImmutableEntry.
* Test for equality, checking for nulls.
+ *
+ * NB: Do not replace with Object.equals until JDK-8015417 is resolved.
*/
private static boolean eq(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
diff --git a/src/share/classes/java/util/Collections.java b/src/share/classes/java/util/Collections.java
--- a/src/share/classes/java/util/Collections.java
+++ b/src/share/classes/java/util/Collections.java
@@ -27,6 +27,7 @@
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.IOException;
+import java.io.InvalidObjectException;
import java.lang.reflect.Array;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@@ -136,7 +137,7 @@
*
*
The implementation was adapted from Tim Peters's list sort for Python
* (
- * TimSort). It uses techiques from Peter McIlroy's "Optimistic
+ * TimSort). It uses techniques from Peter McIlroy's "Optimistic
* Sorting and Information Theoretic Complexity", in Proceedings of the
* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
* January 1993.
@@ -197,7 +198,7 @@
*
*
The implementation was adapted from Tim Peters's list sort for Python
* (
- * TimSort). It uses techiques from Peter McIlroy's "Optimistic
+ * TimSort). It uses techniques from Peter McIlroy's "Optimistic
* Sorting and Information Theoretic Complexity", in Proceedings of the
* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
* January 1993.
@@ -1212,6 +1213,94 @@
}
/**
+ * Returns an unmodifiable view of the specified navigable set. This method
+ * allows modules to provide users with "read-only" access to internal
+ * navigable sets. Query operations on the returned navigable set "read
+ * through" to the specified navigable set. Attempts to modify the returned
+ * navigable set, whether direct, via its iterator, or via its
+ * {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
+ * an {@code UnsupportedOperationException}.
+ *
+ * The returned navigable set will be serializable if the specified
+ * navigable set is serializable.
+ *
+ * @param s the navigable set for which an unmodifiable view is to be
+ * returned
+ * @return an unmodifiable view of the specified navigable set
+ * @since 1.8
+ */
+ public static NavigableSet unmodifiableNavigableSet(NavigableSet s) {
+ return new UnmodifiableNavigableSet<>(s);
+ }
+
+ /**
+ * Wraps a navigable set and disables all of the mutative operations.
+ *
+ * @param type of elements
+ * @serial include
+ */
+ static class UnmodifiableNavigableSet
+ extends UnmodifiableSortedSet
+ implements NavigableSet, Serializable {
+
+ private static final long serialVersionUID = -6027448201786391929L;
+
+ /**
+ * A singleton empty unmodifiable navigable set used for
+ * {@link #emptyNavigableSet()}.
+ *
+ * @param type of elements, if there were any, and bounds
+ */
+ private static class EmptyNavigableSet extends UnmodifiableNavigableSet
+ implements Serializable {
+ private static final long serialVersionUID = -6291252904449939134L;
+
+ public EmptyNavigableSet() {
+ super(new TreeSet());
+ }
+
+ private Object readResolve() { return EMPTY_NAVIGABLE_SET; }
+ }
+
+ @SuppressWarnings("rawtypes")
+ private static final NavigableSet> EMPTY_NAVIGABLE_SET =
+ new EmptyNavigableSet<>();
+
+ /**
+ * The instance we are protecting.
+ */
+ private final NavigableSet ns;
+
+ UnmodifiableNavigableSet(NavigableSet s) {super(s); ns = s;}
+
+ public E lower(E e) { return ns.lower(e); }
+ public E floor(E e) { return ns.floor(e); }
+ public E ceiling(E e) { return ns.ceiling(e); }
+ public E higher(E e) { return ns.higher(e); }
+ public E pollFirst() { throw new UnsupportedOperationException(); }
+ public E pollLast() { throw new UnsupportedOperationException(); }
+ public NavigableSet descendingSet()
+ { return new UnmodifiableNavigableSet<>(ns.descendingSet()); }
+ public Iterator descendingIterator()
+ { return descendingSet().iterator(); }
+
+ public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ return new UnmodifiableNavigableSet<>(
+ ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
+ }
+
+ public NavigableSet headSet(E toElement, boolean inclusive) {
+ return new UnmodifiableNavigableSet<>(
+ ns.headSet(toElement, inclusive));
+ }
+
+ public NavigableSet tailSet(E fromElement, boolean inclusive) {
+ return new UnmodifiableNavigableSet<>(
+ ns.tailSet(fromElement, inclusive));
+ }
+ }
+
+ /**
* Returns an unmodifiable view of the specified list. This method allows
* modules to provide users with "read-only" access to internal
* lists. Query operations on the returned list "read through" to the
@@ -1238,6 +1327,7 @@
static class UnmodifiableList extends UnmodifiableCollection
implements List {
private static final long serialVersionUID = -283967356065247728L;
+
final List extends E> list;
UnmodifiableList(List extends E> list) {
@@ -1600,7 +1690,8 @@
private static class UnmodifiableEntry implements Map.Entry {
private Map.Entry extends K, ? extends V> e;
- UnmodifiableEntry(Map.Entry extends K, ? extends V> e) {this.e = e;}
+ UnmodifiableEntry(Map.Entry extends K, ? extends V> e)
+ {this.e = Objects.requireNonNull(e);}
public K getKey() {return e.getKey();}
public V getValue() {return e.getValue();}
@@ -1652,24 +1743,151 @@
private final SortedMap sm;
- UnmodifiableSortedMap(SortedMap m) {super(m); sm = m;}
-
- public Comparator super K> comparator() {return sm.comparator();}
-
- public SortedMap subMap(K fromKey, K toKey) {
- return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey));
- }
- public SortedMap headMap(K toKey) {
- return new UnmodifiableSortedMap<>(sm.headMap(toKey));
- }
- public SortedMap tailMap(K fromKey) {
- return new UnmodifiableSortedMap<>(sm.tailMap(fromKey));
- }
-
- public K firstKey() {return sm.firstKey();}
- public K lastKey() {return sm.lastKey();}
+ UnmodifiableSortedMap(SortedMap m) {super(m); sm = m; }
+ public Comparator super K> comparator() { return sm.comparator(); }
+ public SortedMap subMap(K fromKey, K toKey)
+ { return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); }
+ public SortedMap headMap(K toKey)
+ { return new UnmodifiableSortedMap<>(sm.headMap(toKey)); }
+ public SortedMap tailMap(K fromKey)
+ { return new UnmodifiableSortedMap<>(sm.tailMap(fromKey)); }
+ public K firstKey() { return sm.firstKey(); }
+ public K lastKey() { return sm.lastKey(); }
}
+ /**
+ * Returns an unmodifiable view of the specified navigable map. This method
+ * allows modules to provide users with "read-only" access to internal
+ * navigable maps. Query operations on the returned navigable map "read
+ * through" to the specified navigable map. Attempts to modify the returned
+ * navigable map, whether direct, via its collection views, or via its
+ * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
+ * an {@code UnsupportedOperationException}.
+ *
+ * The returned navigable map will be serializable if the specified
+ * navigable map is serializable.
+ *
+ * @param m the navigable map for which an unmodifiable view is to be
+ * returned
+ * @return an unmodifiable view of the specified navigable map
+ * @since 1.8
+ */
+ public static NavigableMap unmodifiableNavigableMap(NavigableMap m) {
+ return new UnmodifiableNavigableMap<>(m);
+ }
+
+ /**
+ * @serial include
+ */
+ static class UnmodifiableNavigableMap
+ extends UnmodifiableSortedMap
+ implements NavigableMap, Serializable {
+ private static final long serialVersionUID = -4858195264774772197L;
+
+ /**
+ * A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve
+ * to preserve singleton property.
+ *
+ * @param type of keys, if there were any, and of bounds
+ * @param type of values, if there were any
+ */
+ private static class EmptyNavigableMap extends UnmodifiableNavigableMap
+ implements Serializable {
+
+ private static final long serialVersionUID = -2239321462712562324L;
+
+ EmptyNavigableMap() { super(new TreeMap()); }
+
+ @Override
+ public NavigableSet navigableKeySet()
+ { return emptyNavigableSet(); }
+
+ private Object readResolve() { return EMPTY_NAVIGABLE_MAP; }
+ }
+
+ /**
+ * Singleton for {@link emptyNavigableMap()} which is also immutable.
+ */
+ private static final EmptyNavigableMap,?> EMPTY_NAVIGABLE_MAP =
+ new EmptyNavigableMap<>();
+
+ /**
+ * The instance we wrap and protect.
+ */
+ private final NavigableMap nm;
+
+ UnmodifiableNavigableMap(NavigableMap m)
+ {super(m); nm = m;}
+
+ public K lowerKey(K key) { return nm.lowerKey(key); }
+ public K floorKey(K key) { return nm.floorKey(key); }
+ public K ceilingKey(K key) { return nm.ceilingKey(key); }
+ public K higherKey(K key) { return nm.higherKey(key); }
+
+ public Entry lowerEntry(K key) {
+ Entry lower = (Entry) nm.lowerEntry(key);
+ return (null != lower)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(lower)
+ : null;
+ }
+
+ public Entry floorEntry(K key) {
+ Entry floor = (Entry) nm.floorEntry(key);
+ return (null != floor)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(floor)
+ : null;
+ }
+
+ public Entry ceilingEntry(K key) {
+ Entry ceiling = (Entry) nm.ceilingEntry(key);
+ return (null != ceiling)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(ceiling)
+ : null;
+ }
+
+
+ public Entry higherEntry(K key) {
+ Entry higher = (Entry) nm.higherEntry(key);
+ return (null != higher)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(higher)
+ : null;
+ }
+
+ public Entry firstEntry() {
+ Entry first = (Entry) nm.firstEntry();
+ return (null != first)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(first)
+ : null;
+ }
+
+ public Entry lastEntry() {
+ Entry last = (Entry) nm.lastEntry();
+ return (null != last)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(last)
+ : null;
+ }
+
+ public Entry pollFirstEntry()
+ { throw new UnsupportedOperationException(); }
+ public Entry pollLastEntry()
+ { throw new UnsupportedOperationException(); }
+ public NavigableMap descendingMap()
+ { return unmodifiableNavigableMap(nm.descendingMap()); }
+ public NavigableSet navigableKeySet()
+ { return unmodifiableNavigableSet(nm.navigableKeySet()); }
+ public NavigableSet descendingKeySet()
+ { return unmodifiableNavigableSet(nm.descendingKeySet()); }
+
+ public NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ return unmodifiableNavigableMap(
+ nm.subMap(fromKey, fromInclusive, toKey, toInclusive));
+ }
+
+ public NavigableMap headMap(K toKey, boolean inclusive)
+ { return unmodifiableNavigableMap(nm.headMap(toKey, inclusive)); }
+ public NavigableMap tailMap(K fromKey, boolean inclusive)
+ { return unmodifiableNavigableMap(nm.tailMap(fromKey, inclusive)); }
+ }
// Synch Wrappers
@@ -1723,14 +1941,13 @@
final Object mutex; // Object on which to synchronize
SynchronizedCollection(Collection c) {
- if (c==null)
- throw new NullPointerException();
- this.c = c;
+ this.c = Objects.requireNonNull(c);
mutex = this;
}
+
SynchronizedCollection(Collection c, Object mutex) {
- this.c = c;
- this.mutex = mutex;
+ this.c = Objects.requireNonNull(c);
+ this.mutex = Objects.requireNonNull(mutex);
}
public int size() {
@@ -1945,6 +2162,120 @@
}
/**
+ * Returns a synchronized (thread-safe) navigable set backed by the
+ * specified navigable set. In order to guarantee serial access, it is
+ * critical that all access to the backing navigable set is
+ * accomplished through the returned navigable set (or its views).
+ *
+ * It is imperative that the user manually synchronize on the returned
+ * navigable set when iterating over it or any of its {@code subSet},
+ * {@code headSet}, or {@code tailSet} views.
+ *
+ * NavigableSet s = Collections.synchronizedNavigableSet(new TreeSet());
+ * ...
+ * synchronized (s) {
+ * Iterator i = s.iterator(); // Must be in the synchronized block
+ * while (i.hasNext())
+ * foo(i.next());
+ * }
+ *
+ * or:
+ *
+ * NavigableSet s = Collections.synchronizedNavigableSet(new TreeSet());
+ * NavigableSet s2 = s.headSet(foo, true);
+ * ...
+ * synchronized (s) { // Note: s, not s2!!!
+ * Iterator i = s2.iterator(); // Must be in the synchronized block
+ * while (i.hasNext())
+ * foo(i.next());
+ * }
+ *
+ * Failure to follow this advice may result in non-deterministic behavior.
+ *
+ * The returned navigable set will be serializable if the specified
+ * navigable set is serializable.
+ *
+ * @param s the navigable set to be "wrapped" in a synchronized navigable
+ * set
+ * @return a synchronized view of the specified navigable set
+ * @since 1.8
+ */
+ public static NavigableSet synchronizedNavigableSet(NavigableSet s) {
+ return new SynchronizedNavigableSet<>(s);
+ }
+
+ /**
+ * @serial include
+ */
+ static class SynchronizedNavigableSet
+ extends SynchronizedSortedSet
+ implements NavigableSet
+ {
+ private static final long serialVersionUID = -5505529816273629798L;
+
+ private final NavigableSet ns;
+
+ SynchronizedNavigableSet(NavigableSet s) {
+ super(s);
+ ns = s;
+ }
+
+ SynchronizedNavigableSet(NavigableSet s, Object mutex) {
+ super(s, mutex);
+ ns = s;
+ }
+ public E lower(E e) { synchronized (mutex) {return ns.lower(e);} }
+ public E floor(E e) { synchronized (mutex) {return ns.floor(e);} }
+ public E ceiling(E e) { synchronized (mutex) {return ns.ceiling(e);} }
+ public E higher(E e) { synchronized (mutex) {return ns.higher(e);} }
+ public E pollFirst() { synchronized (mutex) {return ns.pollFirst();} }
+ public E pollLast() { synchronized (mutex) {return ns.pollLast();} }
+
+ public NavigableSet descendingSet() {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.descendingSet(), mutex);
+ }
+ }
+
+ public Iterator descendingIterator()
+ { synchronized (mutex) { return descendingSet().iterator(); } }
+
+ public NavigableSet subSet(E fromElement, E toElement) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.subSet(fromElement, true, toElement, false), mutex);
+ }
+ }
+ public NavigableSet headSet(E toElement) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.headSet(toElement, false), mutex);
+ }
+ }
+ public NavigableSet tailSet(E fromElement) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet(ns.tailSet(fromElement, true), mutex);
+ }
+ }
+
+ public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex);
+ }
+ }
+
+ public NavigableSet headSet(E toElement, boolean inclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.headSet(toElement, inclusive), mutex);
+ }
+ }
+
+ public NavigableSet tailSet(E fromElement, boolean inclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.tailSet(fromElement, inclusive));
+ }
+ }
+ }
+
+ /**
* Returns a synchronized (thread-safe) list backed by the specified
* list. In order to guarantee serial access, it is critical that
* all access to the backing list is accomplished
@@ -2153,9 +2484,7 @@
final Object mutex; // Object on which to synchronize
SynchronizedMap(Map m) {
- if (m==null)
- throw new NullPointerException();
- this.m = m;
+ this.m = Objects.requireNonNull(m);
mutex = this;
}
@@ -2334,7 +2663,6 @@
return new SynchronizedSortedMap<>(m);
}
-
/**
* @serial include
*/
@@ -2384,6 +2712,164 @@
}
}
+ /**
+ * Returns a synchronized (thread-safe) navigable map backed by the
+ * specified navigable map. In order to guarantee serial access, it is
+ * critical that all access to the backing navigable map is
+ * accomplished through the returned navigable map (or its views).
+ *
+ * It is imperative that the user manually synchronize on the returned
+ * navigable map when iterating over any of its collection views, or the
+ * collections views of any of its {@code subMap}, {@code headMap} or
+ * {@code tailMap} views.
+ *
+ * NavigableMap m = Collections.synchronizedNavigableMap(new TreeMap());
+ * ...
+ * Set s = m.keySet(); // Needn't be in synchronized block
+ * ...
+ * synchronized (m) { // Synchronizing on m, not s!
+ * Iterator i = s.iterator(); // Must be in synchronized block
+ * while (i.hasNext())
+ * foo(i.next());
+ * }
+ *
+ * or:
+ *
+ * NavigableMap m = Collections.synchronizedNavigableMap(new TreeMap());
+ * NavigableMap m2 = m.subMap(foo, true, bar, false);
+ * ...
+ * Set s2 = m2.keySet(); // Needn't be in synchronized block
+ * ...
+ * synchronized (m) { // Synchronizing on m, not m2 or s2!
+ * Iterator i = s.iterator(); // Must be in synchronized block
+ * while (i.hasNext())
+ * foo(i.next());
+ * }
+ *
+ * Failure to follow this advice may result in non-deterministic behavior.
+ *
+ * The returned navigable map will be serializable if the specified
+ * navigable map is serializable.
+ *
+ * @param m the navigable map to be "wrapped" in a synchronized navigable
+ * map
+ * @return a synchronized view of the specified navigable map.
+ * @since 1.8
+ */
+ public static NavigableMap synchronizedNavigableMap(NavigableMap m) {
+ return new SynchronizedNavigableMap<>(m);
+ }
+
+ /**
+ * A synchronized NavigableMap.
+ *
+ * @serial include
+ */
+ static class SynchronizedNavigableMap
+ extends SynchronizedSortedMap
+ implements NavigableMap
+ {
+ private static final long serialVersionUID = 699392247599746807L;
+
+ private final NavigableMap nm;
+
+ SynchronizedNavigableMap(NavigableMap m) {
+ super(m);
+ nm = m;
+ }
+ SynchronizedNavigableMap(NavigableMap m, Object mutex) {
+ super(m, mutex);
+ nm = m;
+ }
+
+ public Entry lowerEntry(K key)
+ { synchronized (mutex) { return nm.lowerEntry(key); } }
+ public K lowerKey(K key)
+ { synchronized (mutex) { return nm.lowerKey(key); } }
+ public Entry floorEntry(K key)
+ { synchronized (mutex) { return nm.floorEntry(key); } }
+ public K floorKey(K key)
+ { synchronized (mutex) { return nm.floorKey(key); } }
+ public Entry ceilingEntry(K key)
+ { synchronized (mutex) { return nm.ceilingEntry(key); } }
+ public K ceilingKey(K key)
+ { synchronized (mutex) { return nm.ceilingKey(key); } }
+ public Entry higherEntry(K key)
+ { synchronized (mutex) { return nm.higherEntry(key); } }
+ public K higherKey(K key)
+ { synchronized (mutex) { return nm.higherKey(key); } }
+ public Entry firstEntry()
+ { synchronized (mutex) { return nm.firstEntry(); } }
+ public Entry lastEntry()
+ { synchronized (mutex) { return nm.lastEntry(); } }
+ public Entry pollFirstEntry()
+ { synchronized (mutex) { return nm.pollFirstEntry(); } }
+ public Entry pollLastEntry()
+ { synchronized (mutex) { return nm.pollLastEntry(); } }
+
+ public NavigableMap descendingMap() {
+ synchronized (mutex) {
+ return
+ new SynchronizedNavigableMap(nm.descendingMap(), mutex);
+ }
+ }
+
+ public NavigableSet keySet() {
+ return navigableKeySet();
+ }
+
+ public NavigableSet navigableKeySet() {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet(nm.navigableKeySet(), mutex);
+ }
+ }
+
+ public NavigableSet descendingKeySet() {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet(nm.descendingKeySet(), mutex);
+ }
+ }
+
+
+ public SortedMap subMap(K fromKey, K toKey) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap<>(
+ nm.subMap(fromKey, true, toKey, false), mutex);
+ }
+ }
+ public SortedMap headMap(K toKey) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap<>(nm.headMap(toKey, false), mutex);
+ }
+ }
+ public SortedMap tailMap(K fromKey) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap<>(nm.tailMap(fromKey, true),mutex);
+ }
+ }
+
+ public NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap(
+ nm.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex);
+ }
+ }
+
+ public NavigableMap headMap(K toKey, boolean inclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap(
+ nm.headMap(toKey, inclusive), mutex);
+ }
+ }
+
+ public NavigableMap tailMap(K fromKey, boolean inclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap(
+ nm.tailMap(fromKey, inclusive), mutex);
+ }
+ }
+ }
+
// Dynamically typesafe collection wrappers
/**
@@ -2415,12 +2901,12 @@
* program to wrap the collection with a dynamically typesafe view.
* For example, this declaration:
* {@code
- * Collection c = new HashSet();
+ * Collection c = new HashSet<>();
* }
* may be replaced temporarily by this one:
* {@code
* Collection c = Collections.checkedCollection(
- * new HashSet(), String.class);
+ * new HashSet<>(), String.class);
* }
* Running the program again will cause it to fail at the point where
* an incorrectly typed element is inserted into the collection, clearly
@@ -2706,6 +3192,7 @@
implements SortedSet, Serializable
{
private static final long serialVersionUID = 1599911165492914959L;
+
private final SortedSet ss;
CheckedSortedSet(SortedSet s, Class type) {
@@ -2728,6 +3215,87 @@
}
}
+/**
+ * Returns a dynamically typesafe view of the specified navigable set.
+ * Any attempt to insert an element of the wrong type will result in an
+ * immediate {@link ClassCastException}. Assuming a navigable set
+ * contains no incorrectly typed elements prior to the time a
+ * dynamically typesafe view is generated, and that all subsequent
+ * access to the navigable set takes place through the view, it is
+ * guaranteed that the navigable set cannot contain an incorrectly
+ * typed element.
+ *
+ * A discussion of the use of dynamically typesafe views may be
+ * found in the documentation for the {@link #checkedCollection
+ * checkedCollection} method.
+ *
+ *
The returned navigable set will be serializable if the specified
+ * navigable set is serializable.
+ *
+ *
Since {@code null} is considered to be a value of any reference
+ * type, the returned navigable set permits insertion of null elements
+ * whenever the backing sorted set does.
+ *
+ * @param s the navigable set for which a dynamically typesafe view is to be
+ * returned
+ * @param type the type of element that {@code s} is permitted to hold
+ * @return a dynamically typesafe view of the specified navigable set
+ * @since 1.8
+ */
+ public static NavigableSet checkedNavigableSet(NavigableSet s,
+ Class type) {
+ return new CheckedNavigableSet<>(s, type);
+ }
+
+ /**
+ * @serial include
+ */
+ static class CheckedNavigableSet extends CheckedSortedSet
+ implements NavigableSet, Serializable
+ {
+ private static final long serialVersionUID = -5429120189805438922L;
+
+ private final NavigableSet ns;
+
+ CheckedNavigableSet(NavigableSet s, Class type) {
+ super(s, type);
+ ns = s;
+ }
+
+ public E lower(E e) { return ns.lower(e); }
+ public E floor(E e) { return ns.floor(e); }
+ public E ceiling(E e) { return ns.ceiling(e); }
+ public E higher(E e) { return ns.higher(e); }
+ public E pollFirst() { return ns.pollFirst(); }
+ public E pollLast() {return ns.pollLast(); }
+ public NavigableSet descendingSet()
+ { return checkedNavigableSet(ns.descendingSet(), type); }
+ public Iterator descendingIterator()
+ {return checkedNavigableSet(ns.descendingSet(), type).iterator(); }
+
+ public NavigableSet subSet(E fromElement, E toElement) {
+ return checkedNavigableSet(ns.subSet(fromElement, true, toElement, false), type);
+ }
+ public NavigableSet headSet(E toElement) {
+ return checkedNavigableSet(ns.headSet(toElement, false), type);
+ }
+ public NavigableSet tailSet(E fromElement) {
+ return checkedNavigableSet(ns.tailSet(fromElement, true), type);
+ }
+
+ public NavigableSet subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ return checkedNavigableSet(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), type);
+ }
+
+ public NavigableSet headSet(E toElement, boolean inclusive) {
+ return checkedNavigableSet(ns.headSet(toElement, inclusive), type);
+ }
+
+ public NavigableSet tailSet(E fromElement, boolean inclusive) {
+ return checkedNavigableSet(ns.tailSet(fromElement, inclusive), type);
+ }
+ }
+
/**
* Returns a dynamically typesafe view of the specified list.
* Any attempt to insert an element of the wrong type will result in
@@ -2940,11 +3508,9 @@
}
CheckedMap(Map m, Class keyType, Class valueType) {
- if (m == null || keyType == null || valueType == null)
- throw new NullPointerException();
- this.m = m;
- this.keyType = keyType;
- this.valueType = valueType;
+ this.m = Objects.requireNonNull(m);
+ this.keyType = Objects.requireNonNull(keyType);
+ this.valueType = Objects.requireNonNull(valueType);
}
public int size() { return m.size(); }
@@ -3221,8 +3787,8 @@
private final Class valueType;
CheckedEntry(Map.Entry e, Class valueType) {
- this.e = e;
- this.valueType = valueType;
+ this.e = Objects.requireNonNull(e);
+ this.valueType = Objects.requireNonNull(valueType);
}
public K getKey() { return e.getKey(); }
@@ -3325,6 +3891,177 @@
}
}
+ /**
+ * Returns a dynamically typesafe view of the specified navigable map.
+ * Any attempt to insert a mapping whose key or value have the wrong
+ * type will result in an immediate {@link ClassCastException}.
+ * Similarly, any attempt to modify the value currently associated with
+ * a key will result in an immediate {@link ClassCastException},
+ * whether the modification is attempted directly through the map
+ * itself, or through a {@link Map.Entry} instance obtained from the
+ * map's {@link Map#entrySet() entry set} view.
+ *
+ * Assuming a map contains no incorrectly typed keys or values
+ * prior to the time a dynamically typesafe view is generated, and
+ * that all subsequent access to the map takes place through the view
+ * (or one of its collection views), it is guaranteed that the
+ * map cannot contain an incorrectly typed key or value.
+ *
+ *
A discussion of the use of dynamically typesafe views may be
+ * found in the documentation for the {@link #checkedCollection
+ * checkedCollection} method.
+ *
+ *
The returned map will be serializable if the specified map is
+ * serializable.
+ *
+ *
Since {@code null} is considered to be a value of any reference
+ * type, the returned map permits insertion of null keys or values
+ * whenever the backing map does.
+ *
+ * @param type of map keys
+ * @param type of map values
+ * @param m the map for which a dynamically typesafe view is to be
+ * returned
+ * @param keyType the type of key that {@code m} is permitted to hold
+ * @param valueType the type of value that {@code m} is permitted to hold
+ * @return a dynamically typesafe view of the specified map
+ * @since 1.8
+ */
+ public static NavigableMap checkedNavigableMap(NavigableMap m,
+ Class keyType,
+ Class valueType) {
+ return new CheckedNavigableMap<>(m, keyType, valueType);
+ }
+
+ /**
+ * @serial include
+ */
+ static class CheckedNavigableMap extends CheckedSortedMap
+ implements NavigableMap, Serializable
+ {
+ private static final long serialVersionUID = -4852462692372534096L;
+
+ private final NavigableMap nm;
+
+ CheckedNavigableMap(NavigableMap m,
+ Class keyType, Class valueType) {
+ super(m, keyType, valueType);
+ nm = m;
+ }
+
+ public Comparator super K> comparator() { return nm.comparator(); }
+ public K firstKey() { return nm.firstKey(); }
+ public K lastKey() { return nm.lastKey(); }
+
+ public Entry lowerEntry(K key) {
+ Entry lower = nm.lowerEntry(key);
+ return (null != lower)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(lower, valueType)
+ : null;
+ }
+
+ public K lowerKey(K key) { return nm.lowerKey(key); }
+
+ public Entry floorEntry(K key) {
+ Entry floor = nm.floorEntry(key);
+ return (null != floor)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(floor, valueType)
+ : null;
+ }
+
+ public K floorKey(K key) { return nm.floorKey(key); }
+
+ public Entry ceilingEntry(K key) {
+ Entry ceiling = nm.ceilingEntry(key);
+ return (null != ceiling)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(ceiling, valueType)
+ : null;
+ }
+
+ public K ceilingKey(K key) { return nm.ceilingKey(key); }
+
+ public Entry higherEntry(K key) {
+ Entry higher = nm.higherEntry(key);
+ return (null != higher)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(higher, valueType)
+ : null;
+ }
+
+ public K higherKey(K key) { return nm.higherKey(key); }
+
+ public Entry firstEntry() {
+ Entry first = nm.firstEntry();
+ return (null != first)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(first, valueType)
+ : null;
+ }
+
+ public Entry lastEntry() {
+ Entry last = nm.lastEntry();
+ return (null != last)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(last, valueType)
+ : null;
+ }
+
+ public Entry pollFirstEntry() {
+ Entry entry = nm.pollFirstEntry();
+ return (null == entry)
+ ? null
+ : new CheckedMap.CheckedEntrySet.CheckedEntry(entry, valueType);
+ }
+
+ public Entry pollLastEntry() {
+ Entry entry = nm.pollLastEntry();
+ return (null == entry)
+ ? null
+ : new CheckedMap.CheckedEntrySet.CheckedEntry(entry, valueType);
+ }
+
+ public NavigableMap descendingMap() {
+ return checkedNavigableMap(nm.descendingMap(), keyType, valueType);
+ }
+
+ public NavigableSet keySet() {
+ return navigableKeySet();
+ }
+
+ public NavigableSet navigableKeySet() {
+ return checkedNavigableSet(nm.navigableKeySet(), keyType);
+ }
+
+ public NavigableSet descendingKeySet() {
+ return checkedNavigableSet(nm.descendingKeySet(), keyType);
+ }
+
+ @Override
+ public NavigableMap subMap(K fromKey, K toKey) {
+ return checkedNavigableMap(nm.subMap(fromKey, true, toKey, false),
+ keyType, valueType);
+ }
+
+ @Override
+ public NavigableMap headMap(K toKey) {
+ return checkedNavigableMap(nm.headMap(toKey, false), keyType, valueType);
+ }
+
+ @Override
+ public NavigableMap tailMap(K fromKey) {
+ return checkedNavigableMap(nm.tailMap(fromKey, true), keyType, valueType);
+ }
+
+ public NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ return checkedNavigableMap(nm.subMap(fromKey, fromInclusive, toKey, toInclusive), keyType, valueType);
+ }
+
+ public NavigableMap headMap(K toKey, boolean inclusive) {
+ return checkedNavigableMap(nm.headMap(toKey, inclusive), keyType, valueType);
+ }
+
+ public NavigableMap tailMap(K fromKey, boolean inclusive) {
+ return checkedNavigableMap(nm.tailMap(fromKey, inclusive), keyType, valueType);
+ }
+ }
+
// Empty collections
/**
@@ -3346,6 +4083,7 @@
* Implementations of this method are permitted, but not
* required, to return the same object from multiple invocations.
*
+ * @param type of elements, if there were any, in the iterator
* @return an empty iterator
* @since 1.7
*/
@@ -3396,6 +4134,7 @@
* Implementations of this method are permitted, but not
* required, to return the same object from multiple invocations.
*
+ * @param type of elements, if there were any, in the iterator
* @return an empty list iterator
* @since 1.7
*/
@@ -3460,17 +4199,19 @@
public static final Set EMPTY_SET = new EmptySet<>();
/**
- * Returns the empty set (immutable). This set is serializable.
+ * Returns an empty set (immutable). This set is serializable.
* Unlike the like-named field, this method is parameterized.
*
* This example illustrates the type-safe way to obtain an empty set:
*
* Set<String> s = Collections.emptySet();
*
- * Implementation note: Implementations of this method need not
- * create a separate Set object for each call. Using this
- * method is likely to have comparable cost to using the like-named
- * field. (Unlike this method, the field does not provide type safety.)
+ * @implNote Implementations of this method need not create a separate
+ * {@code Set} object for each call. Using this method is likely to have
+ * comparable cost to using the like-named field. (Unlike this method, the
+ * field does not provide type safety.)
+ *
+ * @return the empty set
*
* @see #EMPTY_SET
* @since 1.5
@@ -3525,121 +4266,45 @@
}
/**
- * Returns the empty sorted set (immutable). This set is serializable.
- *
- * This example illustrates the type-safe way to obtain an empty sorted
- * set:
- *
- * SortedSet<String> s = Collections.emptySortedSet();
- *
- * Implementation note: Implementations of this method need not
- * create a separate SortedSet object for each call.
- *
+ * Returns an empty sorted set (immutable). This set is serializable.
+ *
+ * This example illustrates the type-safe way to obtain an empty
+ * sorted set:
+ *
{@code
+ * SortedSet s = Collections.emptySortedSet();
+ * }
+ *
+ * @implNote Implementations of this method need not create a separate
+ * {@code SortedSet} object for each call.
+ *
+ * @param type of elements, if there were any, in the set
+ * @return the empty sorted set
* @since 1.8
*/
@SuppressWarnings("unchecked")
- public static final SortedSet emptySortedSet() {
- return (SortedSet) new EmptySortedSet<>();
+ public static SortedSet emptySortedSet() {
+ return (SortedSet) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET;
}
/**
- * @serial include
+ * Returns an empty navigable set (immutable). This set is serializable.
+ *
+ * This example illustrates the type-safe way to obtain an empty
+ * navigable set:
+ *
{@code
+ * NavigableSet s = Collections.emptyNavigableSet();
+ * }
+ *
+ * @implNote Implementations of this method need not
+ * create a separate {@code NavigableSet} object for each call.
+ *
+ * @param type of elements, if there were any, in the set
+ * @return the empty navigable set
+ * @since 1.8
*/
- private static class EmptySortedSet
- extends AbstractSet
- implements SortedSet, Serializable
- {
- private static final long serialVersionUID = 6316515401502265487L;
- public Iterator iterator() { return emptyIterator(); }
- public int size() {return 0;}
- public boolean isEmpty() {return true;}
- public boolean contains(Object obj) {return false;}
- public boolean containsAll(Collection> c) { return c.isEmpty(); }
- public Object[] toArray() { return new Object[0]; }
-
- public E[] toArray(E[] a) {
- if (a.length > 0)
- a[0] = null;
- return a;
- }
-
- // Preserves singleton property
- private Object readResolve() {
- return new EmptySortedSet<>();
- }
-
- @Override
- public Comparator super E> comparator() {
- return null;
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public SortedSet subSet(Object fromElement, Object toElement) {
- Objects.requireNonNull(fromElement);
- Objects.requireNonNull(toElement);
-
- if (!(fromElement instanceof Comparable) ||
- !(toElement instanceof Comparable))
- {
- throw new ClassCastException();
- }
-
- if ((((Comparable)fromElement).compareTo(toElement) >= 0) ||
- (((Comparable)toElement).compareTo(fromElement) < 0))
- {
- throw new IllegalArgumentException();
- }
-
- return emptySortedSet();
- }
-
- @Override
- public SortedSet headSet(Object toElement) {
- Objects.requireNonNull(toElement);
-
- if (!(toElement instanceof Comparable)) {
- throw new ClassCastException();
- }
-
- return emptySortedSet();
- }
-
- @Override
- public SortedSet tailSet(Object fromElement) {
- Objects.requireNonNull(fromElement);
-
- if (!(fromElement instanceof Comparable)) {
- throw new ClassCastException();
- }
-
- return emptySortedSet();
- }
-
- @Override
- public E first() {
- throw new NoSuchElementException();
- }
-
- @Override
- public E last() {
- throw new NoSuchElementException();
- }
-
- // Override default methods in Collection
- @Override
- public void forEach(Consumer super E> action) {
- Objects.requireNonNull(action);
- }
-
- @Override
- public boolean removeIf(Predicate super E> filter) {
- Objects.requireNonNull(filter);
- return false;
- }
-
- @Override
- public Spliterator spliterator() { return Spliterators.emptySpliterator(); }
+ @SuppressWarnings("unchecked")
+ public static NavigableSet emptyNavigableSet() {
+ return (NavigableSet) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET;
}
/**
@@ -3651,7 +4316,7 @@
public static final List EMPTY_LIST = new EmptyList<>();
/**
- * Returns the empty list (immutable). This list is serializable.
+ * Returns an empty list (immutable). This list is serializable.
*
* This example illustrates the type-safe way to obtain an empty list:
*
@@ -3662,6 +4327,9 @@
* method is likely to have comparable cost to using the like-named
* field. (Unlike this method, the field does not provide type safety.)
*
+ * @param type of elements, if there were any, in the list
+ * @return an empty immutable list
+ *
* @see #EMPTY_LIST
* @since 1.5
*/
@@ -3748,17 +4416,18 @@
public static final Map EMPTY_MAP = new EmptyMap<>();
/**
- * Returns the empty map (immutable). This map is serializable.
- *
- * This example illustrates the type-safe way to obtain an empty set:
+ * Returns an empty map (immutable). This map is serializable.
+ *
+ *
This example illustrates the type-safe way to obtain an empty map:
*
* Map<String, Date> s = Collections.emptyMap();
*
- * Implementation note: Implementations of this method need not
- * create a separate Map object for each call. Using this
- * method is likely to have comparable cost to using the like-named
- * field. (Unlike this method, the field does not provide type safety.)
- *
+ * @implNote Implementations of this method need not create a separate
+ * {@code Map} object for each call. Using this method is likely to have
+ * comparable cost to using the like-named field. (Unlike this method, the
+ * field does not provide type safety.)
+ *
+ * @return an empty map
* @see #EMPTY_MAP
* @since 1.5
*/
@@ -3768,6 +4437,44 @@
}
/**
+ * Returns an empty sorted map (immutable). This map is serializable.
+ *
+ * This example illustrates the type-safe way to obtain an empty map:
+ *
{@code
+ * SortedMap s = Collections.emptySortedMap();
+ * }
+ *
+ * @implNote Implementations of this method need not create a separate
+ * {@code SortedMap} object for each call.
+ *
+ * @return an empty sorted map
+ * @since 1.8
+ */
+ @SuppressWarnings("unchecked")
+ public static final SortedMap emptySortedMap() {
+ return (SortedMap) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP;
+ }
+
+ /**
+ * Returns an empty navigable map (immutable). This map is serializable.
+ *
+ * This example illustrates the type-safe way to obtain an empty map:
+ *
{@code
+ * NavigableMap s = Collections.emptyNavigableMap();
+ * }
+ *
+ * @implNote Implementations of this method need not create a separate
+ * {@code NavigableMap} object for each call.
+ *
+ * @return an empty navigable map
+ * @since 1.8
+ */
+ @SuppressWarnings("unchecked")
+ public static final NavigableMap emptyNavigableMap() {
+ return (NavigableMap) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP;
+ }
+
+ /**
* @serial include
*/
private static class EmptyMap
@@ -4071,15 +4778,11 @@
v = value;
}
- public int size() {return 1;}
-
- public boolean isEmpty() {return false;}
-
- public boolean containsKey(Object key) {return eq(key, k);}
-
- public boolean containsValue(Object value) {return eq(value, v);}
-
- public V get(Object key) {return (eq(key, k) ? v : null);}
+ public int size() {return 1;}
+ public boolean isEmpty() {return false;}
+ public boolean containsKey(Object key) {return eq(key, k);}
+ public boolean containsValue(Object value) {return eq(value, v);}
+ public V get(Object key) {return (eq(key, k) ? v : null);}
private transient Set keySet = null;
private transient Set> entrySet = null;
@@ -4416,6 +5119,8 @@
/**
* Returns true if the specified arguments are equal, or both null.
+ *
+ * NB: Do not replace with Object.equals until JDK-8015417 is resolved.
*/
static boolean eq(Object o1, Object o2) {
return o1==null ? o2==null : o1.equals(o2);
diff --git a/src/share/classes/java/util/NavigableSet.java b/src/share/classes/java/util/NavigableSet.java
--- a/src/share/classes/java/util/NavigableSet.java
+++ b/src/share/classes/java/util/NavigableSet.java
@@ -303,7 +303,7 @@
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
-na */
+ */
SortedSet headSet(E toElement);
/**
diff --git a/test/java/util/Collection/MOAT.java b/test/java/util/Collection/MOAT.java
--- a/test/java/util/Collection/MOAT.java
+++ b/test/java/util/Collection/MOAT.java
@@ -71,6 +71,14 @@
testCollection(new LinkedList());
testCollection(new LinkedList().subList(0,0));
testCollection(new TreeSet());
+ testCollection(Collections.checkedList(new ArrayList(), Integer.class));
+ testCollection(Collections.synchronizedList(new ArrayList()));
+ testCollection(Collections.checkedSet(new HashSet(), Integer.class));
+ testCollection(Collections.checkedSortedSet(new TreeSet(), Integer.class));
+ testCollection(Collections.checkedNavigableSet(new TreeSet(), Integer.class));
+ testCollection(Collections.synchronizedSet(new HashSet()));
+ testCollection(Collections.synchronizedSortedSet(new TreeSet()));
+ testCollection(Collections.synchronizedNavigableSet(new TreeSet()));
testCollection(new CopyOnWriteArrayList());
testCollection(new CopyOnWriteArrayList().subList(0,0));
@@ -98,6 +106,12 @@
testMap(new Hashtable());
testMap(new ConcurrentHashMap(10, 0.5f));
testMap(new ConcurrentSkipListMap());
+ testMap(Collections.checkedMap(new HashMap(), Integer.class, Integer.class));
+ testMap(Collections.checkedSortedMap(new TreeMap(), Integer.class, Integer.class));
+ testMap(Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class));
+ testMap(Collections.synchronizedMap(new HashMap()));
+ testMap(Collections.synchronizedSortedMap(new TreeMap()));
+ testMap(Collections.synchronizedNavigableMap(new TreeMap()));
// Empty collections
final List emptyArray = Arrays.asList(new Integer[]{});
@@ -117,19 +131,29 @@
testCollection(emptySet);
testEmptySet(emptySet);
testEmptySet(EMPTY_SET);
+ testEmptySet(Collections.emptySet());
+ testEmptySet(Collections.emptySortedSet());
+ testEmptySet(Collections.emptyNavigableSet());
testImmutableSet(emptySet);
List emptyList = emptyList();
testCollection(emptyList);
testEmptyList(emptyList);
testEmptyList(EMPTY_LIST);
+ testEmptyList(Collections.emptyList());
testImmutableList(emptyList);
Map emptyMap = emptyMap();
testMap(emptyMap);
testEmptyMap(emptyMap);
testEmptyMap(EMPTY_MAP);
+ testEmptyMap(Collections.emptyMap());
+ testEmptyMap(Collections.emptySortedMap());
+ testEmptyMap(Collections.emptyNavigableMap());
testImmutableMap(emptyMap);
+ testImmutableMap(Collections.emptyMap());
+ testImmutableMap(Collections.emptySortedMap());
+ testImmutableMap(Collections.emptyNavigableMap());
// Singleton collections
Set