--- a/jdk/src/share/classes/java/util/AbstractMap.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/AbstractMap.java Fri Jul 12 12:15:59 2013 -0700
@@ -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);
--- a/jdk/src/share/classes/java/util/Collections.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/Collections.java Fri Jul 12 12:15:59 2013 -0700
@@ -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;
@@ -138,7 +139,7 @@
*
* <p>The implementation was adapted from Tim Peters's list sort for Python
* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
- * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic
+ * TimSort</a>). 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.
@@ -199,7 +200,7 @@
*
* <p>The implementation was adapted from Tim Peters's list sort for Python
* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
- * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic
+ * TimSort</a>). 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.
@@ -1214,6 +1215,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}.<p>
+ *
+ * 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 <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) {
+ return new UnmodifiableNavigableSet<>(s);
+ }
+
+ /**
+ * Wraps a navigable set and disables all of the mutative operations.
+ *
+ * @param <E> type of elements
+ * @serial include
+ */
+ static class UnmodifiableNavigableSet<E>
+ extends UnmodifiableSortedSet<E>
+ implements NavigableSet<E>, Serializable {
+
+ private static final long serialVersionUID = -6027448201786391929L;
+
+ /**
+ * A singleton empty unmodifiable navigable set used for
+ * {@link #emptyNavigableSet()}.
+ *
+ * @param <E> type of elements, if there were any, and bounds
+ */
+ private static class EmptyNavigableSet<E> extends UnmodifiableNavigableSet<E>
+ implements Serializable {
+ private static final long serialVersionUID = -6291252904449939134L;
+
+ public EmptyNavigableSet() {
+ super(new TreeSet<E>());
+ }
+
+ 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<E> ns;
+
+ UnmodifiableNavigableSet(NavigableSet<E> 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<E> descendingSet()
+ { return new UnmodifiableNavigableSet<>(ns.descendingSet()); }
+ public Iterator<E> descendingIterator()
+ { return descendingSet().iterator(); }
+
+ public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ return new UnmodifiableNavigableSet<>(
+ ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
+ }
+
+ public NavigableSet<E> headSet(E toElement, boolean inclusive) {
+ return new UnmodifiableNavigableSet<>(
+ ns.headSet(toElement, inclusive));
+ }
+
+ public NavigableSet<E> 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
@@ -1240,6 +1329,7 @@
static class UnmodifiableList<E> extends UnmodifiableCollection<E>
implements List<E> {
private static final long serialVersionUID = -283967356065247728L;
+
final List<? extends E> list;
UnmodifiableList(List<? extends E> list) {
@@ -1682,7 +1772,8 @@
private static class UnmodifiableEntry<K,V> implements Map.Entry<K,V> {
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();}
@@ -1734,24 +1825,151 @@
private final SortedMap<K, ? extends V> sm;
- UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m;}
-
- public Comparator<? super K> comparator() {return sm.comparator();}
-
- public SortedMap<K,V> subMap(K fromKey, K toKey) {
- return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey));
- }
- public SortedMap<K,V> headMap(K toKey) {
- return new UnmodifiableSortedMap<>(sm.headMap(toKey));
- }
- public SortedMap<K,V> tailMap(K fromKey) {
- return new UnmodifiableSortedMap<>(sm.tailMap(fromKey));
- }
-
- public K firstKey() {return sm.firstKey();}
- public K lastKey() {return sm.lastKey();}
+ UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; }
+ public Comparator<? super K> comparator() { return sm.comparator(); }
+ public SortedMap<K,V> subMap(K fromKey, K toKey)
+ { return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); }
+ public SortedMap<K,V> headMap(K toKey)
+ { return new UnmodifiableSortedMap<>(sm.headMap(toKey)); }
+ public SortedMap<K,V> 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}.<p>
+ *
+ * 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 <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K, ? extends V> m) {
+ return new UnmodifiableNavigableMap<>(m);
}
+ /**
+ * @serial include
+ */
+ static class UnmodifiableNavigableMap<K,V>
+ extends UnmodifiableSortedMap<K,V>
+ implements NavigableMap<K,V>, Serializable {
+ private static final long serialVersionUID = -4858195264774772197L;
+
+ /**
+ * A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve
+ * to preserve singleton property.
+ *
+ * @param <K> type of keys, if there were any, and of bounds
+ * @param <V> type of values, if there were any
+ */
+ private static class EmptyNavigableMap<K,V> extends UnmodifiableNavigableMap<K,V>
+ implements Serializable {
+
+ private static final long serialVersionUID = -2239321462712562324L;
+
+ EmptyNavigableMap() { super(new TreeMap()); }
+
+ @Override
+ public NavigableSet<K> 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<K, ? extends V> nm;
+
+ UnmodifiableNavigableMap(NavigableMap<K, ? extends V> 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<K, V> lowerEntry(K key) {
+ Entry<K,V> lower = (Entry<K, V>) nm.lowerEntry(key);
+ return (null != lower)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(lower)
+ : null;
+ }
+
+ public Entry<K, V> floorEntry(K key) {
+ Entry<K,V> floor = (Entry<K, V>) nm.floorEntry(key);
+ return (null != floor)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(floor)
+ : null;
+ }
+
+ public Entry<K, V> ceilingEntry(K key) {
+ Entry<K,V> ceiling = (Entry<K, V>) nm.ceilingEntry(key);
+ return (null != ceiling)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(ceiling)
+ : null;
+ }
+
+
+ public Entry<K, V> higherEntry(K key) {
+ Entry<K,V> higher = (Entry<K, V>) nm.higherEntry(key);
+ return (null != higher)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(higher)
+ : null;
+ }
+
+ public Entry<K, V> firstEntry() {
+ Entry<K,V> first = (Entry<K, V>) nm.firstEntry();
+ return (null != first)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(first)
+ : null;
+ }
+
+ public Entry<K, V> lastEntry() {
+ Entry<K,V> last = (Entry<K, V>) nm.lastEntry();
+ return (null != last)
+ ? new UnmodifiableEntrySet.UnmodifiableEntry(last)
+ : null;
+ }
+
+ public Entry<K, V> pollFirstEntry()
+ { throw new UnsupportedOperationException(); }
+ public Entry<K, V> pollLastEntry()
+ { throw new UnsupportedOperationException(); }
+ public NavigableMap<K, V> descendingMap()
+ { return unmodifiableNavigableMap(nm.descendingMap()); }
+ public NavigableSet<K> navigableKeySet()
+ { return unmodifiableNavigableSet(nm.navigableKeySet()); }
+ public NavigableSet<K> descendingKeySet()
+ { return unmodifiableNavigableSet(nm.descendingKeySet()); }
+
+ public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ return unmodifiableNavigableMap(
+ nm.subMap(fromKey, fromInclusive, toKey, toInclusive));
+ }
+
+ public NavigableMap<K, V> headMap(K toKey, boolean inclusive)
+ { return unmodifiableNavigableMap(nm.headMap(toKey, inclusive)); }
+ public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive)
+ { return unmodifiableNavigableMap(nm.tailMap(fromKey, inclusive)); }
+ }
// Synch Wrappers
@@ -1805,14 +2023,13 @@
final Object mutex; // Object on which to synchronize
SynchronizedCollection(Collection<E> c) {
- if (c==null)
- throw new NullPointerException();
- this.c = c;
+ this.c = Objects.requireNonNull(c);
mutex = this;
}
+
SynchronizedCollection(Collection<E> c, Object mutex) {
- this.c = c;
- this.mutex = mutex;
+ this.c = Objects.requireNonNull(c);
+ this.mutex = Objects.requireNonNull(mutex);
}
public int size() {
@@ -2027,6 +2244,120 @@
}
/**
+ * Returns a synchronized (thread-safe) navigable set backed by the
+ * specified navigable set. In order to guarantee serial access, it is
+ * critical that <strong>all</strong> access to the backing navigable set is
+ * accomplished through the returned navigable set (or its views).<p>
+ *
+ * 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.
+ * <pre>
+ * NavigableSet s = Collections.synchronizedNavigableSet(new TreeSet());
+ * ...
+ * synchronized (s) {
+ * Iterator i = s.iterator(); // Must be in the synchronized block
+ * while (i.hasNext())
+ * foo(i.next());
+ * }
+ * </pre>
+ * or:
+ * <pre>
+ * 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());
+ * }
+ * </pre>
+ * Failure to follow this advice may result in non-deterministic behavior.
+ *
+ * <p>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 <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) {
+ return new SynchronizedNavigableSet<>(s);
+ }
+
+ /**
+ * @serial include
+ */
+ static class SynchronizedNavigableSet<E>
+ extends SynchronizedSortedSet<E>
+ implements NavigableSet<E>
+ {
+ private static final long serialVersionUID = -5505529816273629798L;
+
+ private final NavigableSet<E> ns;
+
+ SynchronizedNavigableSet(NavigableSet<E> s) {
+ super(s);
+ ns = s;
+ }
+
+ SynchronizedNavigableSet(NavigableSet<E> 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<E> descendingSet() {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.descendingSet(), mutex);
+ }
+ }
+
+ public Iterator<E> descendingIterator()
+ { synchronized (mutex) { return descendingSet().iterator(); } }
+
+ public NavigableSet<E> subSet(E fromElement, E toElement) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.subSet(fromElement, true, toElement, false), mutex);
+ }
+ }
+ public NavigableSet<E> headSet(E toElement) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.headSet(toElement, false), mutex);
+ }
+ }
+ public NavigableSet<E> tailSet(E fromElement) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet(ns.tailSet(fromElement, true), mutex);
+ }
+ }
+
+ public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex);
+ }
+ }
+
+ public NavigableSet<E> headSet(E toElement, boolean inclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet<>(ns.headSet(toElement, inclusive), mutex);
+ }
+ }
+
+ public NavigableSet<E> 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
* <strong>all</strong> access to the backing list is accomplished
@@ -2235,9 +2566,7 @@
final Object mutex; // Object on which to synchronize
SynchronizedMap(Map<K,V> m) {
- if (m==null)
- throw new NullPointerException();
- this.m = m;
+ this.m = Objects.requireNonNull(m);
mutex = this;
}
@@ -2416,7 +2745,6 @@
return new SynchronizedSortedMap<>(m);
}
-
/**
* @serial include
*/
@@ -2466,6 +2794,164 @@
}
}
+ /**
+ * Returns a synchronized (thread-safe) navigable map backed by the
+ * specified navigable map. In order to guarantee serial access, it is
+ * critical that <strong>all</strong> access to the backing navigable map is
+ * accomplished through the returned navigable map (or its views).<p>
+ *
+ * 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.
+ * <pre>
+ * 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());
+ * }
+ * </pre>
+ * or:
+ * <pre>
+ * 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());
+ * }
+ * </pre>
+ * Failure to follow this advice may result in non-deterministic behavior.
+ *
+ * <p>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 <K,V> NavigableMap<K,V> synchronizedNavigableMap(NavigableMap<K,V> m) {
+ return new SynchronizedNavigableMap<>(m);
+ }
+
+ /**
+ * A synchronized NavigableMap.
+ *
+ * @serial include
+ */
+ static class SynchronizedNavigableMap<K,V>
+ extends SynchronizedSortedMap<K,V>
+ implements NavigableMap<K,V>
+ {
+ private static final long serialVersionUID = 699392247599746807L;
+
+ private final NavigableMap<K,V> nm;
+
+ SynchronizedNavigableMap(NavigableMap<K,V> m) {
+ super(m);
+ nm = m;
+ }
+ SynchronizedNavigableMap(NavigableMap<K,V> m, Object mutex) {
+ super(m, mutex);
+ nm = m;
+ }
+
+ public Entry<K, V> lowerEntry(K key)
+ { synchronized (mutex) { return nm.lowerEntry(key); } }
+ public K lowerKey(K key)
+ { synchronized (mutex) { return nm.lowerKey(key); } }
+ public Entry<K, V> floorEntry(K key)
+ { synchronized (mutex) { return nm.floorEntry(key); } }
+ public K floorKey(K key)
+ { synchronized (mutex) { return nm.floorKey(key); } }
+ public Entry<K, V> ceilingEntry(K key)
+ { synchronized (mutex) { return nm.ceilingEntry(key); } }
+ public K ceilingKey(K key)
+ { synchronized (mutex) { return nm.ceilingKey(key); } }
+ public Entry<K, V> higherEntry(K key)
+ { synchronized (mutex) { return nm.higherEntry(key); } }
+ public K higherKey(K key)
+ { synchronized (mutex) { return nm.higherKey(key); } }
+ public Entry<K, V> firstEntry()
+ { synchronized (mutex) { return nm.firstEntry(); } }
+ public Entry<K, V> lastEntry()
+ { synchronized (mutex) { return nm.lastEntry(); } }
+ public Entry<K, V> pollFirstEntry()
+ { synchronized (mutex) { return nm.pollFirstEntry(); } }
+ public Entry<K, V> pollLastEntry()
+ { synchronized (mutex) { return nm.pollLastEntry(); } }
+
+ public NavigableMap<K, V> descendingMap() {
+ synchronized (mutex) {
+ return
+ new SynchronizedNavigableMap(nm.descendingMap(), mutex);
+ }
+ }
+
+ public NavigableSet<K> keySet() {
+ return navigableKeySet();
+ }
+
+ public NavigableSet<K> navigableKeySet() {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet(nm.navigableKeySet(), mutex);
+ }
+ }
+
+ public NavigableSet<K> descendingKeySet() {
+ synchronized (mutex) {
+ return new SynchronizedNavigableSet(nm.descendingKeySet(), mutex);
+ }
+ }
+
+
+ public SortedMap<K,V> subMap(K fromKey, K toKey) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap<>(
+ nm.subMap(fromKey, true, toKey, false), mutex);
+ }
+ }
+ public SortedMap<K,V> headMap(K toKey) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap<>(nm.headMap(toKey, false), mutex);
+ }
+ }
+ public SortedMap<K,V> tailMap(K fromKey) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap<>(nm.tailMap(fromKey, true),mutex);
+ }
+ }
+
+ public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap(
+ nm.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex);
+ }
+ }
+
+ public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap(
+ nm.headMap(toKey, inclusive), mutex);
+ }
+ }
+
+ public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
+ synchronized (mutex) {
+ return new SynchronizedNavigableMap(
+ nm.tailMap(fromKey, inclusive), mutex);
+ }
+ }
+ }
+
// Dynamically typesafe collection wrappers
/**
@@ -2497,12 +2983,12 @@
* program to wrap the collection with a dynamically typesafe view.
* For example, this declaration:
* <pre> {@code
- * Collection<String> c = new HashSet<String>();
+ * Collection<String> c = new HashSet<>();
* }</pre>
* may be replaced temporarily by this one:
* <pre> {@code
* Collection<String> c = Collections.checkedCollection(
- * new HashSet<String>(), String.class);
+ * new HashSet<>(), String.class);
* }</pre>
* Running the program again will cause it to fail at the point where
* an incorrectly typed element is inserted into the collection, clearly
@@ -2788,6 +3274,7 @@
implements SortedSet<E>, Serializable
{
private static final long serialVersionUID = 1599911165492914959L;
+
private final SortedSet<E> ss;
CheckedSortedSet(SortedSet<E> s, Class<E> type) {
@@ -2810,6 +3297,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
+ * <em>guaranteed</em> that the navigable set cannot contain an incorrectly
+ * typed element.
+ *
+ * <p>A discussion of the use of dynamically typesafe views may be
+ * found in the documentation for the {@link #checkedCollection
+ * checkedCollection} method.
+ *
+ * <p>The returned navigable set will be serializable if the specified
+ * navigable set is serializable.
+ *
+ * <p>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 <E> NavigableSet<E> checkedNavigableSet(NavigableSet<E> s,
+ Class<E> type) {
+ return new CheckedNavigableSet<>(s, type);
+ }
+
+ /**
+ * @serial include
+ */
+ static class CheckedNavigableSet<E> extends CheckedSortedSet<E>
+ implements NavigableSet<E>, Serializable
+ {
+ private static final long serialVersionUID = -5429120189805438922L;
+
+ private final NavigableSet<E> ns;
+
+ CheckedNavigableSet(NavigableSet<E> s, Class<E> 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<E> descendingSet()
+ { return checkedNavigableSet(ns.descendingSet(), type); }
+ public Iterator<E> descendingIterator()
+ {return checkedNavigableSet(ns.descendingSet(), type).iterator(); }
+
+ public NavigableSet<E> subSet(E fromElement, E toElement) {
+ return checkedNavigableSet(ns.subSet(fromElement, true, toElement, false), type);
+ }
+ public NavigableSet<E> headSet(E toElement) {
+ return checkedNavigableSet(ns.headSet(toElement, false), type);
+ }
+ public NavigableSet<E> tailSet(E fromElement) {
+ return checkedNavigableSet(ns.tailSet(fromElement, true), type);
+ }
+
+ public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
+ return checkedNavigableSet(ns.subSet(fromElement, fromInclusive, toElement, toInclusive), type);
+ }
+
+ public NavigableSet<E> headSet(E toElement, boolean inclusive) {
+ return checkedNavigableSet(ns.headSet(toElement, inclusive), type);
+ }
+
+ public NavigableSet<E> 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
@@ -3022,11 +3590,9 @@
}
CheckedMap(Map<K, V> m, Class<K> keyType, Class<V> 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(); }
@@ -3303,8 +3869,8 @@
private final Class<T> valueType;
CheckedEntry(Map.Entry<K, V> e, Class<T> valueType) {
- this.e = e;
- this.valueType = valueType;
+ this.e = Objects.requireNonNull(e);
+ this.valueType = Objects.requireNonNull(valueType);
}
public K getKey() { return e.getKey(); }
@@ -3407,6 +3973,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.
+ *
+ * <p>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 <em>guaranteed</em> that the
+ * map cannot contain an incorrectly typed key or value.
+ *
+ * <p>A discussion of the use of dynamically typesafe views may be
+ * found in the documentation for the {@link #checkedCollection
+ * checkedCollection} method.
+ *
+ * <p>The returned map will be serializable if the specified map is
+ * serializable.
+ *
+ * <p>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 <K> type of map keys
+ * @param <V> 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 <K,V> NavigableMap<K,V> checkedNavigableMap(NavigableMap<K, V> m,
+ Class<K> keyType,
+ Class<V> valueType) {
+ return new CheckedNavigableMap<>(m, keyType, valueType);
+ }
+
+ /**
+ * @serial include
+ */
+ static class CheckedNavigableMap<K,V> extends CheckedSortedMap<K,V>
+ implements NavigableMap<K,V>, Serializable
+ {
+ private static final long serialVersionUID = -4852462692372534096L;
+
+ private final NavigableMap<K, V> nm;
+
+ CheckedNavigableMap(NavigableMap<K, V> m,
+ Class<K> keyType, Class<V> 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<K, V> lowerEntry(K key) {
+ Entry<K,V> 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<K, V> floorEntry(K key) {
+ Entry<K,V> 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<K, V> ceilingEntry(K key) {
+ Entry<K,V> 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<K, V> higherEntry(K key) {
+ Entry<K,V> 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<K, V> firstEntry() {
+ Entry<K,V> first = nm.firstEntry();
+ return (null != first)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(first, valueType)
+ : null;
+ }
+
+ public Entry<K, V> lastEntry() {
+ Entry<K,V> last = nm.lastEntry();
+ return (null != last)
+ ? new CheckedMap.CheckedEntrySet.CheckedEntry(last, valueType)
+ : null;
+ }
+
+ public Entry<K, V> pollFirstEntry() {
+ Entry<K,V> entry = nm.pollFirstEntry();
+ return (null == entry)
+ ? null
+ : new CheckedMap.CheckedEntrySet.CheckedEntry(entry, valueType);
+ }
+
+ public Entry<K, V> pollLastEntry() {
+ Entry<K,V> entry = nm.pollLastEntry();
+ return (null == entry)
+ ? null
+ : new CheckedMap.CheckedEntrySet.CheckedEntry(entry, valueType);
+ }
+
+ public NavigableMap<K, V> descendingMap() {
+ return checkedNavigableMap(nm.descendingMap(), keyType, valueType);
+ }
+
+ public NavigableSet<K> keySet() {
+ return navigableKeySet();
+ }
+
+ public NavigableSet<K> navigableKeySet() {
+ return checkedNavigableSet(nm.navigableKeySet(), keyType);
+ }
+
+ public NavigableSet<K> descendingKeySet() {
+ return checkedNavigableSet(nm.descendingKeySet(), keyType);
+ }
+
+ @Override
+ public NavigableMap<K,V> subMap(K fromKey, K toKey) {
+ return checkedNavigableMap(nm.subMap(fromKey, true, toKey, false),
+ keyType, valueType);
+ }
+
+ @Override
+ public NavigableMap<K,V> headMap(K toKey) {
+ return checkedNavigableMap(nm.headMap(toKey, false), keyType, valueType);
+ }
+
+ @Override
+ public NavigableMap<K,V> tailMap(K fromKey) {
+ return checkedNavigableMap(nm.tailMap(fromKey, true), keyType, valueType);
+ }
+
+ public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
+ return checkedNavigableMap(nm.subMap(fromKey, fromInclusive, toKey, toInclusive), keyType, valueType);
+ }
+
+ public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
+ return checkedNavigableMap(nm.headMap(toKey, inclusive), keyType, valueType);
+ }
+
+ public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
+ return checkedNavigableMap(nm.tailMap(fromKey, inclusive), keyType, valueType);
+ }
+ }
+
// Empty collections
/**
@@ -3428,6 +4165,7 @@
* <p>Implementations of this method are permitted, but not
* required, to return the same object from multiple invocations.
*
+ * @param <T> type of elements, if there were any, in the iterator
* @return an empty iterator
* @since 1.7
*/
@@ -3478,6 +4216,7 @@
* <p>Implementations of this method are permitted, but not
* required, to return the same object from multiple invocations.
*
+ * @param <T> type of elements, if there were any, in the iterator
* @return an empty list iterator
* @since 1.7
*/
@@ -3542,17 +4281,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.
*
* <p>This example illustrates the type-safe way to obtain an empty set:
* <pre>
* Set<String> s = Collections.emptySet();
* </pre>
- * Implementation note: Implementations of this method need not
- * create a separate <tt>Set</tt> 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
@@ -3607,121 +4348,45 @@
}
/**
- * Returns the empty sorted set (immutable). This set is serializable.
- *
- * <p>This example illustrates the type-safe way to obtain an empty sorted
- * set:
- * <pre>
- * SortedSet<String> s = Collections.emptySortedSet();
- * </pre>
- * Implementation note: Implementations of this method need not
- * create a separate <tt>SortedSet</tt> object for each call.
- *
+ * Returns an empty sorted set (immutable). This set is serializable.
+ *
+ * <p>This example illustrates the type-safe way to obtain an empty
+ * sorted set:
+ * <pre> {@code
+ * SortedSet<String> s = Collections.emptySortedSet();
+ * }</pre>
+ *
+ * @implNote Implementations of this method need not create a separate
+ * {@code SortedSet} object for each call.
+ *
+ * @param <E> type of elements, if there were any, in the set
+ * @return the empty sorted set
* @since 1.8
*/
@SuppressWarnings("unchecked")
- public static final <E> SortedSet<E> emptySortedSet() {
- return (SortedSet<E>) new EmptySortedSet<>();
+ public static <E> SortedSet<E> emptySortedSet() {
+ return (SortedSet<E>) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET;
}
/**
- * @serial include
+ * Returns an empty navigable set (immutable). This set is serializable.
+ *
+ * <p>This example illustrates the type-safe way to obtain an empty
+ * navigable set:
+ * <pre> {@code
+ * NavigableSet<String> s = Collections.emptyNavigableSet();
+ * }</pre>
+ *
+ * @implNote Implementations of this method need not
+ * create a separate {@code NavigableSet} object for each call.
+ *
+ * @param <E> type of elements, if there were any, in the set
+ * @return the empty navigable set
+ * @since 1.8
*/
- private static class EmptySortedSet<E>
- extends AbstractSet<E>
- implements SortedSet<E>, Serializable
- {
- private static final long serialVersionUID = 6316515401502265487L;
- public Iterator<E> 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> 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<E> 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<E> headSet(Object toElement) {
- Objects.requireNonNull(toElement);
-
- if (!(toElement instanceof Comparable)) {
- throw new ClassCastException();
- }
-
- return emptySortedSet();
- }
-
- @Override
- public SortedSet<E> 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<E> spliterator() { return Spliterators.emptySpliterator(); }
+ @SuppressWarnings("unchecked")
+ public static <E> NavigableSet<E> emptyNavigableSet() {
+ return (NavigableSet<E>) UnmodifiableNavigableSet.EMPTY_NAVIGABLE_SET;
}
/**
@@ -3733,7 +4398,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.
*
* <p>This example illustrates the type-safe way to obtain an empty list:
* <pre>
@@ -3744,6 +4409,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 <T> type of elements, if there were any, in the list
+ * @return an empty immutable list
+ *
* @see #EMPTY_LIST
* @since 1.5
*/
@@ -3830,17 +4498,18 @@
public static final Map EMPTY_MAP = new EmptyMap<>();
/**
- * Returns the empty map (immutable). This map is serializable.
- *
- * <p>This example illustrates the type-safe way to obtain an empty set:
+ * Returns an empty map (immutable). This map is serializable.
+ *
+ * <p>This example illustrates the type-safe way to obtain an empty map:
* <pre>
* Map<String, Date> s = Collections.emptyMap();
* </pre>
- * Implementation note: Implementations of this method need not
- * create a separate <tt>Map</tt> 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
*/
@@ -3850,6 +4519,44 @@
}
/**
+ * Returns an empty sorted map (immutable). This map is serializable.
+ *
+ * <p>This example illustrates the type-safe way to obtain an empty map:
+ * <pre> {@code
+ * SortedMap<String, Date> s = Collections.emptySortedMap();
+ * }</pre>
+ *
+ * @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 <K,V> SortedMap<K,V> emptySortedMap() {
+ return (SortedMap<K,V>) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP;
+ }
+
+ /**
+ * Returns an empty navigable map (immutable). This map is serializable.
+ *
+ * <p>This example illustrates the type-safe way to obtain an empty map:
+ * <pre> {@code
+ * NavigableMap<String, Date> s = Collections.emptyNavigableMap();
+ * }</pre>
+ *
+ * @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 <K,V> NavigableMap<K,V> emptyNavigableMap() {
+ return (NavigableMap<K,V>) UnmodifiableNavigableMap.EMPTY_NAVIGABLE_MAP;
+ }
+
+ /**
* @serial include
*/
private static class EmptyMap<K,V>
@@ -4153,15 +4860,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<K> keySet = null;
private transient Set<Map.Entry<K,V>> entrySet = null;
@@ -4508,6 +5211,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);
--- a/jdk/src/share/classes/java/util/NavigableSet.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/NavigableSet.java Fri Jul 12 12:15:59 2013 -0700
@@ -303,7 +303,7 @@
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
-na */
+ */
SortedSet<E> headSet(E toElement);
/**
--- a/jdk/src/share/classes/java/util/Optional.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/Optional.java Fri Jul 12 12:15:59 2013 -0700
@@ -25,6 +25,8 @@
package java.util;
import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
import java.util.function.Supplier;
/**
@@ -52,7 +54,7 @@
private final T value;
/**
- * Construct an empty instance.
+ * Constructs an empty instance.
*
* @implNote Generally only one empty instance, {@link Optional#EMPTY},
* should exist per VM.
@@ -80,7 +82,7 @@
}
/**
- * Construct an instance with the value present.
+ * Constructs an instance with the value present.
*
* @param value the non-null value to be present
*/
@@ -89,7 +91,7 @@
}
/**
- * Return an {@code Optional} with the specified present value.
+ * Returns an {@code Optional} with the specified present non-null value.
*
* @param value the value to be present, which must be non-null
* @return an {@code Optional} with the value present
@@ -99,6 +101,18 @@
}
/**
+ * Returns an {@code Optional} describing the specified value, if non-null,
+ * otherwise returns an empty {@code Optional}.
+ *
+ * @param value the possibly-null value to describe
+ * @return an {@code Optional} with a present value if the specified value
+ * is non-null, otherwise an empty {@code Optional}
+ */
+ public static <T> Optional<T> ofNullable(T value) {
+ return value == null ? empty() : of(value);
+ }
+
+ /**
* If a value is present in this {@code Optional}, returns the value,
* otherwise throws {@code NoSuchElementException}.
*
@@ -124,7 +138,7 @@
}
/**
- * Have the specified consumer accept the value if a value is present,
+ * If a value is present, invoke the specified consumer with the value,
* otherwise do nothing.
*
* @param consumer block to be executed if a value is present
@@ -137,6 +151,89 @@
}
/**
+ * If a value is present, and the value matches the given predicate,
+ * return an {@code Optional} describing the value, otherwise return an
+ * empty {@code Optional}.
+ *
+ * @param predicate a predicate to apply to the value, if present
+ * @return an {@code Optional} describing the value of this {@code Optional}
+ * if a value is present and the value matches the given predicate,
+ * otherwise an empty {@code Optional}
+ * @throws NullPointerException if the predicate is null
+ */
+ public Optional<T> filter(Predicate<? super T> predicate) {
+ Objects.requireNonNull(predicate);
+ if (!isPresent())
+ return this;
+ else
+ return predicate.test(value) ? this : empty();
+ }
+
+ /**
+ * If a value is present, apply the provided mapping function to it,
+ * and if the result is non-null, return an {@code Optional} describing the
+ * result. Otherwise return an empty {@code Optional}.
+ *
+ * @apiNote This method supports post-processing on optional values, without
+ * the need to explicitly check for a return status. For example, the
+ * following code traverses a stream of file names, selects one that has
+ * not yet been processed, and then opens that file, returning an
+ * {@code Optional<FileInputStream>}:
+ *
+ * <pre>{@code
+ * Optional<FileInputStream> fis =
+ * names.stream().filter(name -> !isProcessedYet(name))
+ * .findFirst()
+ * .map(name -> new FileInputStream(name));
+ * }</pre>
+ *
+ * Here, {@code findFirst} returns an {@code Optional<String>}, and then
+ * {@code map} returns an {@code Optional<FileInputStream>} for the desired
+ * file if one exists.
+ *
+ * @param <U> The type of the result of the mapping function
+ * @param mapper a mapping function to apply to the value, if present
+ * @return an {@code Optional} describing the result of applying a mapping
+ * function to the value of this {@code Optional}, if a value is present,
+ * otherwise an empty {@code Optional}
+ * @throws NullPointerException if the mapping function is null
+ */
+ public<U> Optional<U> map(Function<? super T, ? extends U> mapper) {
+ Objects.requireNonNull(mapper);
+ if (!isPresent())
+ return empty();
+ else {
+ return Optional.ofNullable(mapper.apply(value));
+ }
+ }
+
+ /**
+ * If a value is present, apply the provided {@code Optional}-bearing
+ * mapping function to it, return that result, otherwise return an empty
+ * {@code Optional}. This method is similar to {@link #map(Function)},
+ * but the provided mapper is one whose result is already an {@code Optional},
+ * and if invoked, {@code flatMap} does not wrap it with an additional
+ * {@code Optional}.
+ *
+ * @param <U> The type parameter to the {@code Optional} returned by
+ * @param mapper a mapping function to apply to the value, if present
+ * the mapping function
+ * @return the result of applying an {@code Optional}-bearing mapping
+ * function to the value of this {@code Optional}, if a value is present,
+ * otherwise an empty {@code Optional}
+ * @throws NullPointerException if the mapping function is null or returns
+ * a null result
+ */
+ public<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper) {
+ Objects.requireNonNull(mapper);
+ if (!isPresent())
+ return empty();
+ else {
+ return Objects.requireNonNull(mapper.apply(value));
+ }
+ }
+
+ /**
* Return the value if present, otherwise return {@code other}.
*
* @param other the value to be returned if there is no value present, may
--- a/jdk/src/share/classes/java/util/OptionalDouble.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/OptionalDouble.java Fri Jul 12 12:15:59 2013 -0700
@@ -186,10 +186,10 @@
}
/**
- * Indicates whether some other object is "equal to" this Optional. The
+ * Indicates whether some other object is "equal to" this OptionalDouble. The
* other object is considered equal if:
* <ul>
- * <li>it is also an {@code OptionalInt} and;
+ * <li>it is also an {@code OptionalDouble} and;
* <li>both instances have no value present or;
* <li>the present values are "equal to" each other via {@code Double.compare() == 0}.
* </ul>
@@ -226,12 +226,14 @@
}
/**
- * Returns a non-empty string representation of this OptionalDouble suitable for
+ * {@inheritDoc}
+ *
+ * Returns a non-empty string representation of this object suitable for
* debugging. The exact presentation format is unspecified and may vary
* between implementations and versions.
*
* @implSpec If a value is present the result must include its string
- * representation in the result. Empty and present OptionalDoubless must be
+ * representation in the result. Empty and present instances must be
* unambiguously differentiable.
*
* @return the string representation of this instance
--- a/jdk/src/share/classes/java/util/OptionalInt.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/OptionalInt.java Fri Jul 12 12:15:59 2013 -0700
@@ -186,7 +186,7 @@
}
/**
- * Indicates whether some other object is "equal to" this Optional. The
+ * Indicates whether some other object is "equal to" this OptionalInt. The
* other object is considered equal if:
* <ul>
* <li>it is also an {@code OptionalInt} and;
@@ -226,12 +226,14 @@
}
/**
- * Returns a non-empty string representation of this OptionalInt suitable for
+ * {@inheritDoc}
+ *
+ * Returns a non-empty string representation of this object suitable for
* debugging. The exact presentation format is unspecified and may vary
* between implementations and versions.
*
* @implSpec If a value is present the result must include its string
- * representation in the result. Empty and present OptionalInts must be
+ * representation in the result. Empty and present instances must be
* unambiguously differentiable.
*
* @return the string representation of this instance
--- a/jdk/src/share/classes/java/util/OptionalLong.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/OptionalLong.java Fri Jul 12 12:15:59 2013 -0700
@@ -186,10 +186,10 @@
}
/**
- * Indicates whether some other object is "equal to" this Optional. The
+ * Indicates whether some other object is "equal to" this OptionalLong. The
* other object is considered equal if:
* <ul>
- * <li>it is also an {@code OptionalInt} and;
+ * <li>it is also an {@code OptionalLong} and;
* <li>both instances have no value present or;
* <li>the present values are "equal to" each other via {@code ==}.
* </ul>
@@ -226,12 +226,14 @@
}
/**
- * Returns a non-empty string representation of this OptionalLong suitable for
+ * {@inheritDoc}
+ *
+ * Returns a non-empty string representation of this object suitable for
* debugging. The exact presentation format is unspecified and may vary
* between implementations and versions.
*
* @implSpec If a value is present the result must include its string
- * representation in the result. Empty and present OptionalLongs must be
+ * representation in the result. Empty and present instances must be
* unambiguously differentiable.
*
* @return the string representation of this instance
--- a/jdk/src/share/classes/java/util/stream/DoubleStream.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/stream/DoubleStream.java Fri Jul 12 12:15:59 2013 -0700
@@ -746,4 +746,26 @@
return StreamSupport.doubleStream(
new StreamSpliterators.InfiniteSupplyingSpliterator.OfDouble(Long.MAX_VALUE, s));
}
+
+ /**
+ * Creates a lazy concatenated {@code DoubleStream} whose elements are all the
+ * elements of a first {@code DoubleStream} succeeded by all the elements of the
+ * second {@code DoubleStream}. The resulting stream is ordered if both
+ * of the input streams are ordered, and parallel if either of the input
+ * streams is parallel.
+ *
+ * @param a the first stream
+ * @param b the second stream to concatenate on to end of the first stream
+ * @return the concatenation of the two streams
+ */
+ public static DoubleStream concat(DoubleStream a, DoubleStream b) {
+ Objects.requireNonNull(a);
+ Objects.requireNonNull(b);
+
+ Spliterator.OfDouble split = new Streams.ConcatSpliterator.OfDouble(
+ a.spliterator(), b.spliterator());
+ return (a.isParallel() || b.isParallel())
+ ? StreamSupport.doubleParallelStream(split)
+ : StreamSupport.doubleStream(split);
+ }
}
--- a/jdk/src/share/classes/java/util/stream/IntStream.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/stream/IntStream.java Fri Jul 12 12:15:59 2013 -0700
@@ -800,4 +800,26 @@
new Streams.RangeIntSpliterator(startInclusive, endInclusive, true));
}
}
+
+ /**
+ * Creates a lazy concatenated {@code IntStream} whose elements are all the
+ * elements of a first {@code IntStream} succeeded by all the elements of the
+ * second {@code IntStream}. The resulting stream is ordered if both
+ * of the input streams are ordered, and parallel if either of the input
+ * streams is parallel.
+ *
+ * @param a the first stream
+ * @param b the second stream to concatenate on to end of the first stream
+ * @return the concatenation of the two streams
+ */
+ public static IntStream concat(IntStream a, IntStream b) {
+ Objects.requireNonNull(a);
+ Objects.requireNonNull(b);
+
+ Spliterator.OfInt split = new Streams.ConcatSpliterator.OfInt(
+ a.spliterator(), b.spliterator());
+ return (a.isParallel() || b.isParallel())
+ ? StreamSupport.intParallelStream(split)
+ : StreamSupport.intStream(split);
+ }
}
--- a/jdk/src/share/classes/java/util/stream/LongStream.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/stream/LongStream.java Fri Jul 12 12:15:59 2013 -0700
@@ -765,10 +765,8 @@
// Split the range in two and concatenate
// Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then
// the lower range, [Long.MIN_VALUE, 0) will be further split in two
-// long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1;
-// return Streams.concat(range(startInclusive, m), range(m, endExclusive));
- // This is temporary until Streams.concat is supported
- throw new UnsupportedOperationException();
+ long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1;
+ return concat(range(startInclusive, m), range(m, endExclusive));
} else {
return StreamSupport.longStream(
new Streams.RangeLongSpliterator(startInclusive, endExclusive, false));
@@ -801,13 +799,33 @@
// Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then
// the lower range, [Long.MIN_VALUE, 0), and upper range,
// [0, Long.MAX_VALUE], will both be further split in two
-// long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
-// return Streams.concat(range(startInclusive, m), rangeClosed(m, endInclusive));
- // This is temporary until Streams.concat is supported
- throw new UnsupportedOperationException();
+ long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
+ return concat(range(startInclusive, m), rangeClosed(m, endInclusive));
} else {
return StreamSupport.longStream(
new Streams.RangeLongSpliterator(startInclusive, endInclusive, true));
}
}
+
+ /**
+ * Creates a lazy concatenated {@code LongStream} whose elements are all the
+ * elements of a first {@code LongStream} succeeded by all the elements of the
+ * second {@code LongStream}. The resulting stream is ordered if both
+ * of the input streams are ordered, and parallel if either of the input
+ * streams is parallel.
+ *
+ * @param a the first stream
+ * @param b the second stream to concatenate on to end of the first stream
+ * @return the concatenation of the two streams
+ */
+ public static LongStream concat(LongStream a, LongStream b) {
+ Objects.requireNonNull(a);
+ Objects.requireNonNull(b);
+
+ Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong(
+ a.spliterator(), b.spliterator());
+ return (a.isParallel() || b.isParallel())
+ ? StreamSupport.longParallelStream(split)
+ : StreamSupport.longStream(split);
+ }
}
--- a/jdk/src/share/classes/java/util/stream/Stream.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/stream/Stream.java Fri Jul 12 12:15:59 2013 -0700
@@ -883,4 +883,29 @@
return StreamSupport.stream(
new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s));
}
+
+ /**
+ * Creates a lazy concatenated {@code Stream} whose elements are all the
+ * elements of a first {@code Stream} succeeded by all the elements of the
+ * second {@code Stream}. The resulting stream is ordered if both
+ * of the input streams are ordered, and parallel if either of the input
+ * streams is parallel.
+ *
+ * @param <T> The type of stream elements
+ * @param a the first stream
+ * @param b the second stream to concatenate on to end of the first
+ * stream
+ * @return the concatenation of the two input streams
+ */
+ public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
+ Objects.requireNonNull(a);
+ Objects.requireNonNull(b);
+
+ @SuppressWarnings("unchecked")
+ Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
+ (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
+ return (a.isParallel() || b.isParallel())
+ ? StreamSupport.parallelStream(split)
+ : StreamSupport.stream(split);
+ }
}
--- a/jdk/src/share/classes/java/util/stream/Streams.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/src/share/classes/java/util/stream/Streams.java Fri Jul 12 12:15:59 2013 -0700
@@ -43,7 +43,7 @@
*
* @since 1.8
*/
-class Streams {
+final class Streams {
private Streams() {
throw new Error("no instances");
@@ -670,4 +670,147 @@
}
}
}
+
+ abstract static class ConcatSpliterator<T, T_SPLITR extends Spliterator<T>>
+ implements Spliterator<T> {
+ protected final T_SPLITR aSpliterator;
+ protected final T_SPLITR bSpliterator;
+ // True when no split has occurred, otherwise false
+ boolean beforeSplit;
+ // Never read after splitting
+ final boolean unsized;
+
+ public ConcatSpliterator(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
+ this.aSpliterator = aSpliterator;
+ this.bSpliterator = bSpliterator;
+ beforeSplit = true;
+ // The spliterator is unsized before splitting if a and b are
+ // sized and the sum of the estimates overflows
+ unsized = aSpliterator.hasCharacteristics(SIZED)
+ && aSpliterator.hasCharacteristics(SIZED)
+ && aSpliterator.estimateSize() + bSpliterator.estimateSize() < 0;
+ }
+
+ @Override
+ public T_SPLITR trySplit() {
+ T_SPLITR ret = beforeSplit ? aSpliterator : (T_SPLITR) bSpliterator.trySplit();
+ beforeSplit = false;
+ return ret;
+ }
+
+ @Override
+ public boolean tryAdvance(Consumer<? super T> consumer) {
+ boolean hasNext;
+ if (beforeSplit) {
+ hasNext = aSpliterator.tryAdvance(consumer);
+ if (!hasNext) {
+ beforeSplit = false;
+ hasNext = bSpliterator.tryAdvance(consumer);
+ }
+ }
+ else
+ hasNext = bSpliterator.tryAdvance(consumer);
+ return hasNext;
+ }
+
+ @Override
+ public void forEachRemaining(Consumer<? super T> consumer) {
+ if (beforeSplit)
+ aSpliterator.forEachRemaining(consumer);
+ bSpliterator.forEachRemaining(consumer);
+ }
+
+ @Override
+ public long estimateSize() {
+ if (beforeSplit) {
+ // If one or both estimates are Long.MAX_VALUE then the sum
+ // will either be Long.MAX_VALUE or overflow to a negative value
+ long size = aSpliterator.estimateSize() + bSpliterator.estimateSize();
+ return (size >= 0) ? size : Long.MAX_VALUE;
+ }
+ else {
+ return bSpliterator.estimateSize();
+ }
+ }
+
+ @Override
+ public int characteristics() {
+ if (beforeSplit) {
+ // Concatenation loses DISTINCT and SORTED characteristics
+ return aSpliterator.characteristics() & bSpliterator.characteristics()
+ & ~(Spliterator.DISTINCT | Spliterator.SORTED
+ | (unsized ? Spliterator.SIZED | Spliterator.SUBSIZED : 0));
+ }
+ else {
+ return bSpliterator.characteristics();
+ }
+ }
+
+ @Override
+ public Comparator<? super T> getComparator() {
+ if (beforeSplit)
+ throw new IllegalStateException();
+ return bSpliterator.getComparator();
+ }
+
+ static class OfRef<T> extends ConcatSpliterator<T, Spliterator<T>> {
+ OfRef(Spliterator<T> aSpliterator, Spliterator<T> bSpliterator) {
+ super(aSpliterator, bSpliterator);
+ }
+ }
+
+ private static abstract class OfPrimitive<T, T_CONS, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>>
+ extends ConcatSpliterator<T, T_SPLITR>
+ implements Spliterator.OfPrimitive<T, T_CONS, T_SPLITR> {
+ private OfPrimitive(T_SPLITR aSpliterator, T_SPLITR bSpliterator) {
+ super(aSpliterator, bSpliterator);
+ }
+
+ @Override
+ public boolean tryAdvance(T_CONS action) {
+ boolean hasNext;
+ if (beforeSplit) {
+ hasNext = aSpliterator.tryAdvance(action);
+ if (!hasNext) {
+ beforeSplit = false;
+ hasNext = bSpliterator.tryAdvance(action);
+ }
+ }
+ else
+ hasNext = bSpliterator.tryAdvance(action);
+ return hasNext;
+ }
+
+ @Override
+ public void forEachRemaining(T_CONS action) {
+ if (beforeSplit)
+ aSpliterator.forEachRemaining(action);
+ bSpliterator.forEachRemaining(action);
+ }
+ }
+
+ static class OfInt
+ extends ConcatSpliterator.OfPrimitive<Integer, IntConsumer, Spliterator.OfInt>
+ implements Spliterator.OfInt {
+ OfInt(Spliterator.OfInt aSpliterator, Spliterator.OfInt bSpliterator) {
+ super(aSpliterator, bSpliterator);
+ }
+ }
+
+ static class OfLong
+ extends ConcatSpliterator.OfPrimitive<Long, LongConsumer, Spliterator.OfLong>
+ implements Spliterator.OfLong {
+ OfLong(Spliterator.OfLong aSpliterator, Spliterator.OfLong bSpliterator) {
+ super(aSpliterator, bSpliterator);
+ }
+ }
+
+ static class OfDouble
+ extends ConcatSpliterator.OfPrimitive<Double, DoubleConsumer, Spliterator.OfDouble>
+ implements Spliterator.OfDouble {
+ OfDouble(Spliterator.OfDouble aSpliterator, Spliterator.OfDouble bSpliterator) {
+ super(aSpliterator, bSpliterator);
+ }
+ }
+ }
}
--- a/jdk/test/java/util/Collection/MOAT.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/Collection/MOAT.java Fri Jul 12 12:15:59 2013 -0700
@@ -71,6 +71,14 @@
testCollection(new LinkedList<Integer>());
testCollection(new LinkedList<Integer>().subList(0,0));
testCollection(new TreeSet<Integer>());
+ testCollection(Collections.checkedList(new ArrayList<Integer>(), Integer.class));
+ testCollection(Collections.synchronizedList(new ArrayList<Integer>()));
+ testCollection(Collections.checkedSet(new HashSet<Integer>(), Integer.class));
+ testCollection(Collections.checkedSortedSet(new TreeSet<Integer>(), Integer.class));
+ testCollection(Collections.checkedNavigableSet(new TreeSet<Integer>(), Integer.class));
+ testCollection(Collections.synchronizedSet(new HashSet<Integer>()));
+ testCollection(Collections.synchronizedSortedSet(new TreeSet<Integer>()));
+ testCollection(Collections.synchronizedNavigableSet(new TreeSet<Integer>()));
testCollection(new CopyOnWriteArrayList<Integer>());
testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
@@ -98,6 +106,12 @@
testMap(new Hashtable<Integer,Integer>());
testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
testMap(new ConcurrentSkipListMap<Integer,Integer>());
+ testMap(Collections.checkedMap(new HashMap<Integer,Integer>(), Integer.class, Integer.class));
+ testMap(Collections.checkedSortedMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class));
+ testMap(Collections.checkedNavigableMap(new TreeMap<Integer,Integer>(), Integer.class, Integer.class));
+ testMap(Collections.synchronizedMap(new HashMap<Integer,Integer>()));
+ testMap(Collections.synchronizedSortedMap(new TreeMap<Integer,Integer>()));
+ testMap(Collections.synchronizedNavigableMap(new TreeMap<Integer,Integer>()));
// Empty collections
final List<Integer> 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<Integer> emptyList = emptyList();
testCollection(emptyList);
testEmptyList(emptyList);
testEmptyList(EMPTY_LIST);
+ testEmptyList(Collections.emptyList());
testImmutableList(emptyList);
Map<Integer,Integer> 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<Integer> singletonSet = singleton(1);
--- a/jdk/test/java/util/Collections/CheckedIdentityMap.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/Collections/CheckedIdentityMap.java Fri Jul 12 12:15:59 2013 -0700
@@ -24,59 +24,42 @@
/*
* @test
* @bug 6585904
+ * @run testng CheckedIdentityMap
* @summary Checked collections with underlying maps with identity comparisons
*/
import java.util.*;
import static java.util.Collections.*;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotEquals;
+
+import org.testng.annotations.Test;
+
public class CheckedIdentityMap {
- void test(String[] args) throws Throwable {
+
+ @Test
+ public void testHashCode() {
Map<Integer, Integer> m1 = checkedMap(
new IdentityHashMap<Integer, Integer>(),
Integer.class, Integer.class);
Map<Integer, Integer> m2 = checkedMap(
new IdentityHashMap<Integer, Integer>(),
Integer.class, Integer.class);
+ // NB: these are unique instances. Compare vs. Integer.valueOf(1)
m1.put(new Integer(1), new Integer(1));
m2.put(new Integer(1), new Integer(1));
Map.Entry<Integer, Integer> e1 = m1.entrySet().iterator().next();
Map.Entry<Integer, Integer> e2 = m2.entrySet().iterator().next();
- check(! e1.equals(e2));
- check(e1.hashCode() == hashCode(e1));
- check(e2.hashCode() == hashCode(e2));
+
+ assertNotEquals(e1, e2);
+ assertEquals(e1.hashCode(), hashCode(e1));
+ assertEquals(e2.hashCode(), hashCode(e2));
}
- int hashCode(Map.Entry<?,?> e) {
+ static int hashCode(Map.Entry<?,?> e) {
return (System.identityHashCode(e.getKey()) ^
System.identityHashCode(e.getValue()));
}
-
- //--------------------- Infrastructure ---------------------------
- volatile int passed = 0, failed = 0;
- void pass() {passed++;}
- void fail() {failed++; Thread.dumpStack();}
- void fail(String msg) {System.err.println(msg); fail();}
- void unexpected(Throwable t) {failed++; t.printStackTrace();}
- void check(boolean cond) {if (cond) pass(); else fail();}
- void equal(Object x, Object y) {
- if (x == null ? y == null : x.equals(y)) pass();
- else fail(x + " not equal to " + y);}
- public static void main(String[] args) throws Throwable {
- new CheckedIdentityMap().instanceMain(args);}
- void instanceMain(String[] args) throws Throwable {
- try {test(args);} catch (Throwable t) {unexpected(t);}
- System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
- if (failed > 0) throw new AssertionError("Some tests failed");}
- abstract class F {abstract void f() throws Throwable;}
- void THROWS(Class<? extends Throwable> k, F... fs) {
- for (F f : fs)
- try {f.f(); fail("Expected " + k.getName() + " not thrown");}
- catch (Throwable t) {
- if (k.isAssignableFrom(t.getClass())) pass();
- else unexpected(t);}}
- Thread checkedThread(final Runnable r) {
- return new Thread() {public void run() {
- try {r.run();} catch (Throwable t) {unexpected(t);}}};}
}
--- a/jdk/test/java/util/Collections/CheckedMapBash.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/Collections/CheckedMapBash.java Fri Jul 12 12:15:59 2013 -0700
@@ -23,76 +23,83 @@
/*
* @test
- * @bug 4904067 5023830
+ * @bug 4904067 5023830 7129185
* @summary Unit test for Collections.checkedMap
* @author Josh Bloch
+ * @run testng CheckedMapBash
*/
import java.util.*;
+import java.util.function.Supplier;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertTrue;
public class CheckedMapBash {
- static Random rnd = new Random();
- static Object nil = new Integer(0);
-
- public static void main(String[] args) {
- int numItr = 100;
- int mapSize = 100;
-
- // Linked List test
- for (int i=0; i<numItr; i++) {
- Map m = newMap();
- Object head = nil;
+ static final Random rnd = new Random();
+ static final Object nil = new Integer(0);
+ static final int numItr = 100;
+ static final int mapSize = 100;
- for (int j=0; j<mapSize; j++) {
- Object newHead;
- do {
- newHead = new Integer(rnd.nextInt());
- } while (m.containsKey(newHead));
- m.put(newHead, head);
- head = newHead;
- }
- if (m.size() != mapSize)
- fail("Size not as expected.");
-
- {
- HashMap hm = new HashMap(m);
- if (! (hm.hashCode() == m.hashCode() &&
- hm.entrySet().hashCode() == m.entrySet().hashCode() &&
- hm.keySet().hashCode() == m.keySet().hashCode()))
- fail("Incorrect hashCode computation.");
+ @Test(dataProvider = "Bash.Supplier<Map<Integer,Integer>>")
+ public static void testCheckedMap(String description, Supplier<Map<Integer,Integer>> supplier) {
+ Map m = supplier.get();
+ Object head = nil;
- if (! (hm.equals(m) &&
- hm.entrySet().equals(m.entrySet()) &&
- hm.keySet().equals(m.keySet()) &&
- m.equals(hm) &&
- m.entrySet().equals(hm.entrySet()) &&
- m.keySet().equals(hm.keySet())))
- fail("Incorrect equals computation.");
- }
-
- Map m2 = newMap(); m2.putAll(m);
- m2.values().removeAll(m.keySet());
- if (m2.size()!= 1 || !m2.containsValue(nil))
- fail("Collection views test failed.");
+ for (int j=0; j<mapSize; j++) {
+ Object newHead;
+ do {
+ newHead = new Integer(rnd.nextInt());
+ } while (m.containsKey(newHead));
+ m.put(newHead, head);
+ head = newHead;
+ }
+ if (m.size() != mapSize)
+ fail("Size not as expected.");
- int j=0;
- while (head != nil) {
- if (!m.containsKey(head))
- fail("Linked list doesn't contain a link.");
- Object newHead = m.get(head);
- if (newHead == null)
- fail("Could not retrieve a link.");
- m.remove(head);
- head = newHead;
- j++;
- }
- if (!m.isEmpty())
- fail("Map nonempty after removing all links.");
- if (j != mapSize)
- fail("Linked list size not as expected.");
+ {
+ HashMap hm = new HashMap(m);
+ if (! (hm.hashCode() == m.hashCode() &&
+ hm.entrySet().hashCode() == m.entrySet().hashCode() &&
+ hm.keySet().hashCode() == m.keySet().hashCode()))
+ fail("Incorrect hashCode computation.");
+
+ if (! (hm.equals(m) &&
+ hm.entrySet().equals(m.entrySet()) &&
+ hm.keySet().equals(m.keySet()) &&
+ m.equals(hm) &&
+ m.entrySet().equals(hm.entrySet()) &&
+ m.keySet().equals(hm.keySet())))
+ fail("Incorrect equals computation.");
}
- Map m = newMap();
+ Map m2 = supplier.get(); m2.putAll(m);
+ m2.values().removeAll(m.keySet());
+ if (m2.size()!= 1 || !m2.containsValue(nil))
+ fail("Collection views test failed.");
+
+ int j=0;
+ while (head != nil) {
+ if (!m.containsKey(head))
+ fail("Linked list doesn't contain a link.");
+ Object newHead = m.get(head);
+ if (newHead == null)
+ fail("Could not retrieve a link.");
+ m.remove(head);
+ head = newHead;
+ j++;
+ }
+ if (!m.isEmpty())
+ fail("Map nonempty after removing all links.");
+ if (j != mapSize)
+ fail("Linked list size not as expected.");
+ }
+
+ @Test(dataProvider = "Supplier<Map<Integer,Integer>>")
+ public static void testCheckeMap2(String description, Supplier<Map<Integer,Integer>> supplier) {
+ Map m = supplier.get();
for (int i=0; i<mapSize; i++)
if (m.put(new Integer(i), new Integer(2*i)) != null)
fail("put returns a non-null value erroenously.");
@@ -101,7 +108,7 @@
fail("contains value "+i);
if (m.put(nil, nil) == null)
fail("put returns a null value erroenously.");
- Map m2 = newMap(); m2.putAll(m);
+ Map m2 = supplier.get(); m2.putAll(m);
if (!m.equals(m2))
fail("Clone not equal to original. (1)");
if (!m2.equals(m))
@@ -134,16 +141,36 @@
fail("Iterator.remove() failed");
}
- static Map newMap() {
- Map m = Collections.checkedMap(new HashMap(),
- Integer.class, Integer.class);
- if (!m.isEmpty())
- fail("New instance non empty.");
- return m;
+ @DataProvider(name = "Bash.Supplier<Map<Integer,Integer>>", parallel = true)
+ public static Iterator<Object[]> bashNavigableMapProvider() {
+ ArrayList<Object[]> iters = new ArrayList<>(makeCheckedMaps());
+ iters.ensureCapacity(numItr * iters.size());
+ for(int each=1; each < numItr; each++) {
+ iters.addAll( makeCheckedMaps());
+ }
+ return iters.iterator();
+ }
+
+ @DataProvider(name = "Supplier<Map<Integer,Integer>>", parallel = true)
+ public static Iterator<Object[]> navigableMapProvider() {
+ return makeCheckedMaps().iterator();
}
- static void fail(String s) {
- throw new RuntimeException(s);
+ public static Collection<Object[]> makeCheckedMaps() {
+ return Arrays.asList(
+ new Object[]{"Collections.checkedMap(HashMap)",
+ (Supplier) () -> {return Collections.checkedMap(new HashMap(), Integer.class, Integer.class);}},
+ new Object[]{"Collections.checkedMap(TreeSet(reverseOrder)",
+ (Supplier) () -> {return Collections.checkedMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
+ new Object[]{"Collections.checkedMap(TreeSet).descendingSet()",
+ (Supplier) () -> {return Collections.checkedMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}},
+ new Object[]{"Collections.checkedNavigableMap(TreeSet)",
+ (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class);}},
+ new Object[]{"Collections.checkedNavigableMap(TreeSet(reverseOrder)",
+ (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap(Collections.reverseOrder()), Integer.class, Integer.class);}},
+ new Object[]{"Collections.checkedNavigableMap().descendingSet()",
+ (Supplier) () -> {return Collections.checkedNavigableMap(new TreeMap().descendingMap(), Integer.class, Integer.class);}}
+ );
}
}
--- a/jdk/test/java/util/Collections/CheckedSetBash.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/Collections/CheckedSetBash.java Fri Jul 12 12:15:59 2013 -0700
@@ -23,82 +23,93 @@
/*
* @test
- * @bug 4904067
+ * @bug 4904067 7129185
* @summary Unit test for Collections.checkedSet
* @author Josh Bloch
+ * @run testng CheckedSetBash
*/
import java.util.*;
+import java.util.function.Supplier;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertTrue;
public class CheckedSetBash {
- static Random rnd = new Random();
+ static final int numItr = 100;
+ static final int setSize = 100;
+ static final Random rnd = new Random();
- public static void main(String[] args) {
- int numItr = 100;
- int setSize = 100;
+ @Test(dataProvider = "Supplier<Set<Integer>>")
+ public static void testCheckedSet(String description, Supplier<Set<Integer>> supplier) {
+
+ Set<Integer> s1 = supplier.get();
+ assertTrue(s1.isEmpty());
+
+ AddRandoms(s1, setSize);
- for (int i=0; i<numItr; i++) {
- Set s1 = newSet();
- AddRandoms(s1, setSize);
+ Set<Integer> s2 = supplier.get();
+
+ assertTrue(s2.isEmpty());
+
+ AddRandoms(s2, setSize);
- Set s2 = newSet();
- AddRandoms(s2, setSize);
-
- Set intersection = clone(s1);
- intersection.retainAll(s2);
- Set diff1 = clone(s1); diff1.removeAll(s2);
- Set diff2 = clone(s2); diff2.removeAll(s1);
- Set union = clone(s1); union.addAll(s2);
+ Set<Integer> intersection = clone(s1, supplier);
+ intersection.retainAll(s2);
+ Set<Integer> diff1 = clone(s1, supplier); diff1.removeAll(s2);
+ Set<Integer> diff2 = clone(s2, supplier); diff2.removeAll(s1);
+ Set<Integer> union = clone(s1, supplier); union.addAll(s2);
- if (diff1.removeAll(diff2))
- fail("Set algebra identity 2 failed");
- if (diff1.removeAll(intersection))
- fail("Set algebra identity 3 failed");
- if (diff2.removeAll(diff1))
- fail("Set algebra identity 4 failed");
- if (diff2.removeAll(intersection))
- fail("Set algebra identity 5 failed");
- if (intersection.removeAll(diff1))
- fail("Set algebra identity 6 failed");
- if (intersection.removeAll(diff1))
- fail("Set algebra identity 7 failed");
+ if (diff1.removeAll(diff2))
+ fail("Set algebra identity 2 failed");
+ if (diff1.removeAll(intersection))
+ fail("Set algebra identity 3 failed");
+ if (diff2.removeAll(diff1))
+ fail("Set algebra identity 4 failed");
+ if (diff2.removeAll(intersection))
+ fail("Set algebra identity 5 failed");
+ if (intersection.removeAll(diff1))
+ fail("Set algebra identity 6 failed");
+ if (intersection.removeAll(diff1))
+ fail("Set algebra identity 7 failed");
- intersection.addAll(diff1); intersection.addAll(diff2);
- if (!intersection.equals(union))
- fail("Set algebra identity 1 failed");
+ intersection.addAll(diff1); intersection.addAll(diff2);
+ if (!intersection.equals(union))
+ fail("Set algebra identity 1 failed");
- if (new HashSet(union).hashCode() != union.hashCode())
- fail("Incorrect hashCode computation.");
+ if (new HashSet(union).hashCode() != union.hashCode())
+ fail("Incorrect hashCode computation.");
- Iterator e = union.iterator();
- while (e.hasNext())
- if (!intersection.remove(e.next()))
- fail("Couldn't remove element from copy.");
- if (!intersection.isEmpty())
- fail("Copy nonempty after deleting all elements.");
+ Iterator e = union.iterator();
+ while (e.hasNext())
+ if (!intersection.remove(e.next()))
+ fail("Couldn't remove element from copy.");
+ if (!intersection.isEmpty())
+ fail("Copy nonempty after deleting all elements.");
- e = union.iterator();
- while (e.hasNext()) {
- Object o = e.next();
- if (!union.contains(o))
- fail("Set doesn't contain one of its elements.");
- e.remove();
- if (union.contains(o))
- fail("Set contains element after deletion.");
- }
- if (!union.isEmpty())
- fail("Set nonempty after deleting all elements.");
+ e = union.iterator();
+ while (e.hasNext()) {
+ Object o = e.next();
+ if (!union.contains(o))
+ fail("Set doesn't contain one of its elements.");
+ e.remove();
+ if (union.contains(o))
+ fail("Set contains element after deletion.");
+ }
+ if (!union.isEmpty())
+ fail("Set nonempty after deleting all elements.");
- s1.clear();
- if (!s1.isEmpty())
- fail("Set nonempty after clear.");
- }
+ s1.clear();
+ if (!s1.isEmpty())
+ fail("Set nonempty after clear.");
}
// Done inefficiently so as to exercise toArray
- static Set clone(Set s) {
- Set clone = newSet();
- List arrayList = Arrays.asList(s.toArray());
+ static <T> Set<T> clone(Set<T> s, Supplier<Set<T>> supplier) {
+ Set<T> clone = supplier.get();
+ List<T> arrayList = Arrays.asList((T[]) s.toArray());
clone.addAll(arrayList);
if (!s.equals(clone))
fail("Set not equal to copy.");
@@ -109,13 +120,6 @@
return clone;
}
- static Set newSet() {
- Set s = Collections.checkedSet(new HashSet(), Integer.class);
- if (!s.isEmpty())
- fail("New instance non empty.");
- return s;
- }
-
static void AddRandoms(Set s, int n) {
for (int i=0; i<n; i++) {
int r = rnd.nextInt() % n;
@@ -136,8 +140,30 @@
}
}
- static void fail(String s) {
- throw new RuntimeException(s);
+ @DataProvider(name = "Supplier<Set<Integer>>", parallel = true)
+ public static Iterator<Object[]> navigableSetsProvider() {
+ ArrayList<Object[]> iters = new ArrayList<>(makeCheckedSets());
+ iters.ensureCapacity(numItr * iters.size());
+ for(int each=1; each < numItr; each++) {
+ iters.addAll( makeCheckedSets());
+ }
+ return iters.iterator();
+ }
+ public static Collection<Object[]> makeCheckedSets() {
+ return Arrays.asList(
+ new Object[]{"Collections.checkedSet(HashSet)",
+ (Supplier) () -> {return Collections.checkedSet(new HashSet(), Integer.class);}},
+ new Object[]{"Collections.checkedSet(TreeSet(reverseOrder)",
+ (Supplier) () -> {return Collections.checkedSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
+ new Object[]{"Collections.checkedSet(TreeSet).descendingSet()",
+ (Supplier) () -> {return Collections.checkedSet(new TreeSet().descendingSet(), Integer.class);}},
+ new Object[]{"Collections.checkedNavigableSet(TreeSet)",
+ (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(), Integer.class);}},
+ new Object[]{"Collections.checkedNavigableSet(TreeSet(reverseOrder)",
+ (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet(Collections.reverseOrder()), Integer.class);}},
+ new Object[]{"Collections.checkedNavigableSet().descendingSet()",
+ (Supplier) () -> {return Collections.checkedNavigableSet(new TreeSet().descendingSet(), Integer.class);}}
+ );
}
}
--- a/jdk/test/java/util/Collections/EmptyCollectionSerialization.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/Collections/EmptyCollectionSerialization.java Fri Jul 12 12:15:59 2013 -0700
@@ -23,13 +23,20 @@
/*
* @test
- * @bug 4684279
+ * @bug 4684279 7129185
* @summary Empty utility collections should be singletons
* @author Josh Bloch
+ * @run testng EmptyCollectionSerialization
*/
import java.util.*;
+import java.util.function.Supplier;
import java.io.*;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertSame;
public class EmptyCollectionSerialization {
private static Object patheticDeepCopy(Object o) throws Exception {
@@ -45,16 +52,48 @@
return ois.readObject();
}
- private static boolean isSingleton(Object o) throws Exception {
- return patheticDeepCopy(o) == o;
+ @Test(dataProvider="SerializableSingletons")
+ public static void serializableSingletons(String description, Supplier<Object> o) {
+ try {
+ Object singleton = o.get();
+ assertSame(o.get(), singleton, description + ": broken Supplier not returning singleton");
+ Object copy = patheticDeepCopy(singleton);
+ assertSame( copy, singleton, description + ": " +
+ copy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(copy)) +
+ " is not the singleton " +
+ singleton.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(singleton)));
+ } catch(Exception all) {
+ fail(description + ": Unexpected Exception", all);
+ }
+ }
+
+ @DataProvider(name = "SerializableSingletons", parallel = true)
+ public static Iterator<Object[]> navigableMapProvider() {
+ return makeSingletons().iterator();
}
- public static void main(String[] args) throws Exception {
- if (!isSingleton(Collections.EMPTY_SET))
- throw new Exception("EMPTY_SET");
- if (!isSingleton(Collections.EMPTY_LIST))
- throw new Exception("EMPTY_LIST");
- if (!isSingleton(Collections.EMPTY_MAP))
- throw new Exception("EMPTY_MAP");
+ public static Collection<Object[]> makeSingletons() {
+ return Arrays.asList(
+ new Object[]{"Collections.EMPTY_LIST",
+ (Supplier) () -> {return Collections.EMPTY_LIST;}},
+ new Object[]{"Collections.EMPTY_MAP",
+ (Supplier) () -> {return Collections.EMPTY_MAP;}},
+ new Object[]{"Collections.EMPTY_SET",
+ (Supplier) () -> {return Collections.EMPTY_SET;}},
+ new Object[]{"Collections.singletonMap()",
+ (Supplier) () -> {return Collections.emptyList();}},
+ new Object[]{"Collections.emptyMap()",
+ (Supplier) () -> {return Collections.emptyMap();}},
+ new Object[]{"Collections.emptySet()",
+ (Supplier) () -> {return Collections.emptySet();}},
+ new Object[]{"Collections.emptySortedSet()",
+ (Supplier) () -> {return Collections.emptySortedSet();}},
+ new Object[]{"Collections.emptySortedMap()",
+ (Supplier) () -> {return Collections.emptySortedMap();}},
+ new Object[]{"Collections.emptyNavigableSet()",
+ (Supplier) () -> {return Collections.emptyNavigableSet();}},
+ new Object[]{"Collections.emptyNavigableMap()",
+ (Supplier) () -> {return Collections.emptyNavigableMap();}}
+ );
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Collections/EmptyNavigableMap.java Fri Jul 12 12:15:59 2013 -0700
@@ -0,0 +1,373 @@
+/*
+ * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4533691 7129185
+ * @summary Unit test for Collections.emptyNavigableMap
+ * @run testng EmptyNavigableMap
+ */
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.NavigableMap;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertSame;
+
+public class EmptyNavigableMap {
+
+ public static <T> void assertInstance(T actual, Class<? extends T> expected) {
+ assertInstance(expected.isInstance(actual), null);
+ }
+
+ public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
+ assertTrue(expected.isInstance(actual), ((null != message) ? message : "")
+ + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");
+ }
+
+ public static <T extends Throwable> void assertEmptyNavigableMap(Object obj) {
+ assertInstance(obj, NavigableMap.class);
+ assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0));
+ }
+
+ public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) {
+ assertInstance(obj, NavigableMap.class, message);
+ assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0),
+ ((null != message) ? message : "") + " Not empty. ");
+ }
+
+ public interface Thrower<T extends Throwable> {
+
+ public void run() throws T;
+ }
+
+ public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
+ assertThrows(thrower, throwable, null);
+ }
+
+ public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
+ Throwable result;
+ try {
+ thrower.run();
+ fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
+ return;
+ } catch (Throwable caught) {
+ result = caught;
+ }
+
+ assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
+ }
+
+ public static final boolean isDescending(SortedMap<?,?> set) {
+ if (null == set.comparator()) {
+ // natural order
+ return false;
+ }
+
+ if (Collections.reverseOrder() == set.comparator()) {
+ // reverse natural order.
+ return true;
+ }
+
+ if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
+ // it's a Collections.reverseOrder(Comparator).
+ return true;
+ }
+
+ throw new IllegalStateException("can't determine ordering for " + set);
+ }
+
+ /**
+ * Tests that the comparator is {@code null}.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) {
+ Comparator comparator = navigableMap.comparator();
+
+ assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
+ }
+
+ /**
+ * Tests that contains requires Comparable
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) {
+ assertThrows(() -> {
+ navigableMap.containsKey(new Object());
+ },
+ ClassCastException.class,
+ description + ": Compareable should be required");
+ }
+
+ /**
+ * Tests that the contains method returns {@code false}.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testContains(String description, NavigableMap<?,?> navigableMap) {
+ assertFalse(navigableMap.containsKey(new Integer(1)),
+ description + ": Should not contain any elements.");
+ assertFalse(navigableMap.containsValue(new Integer(1)),
+ description + ": Should not contain any elements.");
+ }
+
+ /**
+ * Tests that the containsAll method returns {@code false}.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testContainsAll(String description, NavigableMap<?,?> navigableMap) {
+ TreeMap treeMap = new TreeMap();
+ treeMap.put("1", 1);
+ treeMap.put("2", 2);
+ treeMap.put("3", 3);
+
+ assertFalse(navigableMap.equals(treeMap), "Should not contain any elements.");
+ }
+
+ /**
+ * Tests that the iterator is empty.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) {
+ assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty.");
+ assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty.");
+ assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty.");
+ }
+
+ /**
+ * Tests that the set is empty.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) {
+ assertTrue(navigableMap.isEmpty(), "The set is not empty.");
+ }
+
+ /**
+ * Tests the headMap() method.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testHeadMap(String description, NavigableMap navigableMap) {
+ assertThrows(
+ () -> { NavigableMap ss = navigableMap.headMap(null, false); },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); },
+ ClassCastException.class,
+ description + ": Must throw ClassCastException for non-Comparable element");
+
+ NavigableMap ss = navigableMap.headMap("1", false);
+
+ assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
+ }
+
+ /**
+ * Tests that the size is 0.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) {
+ assertTrue(0 == navigableMap.size(), "The size of the set is not 0.");
+ }
+
+ /**
+ * Tests the subMap() method.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testSubMap(String description, NavigableMap navigableMap) {
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(null, BigInteger.TEN);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(null, null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ Object obj1 = new Object();
+ Object obj2 = new Object();
+
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
+
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
+
+ assertThrows(
+ () -> {
+ SortedMap ss = navigableMap.subMap(obj1, obj2);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
+
+ // minimal range
+ navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false);
+ navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true);
+ navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false);
+ navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true);
+
+ Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
+ Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
+
+ assertThrows(
+ () -> {
+ navigableMap.subMap(last, true, first, false);
+ },
+ IllegalArgumentException.class, description
+ + ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");
+
+ navigableMap.subMap(first, true, last, false);
+ }
+
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testSubMapRanges(String description, NavigableMap navigableMap) {
+ Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
+ Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
+
+ NavigableMap subMap = navigableMap.subMap(first, true, last, true);
+
+ // same subset
+ subMap.subMap(first, true, last, true);
+
+ // slightly smaller
+ NavigableMap ns = subMap.subMap(first, false, last, false);
+ // slight exapansion
+ assertThrows(() -> {
+ ns.subMap(first, true, last, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subMap.subMap(first, false, BigInteger.ONE, false);
+ }
+
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testheadMapRanges(String description, NavigableMap navigableMap) {
+ NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);
+
+ // same subset
+ subMap.headMap(BigInteger.ONE, true);
+
+ // slightly smaller
+ NavigableMap ns = subMap.headMap(BigInteger.ONE, false);
+
+ // slight exapansion
+ assertThrows(() -> {
+ ns.headMap(BigInteger.ONE, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
+ }
+
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testTailMapRanges(String description, NavigableMap navigableMap) {
+ NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);
+
+ // same subset
+ subMap.tailMap(BigInteger.ONE, true);
+
+ // slightly smaller
+ NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
+
+ // slight exapansion
+ assertThrows(() -> {
+ ns.tailMap(BigInteger.ONE, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
+ }
+
+ /**
+ * Tests the tailMap() method.
+ */
+ @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
+ public void testTailMap(String description, NavigableMap navigableMap) {
+ assertThrows(() -> {
+ navigableMap.tailMap(null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(() -> {
+ navigableMap.tailMap(new Object());
+ }, ClassCastException.class);
+
+ NavigableMap ss = navigableMap.tailMap("1", true);
+
+ assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
+ }
+
+ @DataProvider(name = "NavigableMap<?,?>", parallel = true)
+ public static Iterator<Object[]> navigableMapsProvider() {
+ return makeNavigableMaps().iterator();
+ }
+
+ public static Collection<Object[]> makeNavigableMaps() {
+ return Arrays.asList(
+ new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())},
+ new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())},
+ new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())},
+ new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()},
+ new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()},
+ new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()}
+ );
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/Collections/EmptyNavigableSet.java Fri Jul 12 12:15:59 2013 -0700
@@ -0,0 +1,410 @@
+/*
+ * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4533691 7129185
+ * @summary Unit test for Collections.emptyNavigableSet
+ * @run testng EmptyNavigableSet
+ */
+import java.math.BigInteger;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.NavigableSet;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import org.testng.annotations.Test;
+import org.testng.annotations.DataProvider;
+
+import static org.testng.Assert.fail;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertSame;
+
+public class EmptyNavigableSet {
+
+ public static <T> void assertInstance(T actual, Class<? extends T> expected) {
+ assertInstance(expected.isInstance(actual), null);
+ }
+
+ public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
+ assertTrue(expected.isInstance(actual), ((null != message) ? message : "")
+ + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");
+ }
+
+ public static <T extends Throwable> void assertEmptyNavigableSet(Object obj) {
+ assertInstance(obj, NavigableSet.class);
+ assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0));
+ }
+
+ public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) {
+ assertInstance(obj, NavigableSet.class, message);
+ assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),
+ ((null != message) ? message : "") + " Not empty. ");
+ }
+
+ public interface Thrower<T extends Throwable> {
+
+ public void run() throws T;
+ }
+
+ public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) {
+ assertThrows(thrower, throwable, null);
+ }
+
+ public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) {
+ Throwable result;
+ try {
+ thrower.run();
+ fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
+ return;
+ } catch (Throwable caught) {
+ result = caught;
+ }
+
+ assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". ");
+ }
+
+ public static final boolean isDescending(SortedSet<?> set) {
+ if (null == set.comparator()) {
+ // natural order
+ return false;
+ }
+
+ if (Collections.reverseOrder() == set.comparator()) {
+ // reverse natural order.
+ return true;
+ }
+
+ if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
+ // it's a Collections.reverseOrder(Comparator).
+ return true;
+ }
+
+ throw new IllegalStateException("can't determine ordering for " + set);
+ }
+
+ /**
+ * Tests that the comparator is {@code null}.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {
+ Comparator comparator = navigableSet.comparator();
+
+ assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
+ }
+
+ /**
+ * Tests that contains requires Comparable
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
+ assertThrows(() -> {
+ navigableSet.contains(new Object());
+ },
+ ClassCastException.class,
+ description + ": Compareable should be required");
+ }
+
+ /**
+ * Tests that the contains method returns {@code false}.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testContains(String description, NavigableSet<?> navigableSet) {
+ assertFalse(navigableSet.contains(new Integer(1)),
+ description + ": Should not contain any elements.");
+ }
+
+ /**
+ * Tests that the containsAll method returns {@code false}.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testContainsAll(String description, NavigableSet<?> navigableSet) {
+ TreeSet treeSet = new TreeSet();
+ treeSet.add("1");
+ treeSet.add("2");
+ treeSet.add("3");
+
+ assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements.");
+ }
+
+ /**
+ * Tests that the iterator is empty.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {
+ Iterator emptyIterator = navigableSet.iterator();
+
+ assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),
+ "The iterator is not empty.");
+ }
+
+ /**
+ * Tests that the set is empty.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testIsEmpty(String description, NavigableSet<?> navigableSet) {
+ assertTrue(navigableSet.isEmpty(), "The set is not empty.");
+ }
+
+ /**
+ * Tests that the first() method throws NoSuchElementException
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testFirst(String description, NavigableSet<?> navigableSet) {
+ assertThrows(() -> {
+ navigableSet.first();
+ }, NoSuchElementException.class, description);
+ }
+
+ /**
+ * Tests the headSet() method.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testHeadSet(String description, NavigableSet navigableSet) {
+ assertThrows(
+ () -> { NavigableSet ns = navigableSet.headSet(null, false); },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
+ ClassCastException.class,
+ description + ": Must throw ClassCastException for non-Comparable element");
+
+ NavigableSet ns = navigableSet.headSet("1", false);
+
+ assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set.");
+ }
+
+ /**
+ * Tests that the last() method throws NoSuchElementException
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testLast(String description, NavigableSet<?> navigableSet) {
+ assertThrows(() -> {
+ navigableSet.last();
+ }, NoSuchElementException.class, description);
+ }
+
+ /**
+ * Tests that the size is 0.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {
+ assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");
+ }
+
+ /**
+ * Tests the subSet() method.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testSubSet(String description, NavigableSet navigableSet) {
+ assertThrows(
+ () -> {
+ SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> {
+ SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(
+ () -> {
+ SortedSet ss = navigableSet.subSet(null, null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ Object obj1 = new Object();
+ Object obj2 = new Object();
+
+ assertThrows(
+ () -> {
+ SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
+
+ assertThrows(
+ () -> {
+ SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
+
+ assertThrows(
+ () -> {
+ SortedSet ss = navigableSet.subSet(obj1, obj2);
+ },
+ ClassCastException.class, description
+ + ": Must throw ClassCastException for parameter which is not Comparable.");
+
+ // minimal range
+ navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
+ navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);
+ navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);
+ navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);
+
+ Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
+ Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
+
+ assertThrows(
+ () -> {
+ navigableSet.subSet(last, true, first, false);
+ },
+ IllegalArgumentException.class, description
+ + ": Must throw IllegalArgumentException when fromElement is not less then then toElement.");
+
+ navigableSet.subSet(first, true, last, false);
+ }
+
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testSubSetRanges(String description, NavigableSet navigableSet) {
+ Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
+ Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
+
+ NavigableSet subSet = navigableSet.subSet(first, true, last, true);
+
+ // same subset
+ subSet.subSet(first, true, last, true);
+
+ // slightly smaller
+ NavigableSet ns = subSet.subSet(first, false, last, false);
+ // slight exapansion
+ assertThrows(() -> {
+ ns.subSet(first, true, last, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subSet.subSet(first, false, BigInteger.ONE, false);
+ }
+
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testheadSetRanges(String description, NavigableSet navigableSet) {
+ NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);
+
+ // same subset
+ subSet.headSet(BigInteger.ONE, true);
+
+ // slightly smaller
+ NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
+
+ // slight exapansion
+ assertThrows(() -> {
+ ns.headSet(BigInteger.ONE, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
+ }
+
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testTailSetRanges(String description, NavigableSet navigableSet) {
+ NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
+
+ // same subset
+ subSet.tailSet(BigInteger.ONE, true);
+
+ // slightly smaller
+ NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
+
+ // slight exapansion
+ assertThrows(() -> {
+ ns.tailSet(BigInteger.ONE, true);
+ },
+ IllegalArgumentException.class,
+ description + ": Expansion should not be allowed");
+
+ // much smaller
+ subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
+ }
+
+ /**
+ * Tests the tailSet() method.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testTailSet(String description, NavigableSet navigableSet) {
+ assertThrows(() -> {
+ navigableSet.tailSet(null);
+ },
+ NullPointerException.class,
+ description + ": Must throw NullPointerException for null element");
+
+ assertThrows(() -> {
+ navigableSet.tailSet(new Object());
+ }, ClassCastException.class);
+
+ NavigableSet ss = navigableSet.tailSet("1", true);
+
+ assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
+ }
+
+ /**
+ * Tests that the array has a size of 0.
+ */
+ @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
+ public void testToArray(String description, NavigableSet<?> navigableSet) {
+ Object[] emptyNavigableSetArray = navigableSet.toArray();
+
+ assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");
+
+ emptyNavigableSetArray = new Object[20];
+
+ Object[] result = navigableSet.toArray(emptyNavigableSetArray);
+
+ assertSame(emptyNavigableSetArray, result);
+
+ assertTrue(result[0] == null);
+ }
+
+ @DataProvider(name = "NavigableSet<?>", parallel = true)
+ public static Iterator<Object[]> navigableSetsProvider() {
+ return makeNavigableSets().iterator();
+ }
+
+ public static Collection<Object[]> makeNavigableSets() {
+ return Arrays.asList(
+ new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},
+ new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},
+ new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},
+ new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},
+ new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},
+ new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}
+ );
+ }
+}
--- a/jdk/test/java/util/Collections/EmptySortedSet.java Fri Jul 12 11:48:23 2013 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,351 +0,0 @@
-/*
- * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-/*
- * @test
- * @bug 4533691
- * @summary Unit test for Collections.emptySortedSet
- */
-
-import java.lang.reflect.Method;
-import java.math.BigInteger;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-import java.util.SortedSet;
-import java.util.TreeSet;
-
-public class EmptySortedSet {
- static int status = 0;
- private static final String FAILED = " failed. ";
- private static final String PERIOD = ".";
- private final String thisClassName = this.getClass().getName();
-
- public static void main(String[] args) throws Exception {
- new EmptySortedSet();
- }
-
- public EmptySortedSet() throws Exception {
- run();
- }
-
- /**
- * Returns {@code true} if the {@link Object} passed in is an empty
- * {@link SortedSet}.
- *
- * @param obj the object to test
- * @return {@code true} if the {@link Object} is an empty {@link SortedSet}
- * otherwise {@code false}.
- */
- private boolean isEmptySortedSet(Object obj) {
- boolean isEmptySortedSet = false;
-
- // We determine if the object is an empty sorted set by testing if it's
- // an instance of SortedSet, and if so, if it's empty. Currently the
- // testing doesn't include checks of the other methods.
- if (obj instanceof SortedSet) {
- SortedSet ss = (SortedSet) obj;
-
- if ((ss.isEmpty()) && (ss.size() == 0)) {
- isEmptySortedSet = true;
- }
- }
-
- return isEmptySortedSet;
- }
-
- private void run() throws Exception {
- Method[] methods = this.getClass().getDeclaredMethods();
-
- for (int i = 0; i < methods.length; i++) {
- Method method = methods[i];
- String methodName = method.getName();
-
- if (methodName.startsWith("test")) {
- try {
- Object obj = method.invoke(this, new Object[0]);
- } catch(Exception e) {
- throw new Exception(this.getClass().getName() + "." +
- methodName + " test failed, test exception "
- + "follows\n" + e.getCause());
- }
- }
- }
- }
-
- private void throwException(String methodName, String reason)
- throws Exception
- {
- StringBuilder sb = new StringBuilder(thisClassName);
- sb.append(PERIOD);
- sb.append(methodName);
- sb.append(FAILED);
- sb.append(reason);
- throw new Exception(sb.toString());
- }
-
- /**
- *
- */
- private void test00() throws Exception {
- //throwException("test00", "This test has not been implemented yet.");
- }
-
- /**
- * Tests that the comparator is {@code null}.
- */
- private void testComparatorIsNull() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- Comparator comparator = sortedSet.comparator();
-
- if (comparator != null) {
- throwException("testComparatorIsNull", "Comparator is not null.");
- }
- }
-
- /**
- * Tests that the contains method returns {@code false}.
- */
- private void testContains() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- if (sortedSet.contains(new Object())) {
- throwException("testContains", "Should not contain any elements.");
- }
- }
-
- /**
- * Tests that the containsAll method returns {@code false}.
- */
- private void testContainsAll() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- TreeSet treeSet = new TreeSet();
- treeSet.add("1");
- treeSet.add("2");
- treeSet.add("3");
-
- if (sortedSet.containsAll(treeSet)) {
- throwException("testContainsAll",
- "Should not contain any elements.");
- }
- }
-
- /**
- * Tests that the iterator is empty.
- */
- private void testEmptyIterator() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- Iterator emptyIterator = sortedSet.iterator();
-
- if ((emptyIterator != null) && (emptyIterator.hasNext())) {
- throwException("testEmptyIterator", "The iterator is not empty.");
- }
- }
-
- /**
- * Tests that the set is empty.
- */
- private void testIsEmpty() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- if ((sortedSet != null) && (!sortedSet.isEmpty())) {
- throwException("testSizeIsZero", "The set is not empty.");
- }
- }
-
- /**
- * Tests that the first() method throws NoSuchElementException
- */
- private void testFirst() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- try {
- sortedSet.first();
- throwException("testFirst",
- "NoSuchElemenException was not thrown.");
- } catch(NoSuchElementException nsee) {
- // Do nothing
- }
- }
-
- /**
- * Tests the headSet() method.
- */
- private void testHeadSet() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- SortedSet ss;
-
- try {
- ss = sortedSet.headSet(null);
- throwException("testHeadSet",
- "Must throw NullPointerException for null element");
- } catch(NullPointerException npe) {
- // Do nothing
- }
-
- try {
- ss = sortedSet.headSet(new Object());
- throwException("testHeadSet",
- "Must throw ClassCastException for non-Comparable element");
- } catch(ClassCastException cce) {
- // Do nothing.
- }
-
- ss = sortedSet.headSet("1");
-
- if ((ss == null) || !isEmptySortedSet(ss)) {
- throwException("testHeadSet",
- "Returned value is null or not an EmptySortedSet.");
- }
- }
-
- /**
- * Tests that the last() method throws NoSuchElementException
- */
- private void testLast() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
-
- try {
- sortedSet.last();
- throwException("testLast",
- "NoSuchElemenException was not thrown.");
- } catch(NoSuchElementException nsee) {
- // Do nothing
- }
- }
-
- /**
- * Tests that the size is 0.
- */
- private void testSizeIsZero() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- int size = sortedSet.size();
-
- if (size > 0) {
- throwException("testSizeIsZero",
- "The size of the set is greater then 0.");
- }
- }
-
- /**
- * Tests the subSet() method.
- */
- private void testSubSet() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- SortedSet ss = sortedSet.headSet("1");
-
- try {
- ss = sortedSet.subSet(null, BigInteger.TEN);
- ss = sortedSet.subSet(BigInteger.ZERO, null);
- ss = sortedSet.subSet(null, null);
- throwException("testSubSet",
- "Must throw NullPointerException for null element");
- } catch(NullPointerException npe) {
- // Do nothing
- }
-
- try {
- Object obj1 = new Object();
- Object obj2 = new Object();
- ss = sortedSet.subSet(obj1, BigInteger.TEN);
- ss = sortedSet.subSet(BigInteger.ZERO, obj2);
- ss = sortedSet.subSet(obj1, obj2);
- throwException("testSubSet",
- "Must throw ClassCastException for parameter which is "
- + "not Comparable.");
- } catch(ClassCastException cce) {
- // Do nothing.
- }
-
- try {
- ss = sortedSet.subSet(BigInteger.ZERO, BigInteger.ZERO);
- ss = sortedSet.subSet(BigInteger.TEN, BigInteger.ZERO);
- throwException("testSubSet",
- "Must throw IllegalArgumentException when fromElement is "
- + "not less then then toElement.");
- } catch(IllegalArgumentException iae) {
- // Do nothing.
- }
-
- ss = sortedSet.subSet(BigInteger.ZERO, BigInteger.TEN);
-
- if (!isEmptySortedSet(ss)) {
- throw new Exception("Returned value is not empty sorted set.");
- }
- }
-
- /**
- * Tests the tailSet() method.
- */
- private void testTailSet() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- SortedSet ss;
-
- try {
- ss = sortedSet.tailSet(null);
- throwException("testTailSet",
- "Must throw NullPointerException for null element");
- } catch(NullPointerException npe) {
- // Do nothing
- }
-
- try {
- SortedSet ss2 = sortedSet.tailSet(new Object());
- throwException("testTailSet",
- "Must throw ClassCastException for non-Comparable element");
- } catch(ClassCastException cce) {
- // Do nothing.
- }
-
- ss = sortedSet.tailSet("1");
-
- if ((ss == null) || !isEmptySortedSet(ss)) {
- throwException("testTailSet",
- "Returned value is null or not an EmptySortedSet.");
- }
- }
-
- /**
- * Tests that the array has a size of 0.
- */
- private void testToArray() throws Exception {
- SortedSet sortedSet = Collections.emptySortedSet();
- Object[] emptySortedSetArray = sortedSet.toArray();
-
- if ((emptySortedSetArray == null) || (emptySortedSetArray.length > 0)) {
- throwException("testToArray",
- "Returned null array or array with length > 0.");
- }
-
- String[] strings = new String[2];
- strings[0] = "1";
- strings[1] = "2";
- emptySortedSetArray = sortedSet.toArray(strings);
-
- if ((emptySortedSetArray == null) || (emptySortedSetArray[0] != null)) {
- throwException("testToArray",
- "Returned null array or array with length > 0.");
- }
- }
-}
--- a/jdk/test/java/util/Map/LockStep.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/Map/LockStep.java Fri Jul 12 12:15:59 2013 -0700
@@ -100,7 +100,14 @@
new Hashtable(16),
new TreeMap(),
new ConcurrentHashMap(16),
- new ConcurrentSkipListMap() });
+ new ConcurrentSkipListMap(),
+ Collections.checkedMap(new HashMap(16), Integer.class, Integer.class),
+ Collections.checkedSortedMap(new TreeMap(), Integer.class, Integer.class),
+ Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class),
+ Collections.synchronizedMap(new HashMap(16)),
+ Collections.synchronizedSortedMap(new TreeMap()),
+ Collections.synchronizedNavigableMap(new TreeMap())
+ });
for (int j = 0; j < 10; j++)
put(maps, r.nextInt(100), r.nextInt(100));
--- a/jdk/test/java/util/NavigableMap/LockStep.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/NavigableMap/LockStep.java Fri Jul 12 12:15:59 2013 -0700
@@ -55,11 +55,19 @@
lockSteps(new TreeMap(),
new ConcurrentSkipListMap());
+ lockSteps(new TreeMap(),
+ Collections.checkedNavigableMap(new TreeMap(), Integer.class, Integer.class));
+ lockSteps(new TreeMap(),
+ Collections.synchronizedNavigableMap(new TreeMap()));
lockSteps(new TreeMap(reverseOrder()),
new ConcurrentSkipListMap(reverseOrder()));
lockSteps(new TreeSet(),
new ConcurrentSkipListSet());
+ lockSteps(new TreeSet(),
+ Collections.checkedNavigableSet(new TreeSet(), Integer.class));
+ lockSteps(new TreeSet(),
+ Collections.synchronizedNavigableSet(new TreeSet()));
lockSteps(new TreeSet(reverseOrder()),
new ConcurrentSkipListSet(reverseOrder()));
}
@@ -181,7 +189,15 @@
testEmptyCollection(m.values());
}
- static final Random rnd = new Random();
+ static final Random rnd;
+
+ static {
+ // sufficiently random for this test
+ long seed = System.nanoTime();
+ System.out.println(LockStep.class.getCanonicalName() + ": Trial random seed: " + seed );
+
+ rnd = new Random(seed);
+ }
static void equalNext(final Iterator<?> it, Object expected) {
if (maybe(2))
@@ -208,8 +224,15 @@
check(s.descendingSet().descendingSet().comparator() == null);
equal(s.isEmpty(), s.size() == 0);
equal2(s, s.descendingSet());
- if (maybe(4) && s instanceof Serializable)
- equal2(s, serialClone(s));
+ if (maybe(4) && s instanceof Serializable) {
+ try {
+ equal2(s, serialClone(s));
+ } catch(RuntimeException uhoh) {
+ if(!(uhoh.getCause() instanceof NotSerializableException)) {
+ throw uhoh;
+ }
+ }
+ }
Comparator cmp = comparator(s);
if (s.isEmpty()) {
THROWS(NoSuchElementException.class,
@@ -276,6 +299,15 @@
check(! it2.hasNext());
}
+ static void equalSetsLeaf(final Set s1, final Set s2) {
+ equal2(s1, s2);
+ equal( s1.size(), s2.size());
+ equal( s1.isEmpty(), s2.isEmpty());
+ equal( s1.hashCode(), s2.hashCode());
+ equal( s1.toString(), s2.toString());
+ equal( s1.containsAll(s2), s2.containsAll(s1));
+ }
+
static void equalNavigableSetsLeaf(final NavigableSet s1,
final NavigableSet s2) {
equal2(s1, s2);
@@ -448,8 +480,7 @@
static void equalNavigableMaps(NavigableMap m1,
NavigableMap m2) {
equalNavigableMapsLeaf(m1, m2);
- equalNavigableSetsLeaf((NavigableSet) m1.keySet(),
- (NavigableSet) m2.keySet());
+ equalSetsLeaf(m1.keySet(), m2.keySet());
equalNavigableSets(m1.navigableKeySet(),
m2.navigableKeySet());
equalNavigableSets(m1.descendingKeySet(),
@@ -836,5 +867,7 @@
@SuppressWarnings("unchecked")
static <T> T serialClone(T obj) {
try { return (T) readObject(serializedForm(obj)); }
- catch (Exception e) { throw new RuntimeException(e); }}
+ catch (Error|RuntimeException e) { throw e; }
+ catch (Throwable e) { throw new RuntimeException(e); }
+ }
}
--- a/jdk/test/java/util/Optional/Basic.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/Optional/Basic.java Fri Jul 12 12:15:59 2013 -0700
@@ -58,36 +58,36 @@
assertSame(Boolean.FALSE, empty.orElseGet(()-> Boolean.FALSE));
}
- @Test(expectedExceptions=NoSuchElementException.class)
- public void testEmptyGet() {
- Optional<Boolean> empty = Optional.empty();
+ @Test(expectedExceptions=NoSuchElementException.class)
+ public void testEmptyGet() {
+ Optional<Boolean> empty = Optional.empty();
- Boolean got = empty.get();
- }
+ Boolean got = empty.get();
+ }
- @Test(expectedExceptions=NullPointerException.class)
- public void testEmptyOrElseGetNull() {
- Optional<Boolean> empty = Optional.empty();
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseGetNull() {
+ Optional<Boolean> empty = Optional.empty();
- Boolean got = empty.orElseGet(null);
- }
+ Boolean got = empty.orElseGet(null);
+ }
- @Test(expectedExceptions=NullPointerException.class)
- public void testEmptyOrElseThrowNull() throws Throwable {
- Optional<Boolean> empty = Optional.empty();
+ @Test(expectedExceptions=NullPointerException.class)
+ public void testEmptyOrElseThrowNull() throws Throwable {
+ Optional<Boolean> empty = Optional.empty();
- Boolean got = empty.orElseThrow(null);
- }
+ Boolean got = empty.orElseThrow(null);
+ }
- @Test(expectedExceptions=ObscureException.class)
- public void testEmptyOrElseThrow() throws Exception {
- Optional<Boolean> empty = Optional.empty();
+ @Test(expectedExceptions=ObscureException.class)
+ public void testEmptyOrElseThrow() throws Exception {
+ Optional<Boolean> empty = Optional.empty();
- Boolean got = empty.orElseThrow(ObscureException::new);
- }
+ Boolean got = empty.orElseThrow(ObscureException::new);
+ }
- @Test(groups = "unit")
- public void testPresent() {
+ @Test(groups = "unit")
+ public void testPresent() {
Optional<Boolean> empty = Optional.empty();
Optional<String> presentEmptyString = Optional.of("");
Optional<Boolean> present = Optional.of(Boolean.TRUE);
@@ -116,6 +116,116 @@
assertSame(Boolean.TRUE, present.<RuntimeException>orElseThrow(ObscureException::new));
}
+ @Test(groups = "unit")
+ public void testOfNullable() {
+ Optional<String> instance = Optional.ofNullable(null);
+ assertFalse(instance.isPresent());
+
+ instance = Optional.ofNullable("Duke");
+ assertTrue(instance.isPresent());
+ assertEquals(instance.get(), "Duke");
+ }
+
+ @Test(groups = "unit")
+ public void testFilter() {
+ // Null mapper function
+ Optional<String> empty = Optional.empty();
+ Optional<String> duke = Optional.of("Duke");
+
+ try {
+ Optional<String> result = empty.filter(null);
+ fail("Should throw NPE on null mapping function");
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ Optional<String> result = empty.filter(String::isEmpty);
+ assertFalse(result.isPresent());
+
+ result = duke.filter(String::isEmpty);
+ assertFalse(result.isPresent());
+ result = duke.filter(s -> s.startsWith("D"));
+ assertTrue(result.isPresent());
+ assertEquals(result.get(), "Duke");
+
+ Optional<String> emptyString = Optional.of("");
+ result = emptyString.filter(String::isEmpty);
+ assertTrue(result.isPresent());
+ assertEquals(result.get(), "");
+ }
+
+ @Test(groups = "unit")
+ public void testMap() {
+ Optional<String> empty = Optional.empty();
+ Optional<String> duke = Optional.of("Duke");
+
+ // Null mapper function
+ try {
+ Optional<Boolean> b = empty.map(null);
+ fail("Should throw NPE on null mapping function");
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ // Map an empty value
+ Optional<Boolean> b = empty.map(String::isEmpty);
+ assertFalse(b.isPresent());
+
+ // Map into null
+ b = empty.map(n -> null);
+ assertFalse(b.isPresent());
+ b = duke.map(s -> null);
+ assertFalse(b.isPresent());
+
+ // Map to value
+ Optional<Integer> l = duke.map(String::length);
+ assertEquals(l.get().intValue(), 4);
+ }
+
+ @Test(groups = "unit")
+ public void testFlatMap() {
+ Optional<String> empty = Optional.empty();
+ Optional<String> duke = Optional.of("Duke");
+
+ // Null mapper function
+ try {
+ Optional<Boolean> b = empty.flatMap(null);
+ fail("Should throw NPE on null mapping function");
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ // Map into null
+ try {
+ Optional<Boolean> b = duke.flatMap(s -> null);
+ fail("Should throw NPE when mapper return null");
+ } catch (NullPointerException npe) {
+ // expected
+ }
+
+ // Empty won't invoke mapper function
+ try {
+ Optional<Boolean> b = empty.flatMap(s -> null);
+ assertFalse(b.isPresent());
+ } catch (NullPointerException npe) {
+ fail("Mapper function should not be invoked");
+ }
+
+ // Map an empty value
+ Optional<Integer> l = empty.flatMap(s -> Optional.of(s.length()));
+ assertFalse(l.isPresent());
+
+ // Map to value
+ Optional<Integer> fixture = Optional.of(Integer.MAX_VALUE);
+ l = duke.flatMap(s -> Optional.of(s.length()));
+ assertTrue(l.isPresent());
+ assertEquals(l.get().intValue(), 4);
+
+ // Verify same instance
+ l = duke.flatMap(s -> fixture);
+ assertSame(l, fixture);
+ }
+
private static class ObscureException extends RuntimeException {
}
--- a/jdk/test/java/util/stream/bootlib/java/util/stream/LambdaTestHelpers.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/stream/bootlib/java/util/stream/LambdaTestHelpers.java Fri Jul 12 12:15:59 2013 -0700
@@ -360,35 +360,26 @@
private static<T> Map<T, Integer> toBoxedMultiset(Iterator<T> it) {
Map<Object, Integer> result = new HashMap<>();
- it.forEachRemaining(new OmnivorousConsumer<T>() {
- @Override
- public void accept(T t) {
- add(t);
- }
-
- @Override
- public void accept(int value) {
- add(value);
- }
-
- @Override
- public void accept(long value) {
- add(value);
- }
-
- @Override
- public void accept(double value) {
- add(value);
- }
-
- void add(Object o) {
+ it.forEachRemaining(toBoxingConsumer(o -> {
if (result.containsKey(o))
result.put(o, result.get(o) + 1);
else
result.put(o, 1);
- }
+ }));
+
+ return (Map<T, Integer>) result;
+ }
- });
+ @SuppressWarnings("unchecked")
+ public static<T> Map<T, Integer> toBoxedMultiset(Spliterator<T> it) {
+ Map<Object, Integer> result = new HashMap<>();
+
+ it.forEachRemaining(toBoxingConsumer(o -> {
+ if (result.containsKey(o))
+ result.put(o, result.get(o) + 1);
+ else
+ result.put(o, 1);
+ }));
return (Map<T, Integer>) result;
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ConcatOpTest.java Fri Jul 12 12:15:59 2013 -0700
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.tests.java.util.stream;
+
+import java.util.stream.OpTestCase;
+import java.util.stream.StreamTestDataProvider;
+
+import org.testng.annotations.Test;
+
+import java.util.stream.Stream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.DoubleStream;
+import java.util.stream.TestData;
+
+import static java.util.stream.LambdaTestHelpers.*;
+
+public class ConcatOpTest extends OpTestCase {
+
+ // Sanity to make sure all type of stream source works
+ @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+ public void testOpsSequential(String name, TestData.OfRef<Integer> data) {
+ exerciseOpsInt(data,
+ s -> Stream.concat(s, data.stream()),
+ s -> IntStream.concat(s, data.stream().mapToInt(Integer::intValue)),
+ s -> LongStream.concat(s, data.stream().mapToLong(Integer::longValue)),
+ s -> DoubleStream.concat(s, data.stream().mapToDouble(Integer::doubleValue)));
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ConcatTest.java Fri Jul 12 12:15:59 2013 -0700
@@ -0,0 +1,229 @@
+/*
+ * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.tests.java.util.stream;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Factory;
+import org.testng.annotations.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Spliterator;
+import java.util.TreeSet;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.Stream;
+
+import static java.util.stream.LambdaTestHelpers.*;
+import static org.testng.Assert.*;
+
+@Test
+public class ConcatTest {
+ private static Object[][] cases;
+
+ static {
+ List<Integer> part1 = Arrays.asList(5, 3, 4, 1, 2, 6, 2, 4);
+ List<Integer> part2 = Arrays.asList(8, 8, 6, 6, 9, 7, 10, 9);
+ List<Integer> p1p2 = Arrays.asList(5, 3, 4, 1, 2, 6, 2, 4, 8, 8, 6, 6, 9, 7, 10, 9);
+ List<Integer> p2p1 = Arrays.asList(8, 8, 6, 6, 9, 7, 10, 9, 5, 3, 4, 1, 2, 6, 2, 4);
+ List<Integer> empty = new LinkedList<>(); // To be ordered
+ assertTrue(empty.isEmpty());
+ LinkedHashSet<Integer> distinctP1 = new LinkedHashSet<>(part1);
+ LinkedHashSet<Integer> distinctP2 = new LinkedHashSet<>(part2);
+ TreeSet<Integer> sortedP1 = new TreeSet<>(part1);
+ TreeSet<Integer> sortedP2 = new TreeSet<>(part2);
+
+ cases = new Object[][] {
+ { "regular", part1, part2, p1p2 },
+ { "reverse regular", part2, part1, p2p1 },
+ { "front distinct", distinctP1, part2, Arrays.asList(5, 3, 4, 1, 2, 6, 8, 8, 6, 6, 9, 7, 10, 9) },
+ { "back distinct", part1, distinctP2, Arrays.asList(5, 3, 4, 1, 2, 6, 2, 4, 8, 6, 9, 7, 10) },
+ { "both distinct", distinctP1, distinctP2, Arrays.asList(5, 3, 4, 1, 2, 6, 8, 6, 9, 7, 10) },
+ { "front sorted", sortedP1, part2, Arrays.asList(1, 2, 3, 4, 5, 6, 8, 8, 6, 6, 9, 7, 10, 9) },
+ { "back sorted", part1, sortedP2, Arrays.asList(5, 3, 4, 1, 2, 6, 2, 4, 6, 7, 8, 9, 10) },
+ { "both sorted", sortedP1, sortedP2, Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10) },
+ { "reverse both sorted", sortedP2, sortedP1, Arrays.asList(6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6) },
+ { "empty something", empty, part1, part1 },
+ { "something empty", part1, empty, part1 },
+ { "empty empty", empty, empty, empty }
+ };
+ }
+
+ @DataProvider(name = "cases")
+ private static Object[][] getCases() {
+ return cases;
+ }
+
+ @Factory(dataProvider = "cases")
+ public static Object[] createTests(String scenario, Collection<Integer> c1, Collection<Integer> c2, Collection<Integer> expected) {
+ return new Object[] {
+ new ConcatTest(scenario, c1, c2, expected)
+ };
+ }
+
+ protected final String scenario;
+ protected final Collection<Integer> c1;
+ protected final Collection<Integer> c2;
+ protected final Collection<Integer> expected;
+
+ public ConcatTest(String scenario, Collection<Integer> c1, Collection<Integer> c2, Collection<Integer> expected) {
+ this.scenario = scenario;
+ this.c1 = c1;
+ this.c2 = c2;
+ this.expected = expected;
+
+ // verify prerequisite
+ Stream<Integer> s1s = c1.stream();
+ Stream<Integer> s2s = c2.stream();
+ Stream<Integer> s1p = c1.parallelStream();
+ Stream<Integer> s2p = c2.parallelStream();
+ assertTrue(s1p.isParallel());
+ assertTrue(s2p.isParallel());
+ assertFalse(s1s.isParallel());
+ assertFalse(s2s.isParallel());
+
+ assertTrue(s1s.spliterator().hasCharacteristics(Spliterator.ORDERED));
+ assertTrue(s1p.spliterator().hasCharacteristics(Spliterator.ORDERED));
+ assertTrue(s2s.spliterator().hasCharacteristics(Spliterator.ORDERED));
+ assertTrue(s2p.spliterator().hasCharacteristics(Spliterator.ORDERED));
+ }
+
+ private <T> void assertConcatContent(Spliterator<T> sp, boolean ordered, Spliterator<T> expected) {
+ // concat stream cannot guarantee uniqueness
+ assertFalse(sp.hasCharacteristics(Spliterator.DISTINCT), scenario);
+ // concat stream cannot guarantee sorted
+ assertFalse(sp.hasCharacteristics(Spliterator.SORTED), scenario);
+ // concat stream is ordered if both are ordered
+ assertEquals(sp.hasCharacteristics(Spliterator.ORDERED), ordered, scenario);
+
+ // Verify elements
+ if (ordered) {
+ assertEquals(toBoxedList(sp),
+ toBoxedList(expected),
+ scenario);
+ } else {
+ assertEquals(toBoxedMultiset(sp),
+ toBoxedMultiset(expected),
+ scenario);
+ }
+ }
+
+ private void assertRefConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
+ Stream<Integer> result = Stream.concat(s1, s2);
+ assertEquals(result.isParallel(), parallel);
+ assertConcatContent(result.spliterator(), ordered, expected.spliterator());
+ }
+
+ private void assertIntConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
+ IntStream result = IntStream.concat(s1.mapToInt(Integer::intValue),
+ s2.mapToInt(Integer::intValue));
+ assertEquals(result.isParallel(), parallel);
+ assertConcatContent(result.spliterator(), ordered,
+ expected.stream().mapToInt(Integer::intValue).spliterator());
+ }
+
+ private void assertLongConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
+ LongStream result = LongStream.concat(s1.mapToLong(Integer::longValue),
+ s2.mapToLong(Integer::longValue));
+ assertEquals(result.isParallel(), parallel);
+ assertConcatContent(result.spliterator(), ordered,
+ expected.stream().mapToLong(Integer::longValue).spliterator());
+ }
+
+ private void assertDoubleConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
+ DoubleStream result = DoubleStream.concat(s1.mapToDouble(Integer::doubleValue),
+ s2.mapToDouble(Integer::doubleValue));
+ assertEquals(result.isParallel(), parallel);
+ assertConcatContent(result.spliterator(), ordered,
+ expected.stream().mapToDouble(Integer::doubleValue).spliterator());
+ }
+
+ public void testRefConcat() {
+ // sequential + sequential -> sequential
+ assertRefConcat(c1.stream(), c2.stream(), false, true);
+ // parallel + parallel -> parallel
+ assertRefConcat(c1.parallelStream(), c2.parallelStream(), true, true);
+ // sequential + parallel -> parallel
+ assertRefConcat(c1.stream(), c2.parallelStream(), true, true);
+ // parallel + sequential -> parallel
+ assertRefConcat(c1.parallelStream(), c2.stream(), true, true);
+
+ // not ordered
+ assertRefConcat(c1.stream().unordered(), c2.stream(), false, false);
+ assertRefConcat(c1.stream(), c2.stream().unordered(), false, false);
+ assertRefConcat(c1.parallelStream().unordered(), c2.stream().unordered(), true, false);
+ }
+
+ public void testIntConcat() {
+ // sequential + sequential -> sequential
+ assertIntConcat(c1.stream(), c2.stream(), false, true);
+ // parallel + parallel -> parallel
+ assertIntConcat(c1.parallelStream(), c2.parallelStream(), true, true);
+ // sequential + parallel -> parallel
+ assertIntConcat(c1.stream(), c2.parallelStream(), true, true);
+ // parallel + sequential -> parallel
+ assertIntConcat(c1.parallelStream(), c2.stream(), true, true);
+
+ // not ordered
+ assertIntConcat(c1.stream().unordered(), c2.stream(), false, false);
+ assertIntConcat(c1.stream(), c2.stream().unordered(), false, false);
+ assertIntConcat(c1.parallelStream().unordered(), c2.stream().unordered(), true, false);
+ }
+
+ public void testLongConcat() {
+ // sequential + sequential -> sequential
+ assertLongConcat(c1.stream(), c2.stream(), false, true);
+ // parallel + parallel -> parallel
+ assertLongConcat(c1.parallelStream(), c2.parallelStream(), true, true);
+ // sequential + parallel -> parallel
+ assertLongConcat(c1.stream(), c2.parallelStream(), true, true);
+ // parallel + sequential -> parallel
+ assertLongConcat(c1.parallelStream(), c2.stream(), true, true);
+
+ // not ordered
+ assertLongConcat(c1.stream().unordered(), c2.stream(), false, false);
+ assertLongConcat(c1.stream(), c2.stream().unordered(), false, false);
+ assertLongConcat(c1.parallelStream().unordered(), c2.stream().unordered(), true, false);
+ }
+
+ public void testDoubleConcat() {
+ // sequential + sequential -> sequential
+ assertDoubleConcat(c1.stream(), c2.stream(), false, true);
+ // parallel + parallel -> parallel
+ assertDoubleConcat(c1.parallelStream(), c2.parallelStream(), true, true);
+ // sequential + parallel -> parallel
+ assertDoubleConcat(c1.stream(), c2.parallelStream(), true, true);
+ // parallel + sequential -> parallel
+ assertDoubleConcat(c1.parallelStream(), c2.stream(), true, true);
+
+ // not ordered
+ assertDoubleConcat(c1.stream().unordered(), c2.stream(), false, false);
+ assertDoubleConcat(c1.stream(), c2.stream().unordered(), false, false);
+ assertDoubleConcat(c1.parallelStream().unordered(), c2.stream().unordered(), true, false);
+ }
+}
--- a/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/RangeTest.java Fri Jul 12 11:48:23 2013 -0700
+++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/RangeTest.java Fri Jul 12 12:15:59 2013 -0700
@@ -226,116 +226,114 @@
assertEquals(first, LongStream.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().getAsLong());
}
- // Enable when Stream.concat is present and range implementations are
- // updated to use that
-// private static void assertSizedAndSubSized(Spliterator<?> s) {
-// assertTrue(s.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
-// }
-//
-// private static void assertNotSizedAndSubSized(Spliterator<?> s) {
-// assertFalse(s.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
-// }
-//
-// public void testLongLongRange() {
-// // Test [Long.MIN_VALUE, Long.MAX_VALUE)
-// // This will concatenate streams of three ranges
-// // [Long.MIN_VALUE, x) [x, 0) [0, Long.MAX_VALUE)
-// // where x = Long.divideUnsigned(0 - Long.MIN_VALUE, 2) + 1
-// {
-// Spliterator.OfLong s = LongStream.range(Long.MIN_VALUE, Long.MAX_VALUE).spliterator();
-//
-// assertEquals(s.estimateSize(), Long.MAX_VALUE);
-// assertNotSizedAndSubSized(s);
-//
-// Spliterator.OfLong s1 = s.trySplit();
-// assertNotSizedAndSubSized(s1);
-// assertSizedAndSubSized(s);
-//
-// Spliterator.OfLong s2 = s1.trySplit();
-// assertSizedAndSubSized(s1);
-// assertSizedAndSubSized(s2);
-//
-// assertTrue(s.estimateSize() == Long.MAX_VALUE);
-// assertTrue(s1.estimateSize() < Long.MAX_VALUE);
-// assertTrue(s2.estimateSize() < Long.MAX_VALUE);
-//
-// assertEquals(s.estimateSize() + s1.estimateSize() + s2.estimateSize(),
-// Long.MAX_VALUE - Long.MIN_VALUE);
-// }
-//
-// long[][] ranges = { {Long.MIN_VALUE, 0}, {-1, Long.MAX_VALUE} };
-// for (int i = 0; i < ranges.length; i++) {
-// long start = ranges[i][0];
-// long end = ranges[i][1];
-//
-// Spliterator.OfLong s = LongStream.range(start, end).spliterator();
-//
-// assertEquals(s.estimateSize(), Long.MAX_VALUE);
-// assertNotSizedAndSubSized(s);
-//
-// Spliterator.OfLong s1 = s.trySplit();
-// assertSizedAndSubSized(s1);
-// assertSizedAndSubSized(s);
-//
-// assertTrue(s.estimateSize() < Long.MAX_VALUE);
-// assertTrue(s1.estimateSize() < Long.MAX_VALUE);
-//
-// assertEquals(s.estimateSize() + s1.estimateSize(), end - start);
-// }
-// }
-//
-// public void testLongLongRangeClosed() {
-// // Test [Long.MIN_VALUE, Long.MAX_VALUE]
-// // This will concatenate streams of four ranges
-// // [Long.MIN_VALUE, x) [x, 0) [0, y) [y, Long.MAX_VALUE]
-// // where x = Long.divideUnsigned(0 - Long.MIN_VALUE, 2) + 1
-// // y = Long.divideUnsigned(Long.MAX_VALUE, 2) + 1
-//
-// {
-// Spliterator.OfLong s = LongStream.rangeClosed(Long.MIN_VALUE, Long.MAX_VALUE).spliterator();
-//
-// assertEquals(s.estimateSize(), Long.MAX_VALUE);
-// assertNotSizedAndSubSized(s);
-//
-// Spliterator.OfLong s1 = s.trySplit();
-// assertNotSizedAndSubSized(s1);
-// assertNotSizedAndSubSized(s);
-//
-// Spliterator.OfLong s2 = s1.trySplit();
-// assertSizedAndSubSized(s1);
-// assertSizedAndSubSized(s2);
-//
-// Spliterator.OfLong s3 = s.trySplit();
-// assertSizedAndSubSized(s3);
-// assertSizedAndSubSized(s);
-//
-// assertTrue(s.estimateSize() < Long.MAX_VALUE);
-// assertTrue(s3.estimateSize() < Long.MAX_VALUE);
-// assertTrue(s1.estimateSize() < Long.MAX_VALUE);
-// assertTrue(s2.estimateSize() < Long.MAX_VALUE);
-//
-// assertEquals(s.estimateSize() + s3.estimateSize() + s1.estimateSize() + s2.estimateSize(),
-// Long.MAX_VALUE - Long.MIN_VALUE + 1);
-// }
-//
-// long[][] ranges = { {Long.MIN_VALUE, 0}, {-1, Long.MAX_VALUE} };
-// for (int i = 0; i < ranges.length; i++) {
-// long start = ranges[i][0];
-// long end = ranges[i][1];
-//
-// Spliterator.OfLong s = LongStream.rangeClosed(start, end).spliterator();
-//
-// assertEquals(s.estimateSize(), Long.MAX_VALUE);
-// assertNotSizedAndSubSized(s);
-//
-// Spliterator.OfLong s1 = s.trySplit();
-// assertSizedAndSubSized(s1);
-// assertSizedAndSubSized(s);
-//
-// assertTrue(s.estimateSize() < Long.MAX_VALUE);
-// assertTrue(s1.estimateSize() < Long.MAX_VALUE);
-//
-// assertEquals(s.estimateSize() + s1.estimateSize(), end - start + 1);
-// }
-// }
+ private static void assertSizedAndSubSized(Spliterator<?> s) {
+ assertTrue(s.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
+ }
+
+ private static void assertNotSizedAndSubSized(Spliterator<?> s) {
+ assertFalse(s.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
+ }
+
+ public void testLongLongRange() {
+ // Test [Long.MIN_VALUE, Long.MAX_VALUE)
+ // This will concatenate streams of three ranges
+ // [Long.MIN_VALUE, x) [x, 0) [0, Long.MAX_VALUE)
+ // where x = Long.divideUnsigned(0 - Long.MIN_VALUE, 2) + 1
+ {
+ Spliterator.OfLong s = LongStream.range(Long.MIN_VALUE, Long.MAX_VALUE).spliterator();
+
+ assertEquals(s.estimateSize(), Long.MAX_VALUE);
+ assertNotSizedAndSubSized(s);
+
+ Spliterator.OfLong s1 = s.trySplit();
+ assertNotSizedAndSubSized(s1);
+ assertSizedAndSubSized(s);
+
+ Spliterator.OfLong s2 = s1.trySplit();
+ assertSizedAndSubSized(s1);
+ assertSizedAndSubSized(s2);
+
+ assertTrue(s.estimateSize() == Long.MAX_VALUE);
+ assertTrue(s1.estimateSize() < Long.MAX_VALUE);
+ assertTrue(s2.estimateSize() < Long.MAX_VALUE);
+
+ assertEquals(s.estimateSize() + s1.estimateSize() + s2.estimateSize(),
+ Long.MAX_VALUE - Long.MIN_VALUE);
+ }
+
+ long[][] ranges = { {Long.MIN_VALUE, 0}, {-1, Long.MAX_VALUE} };
+ for (int i = 0; i < ranges.length; i++) {
+ long start = ranges[i][0];
+ long end = ranges[i][1];
+
+ Spliterator.OfLong s = LongStream.range(start, end).spliterator();
+
+ assertEquals(s.estimateSize(), Long.MAX_VALUE);
+ assertNotSizedAndSubSized(s);
+
+ Spliterator.OfLong s1 = s.trySplit();
+ assertSizedAndSubSized(s1);
+ assertSizedAndSubSized(s);
+
+ assertTrue(s.estimateSize() < Long.MAX_VALUE);
+ assertTrue(s1.estimateSize() < Long.MAX_VALUE);
+
+ assertEquals(s.estimateSize() + s1.estimateSize(), end - start);
+ }
+ }
+
+ public void testLongLongRangeClosed() {
+ // Test [Long.MIN_VALUE, Long.MAX_VALUE]
+ // This will concatenate streams of four ranges
+ // [Long.MIN_VALUE, x) [x, 0) [0, y) [y, Long.MAX_VALUE]
+ // where x = Long.divideUnsigned(0 - Long.MIN_VALUE, 2) + 1
+ // y = Long.divideUnsigned(Long.MAX_VALUE, 2) + 1
+
+ {
+ Spliterator.OfLong s = LongStream.rangeClosed(Long.MIN_VALUE, Long.MAX_VALUE).spliterator();
+
+ assertEquals(s.estimateSize(), Long.MAX_VALUE);
+ assertNotSizedAndSubSized(s);
+
+ Spliterator.OfLong s1 = s.trySplit();
+ assertNotSizedAndSubSized(s1);
+ assertNotSizedAndSubSized(s);
+
+ Spliterator.OfLong s2 = s1.trySplit();
+ assertSizedAndSubSized(s1);
+ assertSizedAndSubSized(s2);
+
+ Spliterator.OfLong s3 = s.trySplit();
+ assertSizedAndSubSized(s3);
+ assertSizedAndSubSized(s);
+
+ assertTrue(s.estimateSize() < Long.MAX_VALUE);
+ assertTrue(s3.estimateSize() < Long.MAX_VALUE);
+ assertTrue(s1.estimateSize() < Long.MAX_VALUE);
+ assertTrue(s2.estimateSize() < Long.MAX_VALUE);
+
+ assertEquals(s.estimateSize() + s3.estimateSize() + s1.estimateSize() + s2.estimateSize(),
+ Long.MAX_VALUE - Long.MIN_VALUE + 1);
+ }
+
+ long[][] ranges = { {Long.MIN_VALUE, 0}, {-1, Long.MAX_VALUE} };
+ for (int i = 0; i < ranges.length; i++) {
+ long start = ranges[i][0];
+ long end = ranges[i][1];
+
+ Spliterator.OfLong s = LongStream.rangeClosed(start, end).spliterator();
+
+ assertEquals(s.estimateSize(), Long.MAX_VALUE);
+ assertNotSizedAndSubSized(s);
+
+ Spliterator.OfLong s1 = s.trySplit();
+ assertSizedAndSubSized(s1);
+ assertSizedAndSubSized(s);
+
+ assertTrue(s.estimateSize() < Long.MAX_VALUE);
+ assertTrue(s1.estimateSize() < Long.MAX_VALUE);
+
+ assertEquals(s.estimateSize() + s1.estimateSize(), end - start + 1);
+ }
+ }
}