OpenJDK / lambda / lambda / jdk
changeset 7601:37c4fdad51ce
Expose spliterator() as method on Collection.
Contributed-by: Paul Sandoz <Paul.Sandoz@oracle.com>, Brian Goetz <brian.goetz@Oracle.COM>.
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/jdi/EventSetImpl.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/com/sun/tools/jdi/EventSetImpl.java Mon Mar 11 14:13:55 2013 +0100 @@ -873,9 +873,4 @@ public void clear() { throw new UnsupportedOperationException(); } - - @Override - public Stream<Event> stream() { - return super.stream(); - } }
--- a/src/share/classes/java/lang/Iterable.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/lang/Iterable.java Mon Mar 11 14:13:55 2013 +0100 @@ -49,26 +49,28 @@ Iterator<T> iterator(); /** - * Performs the given action, in the order elements occur when iterating, - * for all elements. Exceptions occurring as a result of performing this - * action are relayed to the caller. + * Performs the given action on the contents of the {@code Iterable}, in the + * order elements are returned by an iterator, until all elements have been + * processed or the action throws an {@code Exception}. Exceptions thrown + * by the action are relayed to the caller. * - * @apiNote - * <p>This implementation may be overridden by implementing Collections to - * provide optimal performance or to include this method within their - * synchronization protocol. + * <p>The default implementation should be overridden by implementations if + * they can provide a more performant implementation than an iterator-based + * one, or to include this method within their synchronization protocol. * - * @implNote - * <p>This implementation calls {@link #iterator()} then repeatedly invokes - * {@link Iterator#next} and performs the action on the next element until - * {@link Iterator#hasNext()} returns {@code false}. + * @implSpec + * <p>The default implementation behaves as if: + * <pre> + * for (T t : this) + * action.accept(t); + * </pre> * * <p>This implementation does not have knowledge of the synchronization - * protocol used by the implementing Collection. It is the caller's + * protocol used by the implementing Collection. It is the caller's * responsibility to ensure that usage follows the correct synchronization - * protocol for the implementing Collection. + * protocol for the implementing @{code Iterable}. * - * @param action The action + * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */
--- a/src/share/classes/java/util/ArrayDeque.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/ArrayDeque.java Mon Mar 11 14:13:55 2013 +0100 @@ -875,18 +875,10 @@ elements[i] = s.readObject(); } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new DeqSpliterator<E>(this, -1, -1); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - static final class DeqSpliterator<E> implements Spliterator<E> { private final ArrayDeque<E> deq; private int fence; // -1 until first use
--- a/src/share/classes/java/util/ArrayList.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/ArrayList.java Mon Mar 11 14:13:55 2013 +0100 @@ -1156,19 +1156,11 @@ throw new ConcurrentModificationException(); } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { checkForComodification(); return new ArrayListSpliterator<E>(ArrayList.this, offset, offset + this.size, this.modCount); } - - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } } @Override @@ -1249,18 +1241,10 @@ modCount++; } - Spliterator<E> spliterator() { // to be made public later + public Spliterator<E> spliterator() { return new ArrayListSpliterator<>(this, 0, -1, 0); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - /** Index-based split-by-two, lazily initialized Spliterator */ static final class ArrayListSpliterator<E> implements Spliterator<E> {
--- a/src/share/classes/java/util/Arrays.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/Arrays.java Mon Mar 11 14:13:55 2013 +0100 @@ -3630,13 +3630,8 @@ } @Override - public Stream<E> stream() { - return Arrays.stream(a); - } - - @Override - public Stream<E> parallelStream() { - return Arrays.parallelStream(a); + public Spliterator<E> spliterator() { + return Spliterators.spliterator(a, Spliterator.IMMUTABLE | Spliterator.ORDERED); } }
--- a/src/share/classes/java/util/Collection.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/Collection.java Mon Mar 11 14:13:55 2013 +0100 @@ -344,6 +344,7 @@ * @see #add(Object) */ boolean addAll(Collection<? extends E> c); + /** * Removes all of this collection's elements that are also contained in the * specified collection (optional operation). After this call returns, @@ -479,21 +480,73 @@ } /** - * Create a {@code Stream} over the elements of this collection. + * Creates a {@link Spliterator}, over the elements in this collection, that + * reports the spliterator characteristic {@code SIZED}. Overriding + * implementations should document if the {@code Spliterator} reports any + * additional characteristic values. * - * @return a {@code Stream} of elements in this collection + * <p>The default implementation should be overridden by implementing + * Collections that can return a more efficient spliterator. In order to + * preserve expected laziness behavior for the {@link #stream()} and + * {@link #parallelStream()}} methods, spliterators should either have the + * characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be + * <em>late-binding</em>. If none of these is practical, the overriding + * class should describe the spliterator's documented policy of binding and + * structural interference, and should override the {@link #stream()} and + * {@link #parallelStream()} methods to create streams using a + * {@code Supplier} of the spliterator, as in: + * <pre> + * Stream<E> s = Streams.stream(() -> spliterator(), spliteratorCharacteristics) + * </pre> + * These requirements ensure that streams produced by the {@link #stream()} + * and {@link #parallelStream()} methods will reflect the concepts of the + * collection as of the beginning of the terminal stream operation. + * + * @implSpec + * This implementation creates a <em>late-binding</em> spliterator from + * the collection's {@code Iterator}. + * + * @return a {@code Spliterator} over the elements in this collection */ - default Stream<E> stream() { - return Streams.stream(Spliterators.spliterator(this, 0)); + default Spliterator<E> spliterator() { + return Spliterators.spliterator(this, 0); } /** - * Create a parallel {@code Stream} over the elements of this collection. + * Creates a sequential {@code Stream} over the elements in this collection. * - * @return a {@code Stream} of elements in this collection + * <p>This method should be overridden when the {@link #spliterator()} + * method cannot return a spliterator that is {@code IMMUTABLE}, + * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} + * for details.) + * + * @implSpec + * This implementation creates a sequential {@code Stream} from the + * collection's {@code Spliterator}. + * + * @return a sequential {@code Stream} over the elements in this collection + */ + default Stream<E> stream() { + return Streams.stream(spliterator()); + } + + /** + * Creates a parallel {@code Stream} over the elements in this collection. It is + * allowable for this method to return a sequential stream. + * + * <p>This method should be overridden when the {@link #spliterator()} + * method cannot return a spliterator that is {@code IMMUTABLE}, + * {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()} + * for details.) + * + * @implSpec + * This implementation creates a parallel {@code Stream} from the + * collection's {@code Spliterator}. + * + * @return a parallel {@code Stream} over the elements in this collection */ default Stream<E> parallelStream() { - return Streams.parallelStream(Spliterators.spliterator(this, 0)); + return Streams.parallelStream(spliterator()); } }
--- a/src/share/classes/java/util/HashMap.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/HashMap.java Mon Mar 11 14:13:55 2013 +0100 @@ -925,21 +925,13 @@ HashMap.this.clear(); } - Spliterator<K> spliterator() { + public Spliterator<K> spliterator() { if (HashMap.this.getClass() == HashMap.class) return new KeySpliterator<K,V>(HashMap.this, 0, -1, 0, 0); else return Spliterators.spliterator (this, Spliterator.SIZED | Spliterator.DISTINCT); } - - public Stream<K> stream() { - return Streams.stream(spliterator()); - } - - public Stream<K> parallelStream() { - return Streams.parallelStream(spliterator()); - } } /** @@ -974,21 +966,13 @@ HashMap.this.clear(); } - Spliterator<V> spliterator() { + public Spliterator<V> spliterator() { if (HashMap.this.getClass() == HashMap.class) return new ValueSpliterator<K,V>(HashMap.this, 0, -1, 0, 0); else return Spliterators.spliterator (this, Spliterator.SIZED); } - - public Stream<V> stream() { - return Streams.stream(spliterator()); - } - - public Stream<V> parallelStream() { - return Streams.parallelStream(spliterator()); - } } /** @@ -1037,21 +1021,13 @@ HashMap.this.clear(); } - Spliterator<Map.Entry<K,V>> spliterator() { + public Spliterator<Map.Entry<K,V>> spliterator() { if (HashMap.this.getClass() == HashMap.class) return new EntrySpliterator<K,V>(HashMap.this, 0, -1, 0, 0); else return Spliterators.spliterator (this, Spliterator.SIZED | Spliterator.DISTINCT); } - - public Stream<Map.Entry<K,V>> stream() { - return Streams.stream(spliterator()); - } - - public Stream<Map.Entry<K,V>> parallelStream() { - return Streams.parallelStream(spliterator()); - } } /**
--- a/src/share/classes/java/util/HashSet.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/HashSet.java Mon Mar 11 14:13:55 2013 +0100 @@ -314,16 +314,7 @@ } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new HashMap.KeySpliterator<E,Object>(map, 0, -1, 0, 0); } - - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - }
--- a/src/share/classes/java/util/IdentityHashMap.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/IdentityHashMap.java Mon Mar 11 14:13:55 2013 +0100 @@ -1014,18 +1014,9 @@ return result; } - Spliterator<K> spliterator() { + public Spliterator<K> spliterator() { return new KeySpliterator<>(IdentityHashMap.this, 0, -1, 0, 0); } - - public Stream<K> stream() { - return Streams.stream(spliterator()); - } - - public Stream<K> parallelStream() { - return Streams.parallelStream(spliterator()); - } - } /** @@ -1079,17 +1070,9 @@ IdentityHashMap.this.clear(); } - Spliterator<V> spliterator() { + public Spliterator<V> spliterator() { return new ValueSpliterator<>(IdentityHashMap.this, 0, -1, 0, 0); } - - public Stream<V> stream() { - return Streams.stream(spliterator()); - } - - public Stream<V> parallelStream() { - return Streams.parallelStream(spliterator()); - } } /** @@ -1199,18 +1182,9 @@ return a; } - Spliterator<Map.Entry<K,V>> spliterator() { + public Spliterator<Map.Entry<K,V>> spliterator() { return new EntrySpliterator<>(IdentityHashMap.this, 0, -1, 0, 0); } - - public Stream<Map.Entry<K,V>> stream() { - return Streams.stream(spliterator()); - } - - public Stream<Map.Entry<K,V>> parallelStream() { - return Streams.parallelStream(spliterator()); - } - }
--- a/src/share/classes/java/util/Iterator.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/Iterator.java Mon Mar 11 14:13:55 2013 +0100 @@ -100,26 +100,27 @@ /** * Performs the given action, in the order elements occur when iterating, - * for all remaining elements. If successful subsequent calls to - * {@link #hasNext} will return {@code false}. Exceptions occurring as a - * result of performing this action are relayed to the caller. + * until all remaining elements have been processed or the action throws + * an {@code Exception}. Exceptions occurring as a result of performing + * this action are relayed to the caller. * - * @apiNote - * <p>This implementation may be overridden by implementing Collections to - * provide optimal performance or to include this method within their - * synchronization protocol. + * <p>The default implementation should be overridden by implementations if + * they can provide a more performant implementation than an iterator-based + * one, or to include this method within their synchronization protocol. * - * @implNote - * <p>This implementation repeatedly invokes {@link #next} and performs the - * action on the next element until {@link #hasNext()} returns - * {@code false}. + * @implSpec + * <p>The default implementation behaves as if: + * <pre> + * while (hasNext()) + * action.accept(next()); + * </pre> * * <p>This implementation does not have knowledge of the synchronization * protocol used by the implementing Collection. It is the caller's * responsibility to ensure that usage follows the correct synchronization - * protocol for the implementing Collection. + * protocol for the implementing {@code Iterator}. * - * @param action The action + * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */
--- a/src/share/classes/java/util/LinkedHashSet.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/LinkedHashSet.java Mon Mar 11 14:13:55 2013 +0100 @@ -172,13 +172,17 @@ addAll(c); } + /** + * Creates a {@code Spliterator}, over the elements in this set, that + * reports {@code SIZED}, {@code DISTINCT} and {@code ORDERED}. + * Overriding implementations are expected to document if the + * {@code Spliterator} reports any additional and relevant characteristic + * values. + * + * @return a {@code Spliterator} over the elements in this set + */ @Override - public Stream<E> stream() { - return Streams.stream(Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED)); - } - - @Override - public Stream<E> parallelStream() { - return Streams.parallelStream(Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED)); + public Spliterator<E> spliterator() { + return Spliterators.spliterator(this, Spliterator.DISTINCT | Spliterator.ORDERED); } }
--- a/src/share/classes/java/util/LinkedList.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/LinkedList.java Mon Mar 11 14:13:55 2013 +0100 @@ -1139,18 +1139,10 @@ linkLast((E)s.readObject()); } - Spliterator<E> spliterator() { // to be made public later + public Spliterator<E> spliterator() { return new LLSpliterator<E>(this, -1, 0); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - // A specialization of iteratorBasedSpliterator static final class LLSpliterator<E> implements Spliterator<E> { static final int MAX_BATCH = 1 << 12; // saturate batch size
--- a/src/share/classes/java/util/List.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/List.java Mon Mar 11 14:13:55 2013 +0100 @@ -626,27 +626,16 @@ } /** - * Create an ordered {@code Stream} over the elements of this list. - * Elements appear in the {@code Stream} in the order they appear in - * this list. + * Creates a {@code Spliterator}, over the elements in this list, that + * reports {@code SIZED} and {@code ORDERED}. Overriding implementations + * are expected to document if the {@code Spliterator} reports any + * additional and relevant characteristic values. * - * @return a {@code Stream} of elements in this list + * @return a {@code Spliterator} over the elements in this list */ @Override - public default Stream<E> stream() { - return Streams.stream(Spliterators.spliterator(this, Spliterator.ORDERED)); - } - - /** - * Create an ordered parallel {@code Stream} over the elements of this list. - * Elements appear in the {@code Stream} in the order they appear in - * this list. - * - * @return a {@code Stream} of elements in this list - */ - @Override - public default Stream<E> parallelStream() { - return Streams.parallelStream(Spliterators.spliterator(this, Spliterator.ORDERED)); + public default Spliterator<E> spliterator() { + return Spliterators.spliterator(this, Spliterator.ORDERED); } }
--- a/src/share/classes/java/util/PrimitiveIterator.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/PrimitiveIterator.java Mon Mar 11 14:13:55 2013 +0100 @@ -53,27 +53,28 @@ /** * Performs the given action, in the order elements occur when - * iterating, for all remaining elements. If successful subsequent - * calls to {@link #hasNext} will return {@code false}. Exceptions - * occurring as a result of performing this action are relayed to the - * caller. + * iterating, until all remaining elements have been processed or the + * action throws an {@code Exception}. Exceptions occurring as a result + * of performing this action are relayed to the caller. * - * @apiNote - * <p>This implementation may be overridden by implementing Collections - * to provide optimal performance or to include this method within their + * <p>The default implementation should be overridden by implementations + * if they can provide a more performant implementation than an + * iterator-based one, or to include this method within their * synchronization protocol. * - * @implNote - * <p>This implementation repeatedly invokes {@link #nextInt()} and - * performs the action on the next element until {@link #hasNext()} - * returns {@code false}. + * @implSpec + * <p>The default implementation behaves as if: + * <pre> + * while (hasNext()) + * action.accept(nextInt()); + * </pre> * * <p>This implementation does not have knowledge of the synchronization * protocol used by the implementing Collection. It is the caller's * responsibility to ensure that usage follows the correct - * synchronization protocol for the implementing Collection. + * synchronization protocol for the implementing {@code Iterator.OfInt}. * - * @param action The action + * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ @@ -101,8 +102,8 @@ * This implementation defers to * {@link #forEachInt(java.util.function.IntConsumer)}. If the * action is an instance of {@code IntConsumer} then it defers as - * follows: {@code forEachInt((IntConsumer) action)}. Otherwise, it - * defers as follows: {@code forEachInt((IntConsumer) action::accept)}, + * if: {@code forEachInt((IntConsumer) action)}. Otherwise, it + * defers as if: {@code forEachInt((IntConsumer) action::accept)}, * and a warning may be output that boxing of {@code int} values will * occur. */ @@ -135,27 +136,29 @@ /** * Performs the given action, in the order elements occur when - * iterating, for all remaining elements. If successful subsequent - * calls to {@link #hasNext} will return {@code false}. Exceptions - * occurring as a result of performing this action are relayed to the - * caller. + * iterating, until all remaining elements have been processed or the + * action throws an {@code Exception}. Exceptions occurring as a result + * of performing this action are relayed to the caller. * - * @apiNote - * <p>This implementation may be overridden by implementing Collections - * to provide optimal performance or to include this method within their + * <p>The default implementation should be overridden by implementations + * if they can provide a more performant implementation than an + * iterator-based one, or to include this method within their * synchronization protocol. * - * @implNote - * <p>This implementation repeatedly invokes {@link #nextLong()} and - * performs the action on the next element until {@link #hasNext()} - * returns {@code false}. + * @implSpec + * <p>The default implementation behaves as if: + * <pre> + * while (hasNext()) + * action.accept(nextLong()); + * </pre> * * <p>This implementation does not have knowledge of the synchronization * protocol used by the implementing Collection. It is the caller's * responsibility to ensure that usage follows the correct - * synchronization protocol for the implementing Collection. + * synchronization protocol for the implementing + * {@code Iterator.OfLong}. * - * @param action The action + * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ @@ -183,8 +186,8 @@ * This implementation defers to * {@link #forEachLong(java.util.function.LongConsumer)}. If the * action is an instance of {@code LongConsumer} then it defers as - * follows: {@code forEachLong((LongConsumer) action)}. Otherwise, it - * defers as follows: {@code forEachLong((LongConsumer) action::accept)}, + * if: {@code forEachLong((LongConsumer) action)}. Otherwise, it + * defers as if: {@code forEachLong((LongConsumer) action::accept)}, * and a warning may be output that boxing of {@code long} values will * occur. */ @@ -216,27 +219,29 @@ /** * Performs the given action, in the order elements occur when - * iterating, for all remaining elements. If successful subsequent - * calls to {@link #hasNext} will return {@code false}. Exceptions - * occurring as a result of performing this action are relayed to the - * caller. + * iterating, until all remaining elements have been processed or the + * action throws an {@code Exception}. Exceptions occurring as a result + * of performing this action are relayed to the caller. * - * @apiNote - * <p>This implementation may be overridden by implementing Collections - * to provide optimal performance or to include this method within their + * <p>The default implementation should be overridden by implementations + * if they can provide a more performant implementation than an + * iterator-based one, or to include this method within their * synchronization protocol. * - * @implNote - * <p>This implementation repeatedly invokes {@link #nextDouble()} and - * performs the action on the next element until {@link #hasNext()} - * returns {@code false}. + * @implSpec + * <p>The default implementation behaves as if: + * <pre> + * while (hasNext()) + * action.accept(nextDouble()); + * </pre> * * <p>This implementation does not have knowledge of the synchronization * protocol used by the implementing Collection. It is the caller's * responsibility to ensure that usage follows the correct - * synchronization protocol for the implementing Collection. + * synchronization protocol for the implementing + * {@code Iterator.OfDouble}. * - * @param action The action + * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ @@ -264,8 +269,8 @@ * This implementation defers to * {@link #forEachDouble(java.util.function.DoubleConsumer)}. If the * action is an instance of {@code DoubleConsumer} then it defers as - * follows: {@code forEachDouble((DoubleConsumer) action)}. Otherwise, - * it defers as follows: + * if: {@code forEachDouble((DoubleConsumer) action)}. Otherwise, + * it defers as if: * {@code forEachDouble((DoubleConsumer) action::accept)}, and a warning * may be output that boxing of {@code double} values will occur. */
--- a/src/share/classes/java/util/PriorityQueue.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/PriorityQueue.java Mon Mar 11 14:13:55 2013 +0100 @@ -783,18 +783,10 @@ heapify(); } - final Spliterator<E> spliterator() { + public final Spliterator<E> spliterator() { return new PriorityQueueSpliterator<E>(this, 0, -1, 0); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - static final class PriorityQueueSpliterator<E> implements Spliterator<E> { /* * This is very similar to ArrayList Spliterator, except for
--- a/src/share/classes/java/util/Set.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/Set.java Mon Mar 11 14:13:55 2013 +0100 @@ -386,13 +386,16 @@ */ int hashCode(); + /** + * Creates a {@code Spliterator}, over the elements in this set, that + * reports {@code SIZED} and {@code DISTINCT}. Overriding implementations + * are expected to document if the {@code Spliterator} reports any + * additional and relevant characteristic values. + * + * @return a {@code Spliterator} over the elements in this set + */ @Override - default Stream<E> stream() { - return Streams.stream(Spliterators.spliterator(this, Spliterator.DISTINCT)); - } - - @Override - default Stream<E> parallelStream() { - return Streams.parallelStream(Spliterators.spliterator(this, Spliterator.DISTINCT)); + default Spliterator<E> spliterator() { + return Spliterators.spliterator(this, Spliterator.DISTINCT); } }
--- a/src/share/classes/java/util/SortedSet.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/SortedSet.java Mon Mar 11 14:13:55 2013 +0100 @@ -223,15 +223,34 @@ */ E last(); + /** + * Creates a {@code Spliterator}, over the elements in this sorted set, that + * reports {@code SIZED}, {@code DISTINCT}, {@ocde SORTED} and + * {@code ORDERED}. Overriding implementations are expected to document if + * the {@code Spliterator} reports any additional and relevant + * characteristic values. + * + * <p>The spliterator's comparator (see + * {@link java.util.Spliterator#getComparator()}) must be {@code null} if + * the sorted set's comparator (see {@link #comparator()}) is {@code null}. + * Otherwise, the spliterator's comparator must be the same as or impose the + * same total ordering as the sorted set's comparator. + * + * @implSpec + * This implementation creates a <em>late-binding</em> spliterator from the + * collection's {@code Iterator}. The spliterator's comparator is the same + * as the sorted set's comparator. + * + * @return a {@code Spliterator} over the elements in this sorted set + */ @Override - default Stream<E> stream() { - return Streams.stream(Spliterators.spliterator( - this, Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED)); - } - - @Override - default Stream<E> parallelStream() { - return Streams.parallelStream(Spliterators.spliterator( - this, Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED)); + default Spliterator<E> spliterator() { + return new Spliterators.IteratorSpliterator<E>( + this, Spliterator.DISTINCT | Spliterator.SORTED | Spliterator.ORDERED) { + @Override + public Comparator<? super E> getComparator() { + return this.getComparator(); + } + }; } }
--- a/src/share/classes/java/util/Spliterators.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/Spliterators.java Mon Mar 11 14:13:55 2013 +0100 @@ -907,7 +907,7 @@ * operations. The spliterator implements {@code trySplit} to * permit limited parallelism. */ - static final class IteratorSpliterator<T> implements Spliterator<T> { + static class IteratorSpliterator<T> implements Spliterator<T> { static final int MAX_BATCH = 1 << 12; // saturate batch size; see below private final Collection<T> collection; // null OK private Iterator<T> it;
--- a/src/share/classes/java/util/TreeMap.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/TreeMap.java Mon Mar 11 14:13:55 2013 +0100 @@ -976,16 +976,9 @@ TreeMap.this.clear(); } - Spliterator<V> spliterator() { + public Spliterator<V> spliterator() { return new ValueSpliterator<K,V>(TreeMap.this, null, null, 0, -1, 0); } - public Stream<V> stream() { - return Streams.stream(spliterator()); - } - - public Stream<V> parallelStream() { - return Streams.parallelStream(spliterator()); - } } class EntrySet extends AbstractSet<Map.Entry<K,V>> { @@ -1023,17 +1016,9 @@ TreeMap.this.clear(); } - Spliterator<Map.Entry<K,V>> spliterator() { + public Spliterator<Map.Entry<K,V>> spliterator() { return new EntrySpliterator<K,V>(TreeMap.this, null, null, 0, -1, 0); } - - public Stream<Map.Entry<K,V>> stream() { - return Streams.stream(spliterator()); - } - - public Stream<Map.Entry<K,V>> parallelStream() { - return Streams.parallelStream(spliterator()); - } } /* @@ -1118,17 +1103,9 @@ return new KeySet<>(m.descendingMap()); } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return keySpliteratorFor(m); } - - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } } /**
--- a/src/share/classes/java/util/TreeSet.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/TreeSet.java Mon Mar 11 14:13:55 2013 +0100 @@ -535,17 +535,9 @@ tm.readTreeSet(size, s, PRESENT); } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return TreeMap.keySpliteratorFor(m); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - private static final long serialVersionUID = -2479143000061671589L; }
--- a/src/share/classes/java/util/Vector.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/Vector.java Mon Mar 11 14:13:55 2013 +0100 @@ -1309,18 +1309,10 @@ modCount++; } - Spliterator<E> spliterator() { // to be made public later + public Spliterator<E> spliterator() { return new VectorSpliterator<>(this, null, 0, -1, 0); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - /** Similar to ArrayList Spliterator */ static final class VectorSpliterator<E> implements Spliterator<E> { private final Vector<E> list;
--- a/src/share/classes/java/util/WeakHashMap.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/WeakHashMap.java Mon Mar 11 14:13:55 2013 +0100 @@ -902,18 +902,9 @@ WeakHashMap.this.clear(); } - Spliterator<K> spliterator() { + public Spliterator<K> spliterator() { return new KeySpliterator<>(WeakHashMap.this, 0, -1, 0, 0); } - - public Stream<K> stream() { - return Streams.stream(spliterator()); - } - - public Stream<K> parallelStream() { - return Streams.parallelStream(spliterator()); - } - } /** @@ -951,18 +942,9 @@ WeakHashMap.this.clear(); } - Spliterator<V> spliterator() { + public Spliterator<V> spliterator() { return new ValueSpliterator<>(WeakHashMap.this, 0, -1, 0, 0); } - - public Stream<V> stream() { - return Streams.stream(spliterator()); - } - - public Stream<V> parallelStream() { - return Streams.parallelStream(spliterator()); - } - } /** @@ -1024,18 +1006,9 @@ return deepCopy().toArray(a); } - Spliterator<Map.Entry<K,V>> spliterator() { + public Spliterator<Map.Entry<K,V>> spliterator() { return new EntrySpliterator<>(WeakHashMap.this, 0, -1, 0, 0); } - - public Stream<Map.Entry<K,V>> stream() { - return Streams.stream(spliterator()); - } - - public Stream<Map.Entry<K,V>> parallelStream() { - return Streams.parallelStream(spliterator()); - } - } /**
--- a/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java Mon Mar 11 14:13:55 2013 +0100 @@ -1398,18 +1398,9 @@ // } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return Spliterators.spliterator (this, Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.CONCURRENT); } - - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - }
--- a/src/share/classes/java/util/concurrent/ConcurrentHashMap.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/ConcurrentHashMap.java Mon Mar 11 14:13:55 2013 +0100 @@ -4899,16 +4899,9 @@ return added; } - Spliterator<K> spliterator() { + public Spliterator<K> spliterator() { return new KeyIterator<>(map, null); } - - public Stream<K> stream() { - return Streams.stream(spliterator()); - } - public Stream<K> parallelStream() { - return Streams.parallelStream(spliterator()); - } } /** @@ -4958,18 +4951,9 @@ throw new UnsupportedOperationException(); } - Spliterator<V> spliterator() { + public Spliterator<V> spliterator() { return new ValueIterator<K,V>(map, null); } - - public Stream<V> stream() { - return Streams.stream(spliterator()); - } - - public Stream<V> parallelStream() { - return Streams.parallelStream(spliterator()); - } - } /** @@ -5034,17 +5018,9 @@ return added; } - Spliterator<Map.Entry<K,V>> spliterator() { + public Spliterator<Map.Entry<K,V>> spliterator() { return new EntryIterator<K,V>(map, null); } - - public Stream<Map.Entry<K,V>> stream() { - return Streams.stream(spliterator()); - } - - public Stream<Map.Entry<K,V>> parallelStream() { - return Streams.parallelStream(spliterator()); - } } // ---------------------------------------------------------------------
--- a/src/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java Mon Mar 11 14:13:55 2013 +0100 @@ -1474,18 +1474,10 @@ } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new CLDSpliterator<E>(this); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - /** * Saves this deque to a stream (that is, serializes it). *
--- a/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java Mon Mar 11 14:13:55 2013 +0100 @@ -885,19 +885,10 @@ } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new CLQSpliterator<E>(this); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - - /** * Throws NullPointerException if argument is null. *
--- a/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java Mon Mar 11 14:13:55 2013 +0100 @@ -2657,21 +2657,14 @@ public NavigableSet<E> descendingSet() { return new KeySet<E>(m.descendingMap()); } + @SuppressWarnings("unchecked") - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { if (m instanceof ConcurrentSkipListMap) return ((ConcurrentSkipListMap<E,?>)m).keySpliterator(); else return (Spliterator<E>)((SubMap<E,?>)m).keyIterator(); } - - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } } static final class Values<E> extends AbstractCollection<E> { @@ -2700,21 +2693,14 @@ } public Object[] toArray() { return toList(this).toArray(); } public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } + @SuppressWarnings("unchecked") - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { if (m instanceof ConcurrentSkipListMap) return ((ConcurrentSkipListMap<?,E>)m).valueSpliterator(); else return (Spliterator<E>)((SubMap<?,E>)m).valueIterator(); } - - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } } static final class EntrySet<K1,V1> extends AbstractSet<Map.Entry<K1,V1>> { @@ -2769,23 +2755,15 @@ } public Object[] toArray() { return toList(this).toArray(); } public <T> T[] toArray(T[] a) { return toList(this).toArray(a); } + @SuppressWarnings("unchecked") - Spliterator<Map.Entry<K1,V1>> spliterator() { + public Spliterator<Map.Entry<K1,V1>> spliterator() { if (m instanceof ConcurrentSkipListMap) return ((ConcurrentSkipListMap<K1,V1>)m).entrySpliterator(); else return (Spliterator<Map.Entry<K1,V1>>) ((SubMap<K1,V1>)m).entryIterator(); } - - public Stream<Map.Entry<K1,V1>> stream() { - return Streams.stream(spliterator()); - } - - public Stream<Map.Entry<K1,V1>> parallelStream() { - return Streams.parallelStream(spliterator()); - } - } /** @@ -3595,18 +3573,9 @@ return new KeySet<K>(m.descendingMap()); } - Spliterator<K> spliterator() { + public Spliterator<K> spliterator() { return m.keySpliterator(); } - - public Stream<K> stream() { - return Streams.stream(spliterator()); - } - - public Stream<K> parallelStream() { - return Streams.parallelStream(spliterator()); - } - } /**
--- a/src/share/classes/java/util/concurrent/ConcurrentSkipListSet.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/ConcurrentSkipListSet.java Mon Mar 11 14:13:55 2013 +0100 @@ -483,21 +483,13 @@ } @SuppressWarnings("unchecked") - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { if (m instanceof ConcurrentSkipListMap) return ((ConcurrentSkipListMap<E,?>)m).keySpliterator(); else return (Spliterator<E>)((ConcurrentSkipListMap.SubMap<E,?>)m).keyIterator(); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - // Support for resetting map in clone private void setMap(ConcurrentNavigableMap<E,Object> map) { UNSAFE.putObjectVolatile(this, mapOffset, map);
--- a/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java Mon Mar 11 14:13:55 2013 +0100 @@ -1016,7 +1016,7 @@ return new COWIterator<E>(elements, index); } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return Spliterators.spliterator (getArray(), Spliterator.IMMUTABLE | Spliterator.ORDERED); } @@ -1306,7 +1306,7 @@ } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { int lo = offset; int hi = offset + size; Object[] a = expectedArray; @@ -1317,14 +1317,6 @@ return Spliterators.spliterator (a, lo, hi, Spliterator.IMMUTABLE | Spliterator.ORDERED); } - - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } } private static class COWSubListIterator<E> implements ListIterator<E> {
--- a/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java Mon Mar 11 14:13:55 2013 +0100 @@ -392,7 +392,7 @@ return k == len; } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return Spliterators.spliterator (al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT | Spliterator.ORDERED);
--- a/src/share/classes/java/util/concurrent/LinkedBlockingDeque.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/LinkedBlockingDeque.java Mon Mar 11 14:13:55 2013 +0100 @@ -1265,16 +1265,9 @@ } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new LBDSpliterator<E>(this); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } /** * Saves this deque to a stream (that is, serializes it).
--- a/src/share/classes/java/util/concurrent/LinkedBlockingQueue.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/LinkedBlockingQueue.java Mon Mar 11 14:13:55 2013 +0100 @@ -967,18 +967,10 @@ } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new LBQSpliterator<E>(this); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - /** * Saves this queue to a stream (that is, serializes it). *
--- a/src/share/classes/java/util/concurrent/LinkedTransferQueue.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/LinkedTransferQueue.java Mon Mar 11 14:13:55 2013 +0100 @@ -1023,19 +1023,10 @@ } } - - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new LTQSpliterator<E>(this); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - /* -------------- Removal methods -------------- */ /**
--- a/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java Mon Mar 11 14:13:55 2013 +0100 @@ -1007,15 +1007,9 @@ } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return new PBQSpliterator<E>(this, null, 0, -1); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } // Unsafe mechanics private static final sun.misc.Unsafe UNSAFE;
--- a/src/share/classes/java/util/concurrent/SynchronousQueue.java Sun Mar 10 20:34:24 2013 -0400 +++ b/src/share/classes/java/util/concurrent/SynchronousQueue.java Mon Mar 11 14:13:55 2013 +0100 @@ -1081,18 +1081,10 @@ public void remove() { throw new IllegalStateException(); } } - Spliterator<E> spliterator() { + public Spliterator<E> spliterator() { return Spliterators.emptySpliterator(); } - public Stream<E> stream() { - return Streams.stream(spliterator()); - } - - public Stream<E> parallelStream() { - return Streams.parallelStream(spliterator()); - } - /** * Returns a zero-length array. * @return a zero-length array