# HG changeset patch # User henryjen # Date 1373666468 25200 # Node ID 9fa4af2af63db6e695aa0c57a767fba8e30cf9e8 # Parent 6c67701507df208c6971a0c7884b2bc97725e482 8015320: Pull spliterator() up from Collection to Iterable Reviewed-by: psandoz, mduigou Contributed-by: brian.goetz@oracle.com diff -r 6c67701507df -r 9fa4af2af63d jdk/src/share/classes/java/lang/Iterable.java --- a/jdk/src/share/classes/java/lang/Iterable.java Fri Jul 12 20:44:34 2013 +0100 +++ b/jdk/src/share/classes/java/lang/Iterable.java Fri Jul 12 15:01:08 2013 -0700 @@ -26,6 +26,8 @@ import java.util.Iterator; import java.util.Objects; +import java.util.Spliterator; +import java.util.Spliterators; import java.util.function.Consumer; /** @@ -72,5 +74,30 @@ action.accept(t); } } + + /** + * Creates a {@link Spliterator} over the elements described by this + * {@code Iterable}. + * + * @implSpec + * The default implementation creates an + * early-binding + * spliterator from the iterable's {@code Iterator}. The spliterator + * inherits the fail-fast properties of the iterable's iterator. + * + * @implNote + * The default implementation should usually be overridden. The + * spliterator returned by the default implementation has poor splitting + * capabilities, is unsized, and does not report any spliterator + * characteristics. Implementing classes can nearly always provide a + * better implementation. + * + * @return a {@code Spliterator} over the elements described by this + * {@code Iterable}. + * @since 1.8 + */ + default Spliterator spliterator() { + return Spliterators.spliteratorUnknownSize(iterator(), 0); + } } diff -r 6c67701507df -r 9fa4af2af63d jdk/src/share/classes/java/util/Collection.java --- a/jdk/src/share/classes/java/util/Collection.java Fri Jul 12 20:44:34 2013 +0100 +++ b/jdk/src/share/classes/java/util/Collection.java Fri Jul 12 15:01:08 2013 -0700 @@ -537,6 +537,7 @@ * @return a {@code Spliterator} over the elements in this collection * @since 1.8 */ + @Override default Spliterator spliterator() { return Spliterators.spliterator(this, 0); } diff -r 6c67701507df -r 9fa4af2af63d jdk/src/share/classes/java/util/ConcurrentModificationException.java --- a/jdk/src/share/classes/java/util/ConcurrentModificationException.java Fri Jul 12 20:44:34 2013 +0100 +++ b/jdk/src/share/classes/java/util/ConcurrentModificationException.java Fri Jul 12 15:01:08 2013 -0700 @@ -57,6 +57,7 @@ * @author Josh Bloch * @see Collection * @see Iterator + * @see Spliterator * @see ListIterator * @see Vector * @see LinkedList diff -r 6c67701507df -r 9fa4af2af63d jdk/test/java/util/Spliterator/SpliteratorCollisions.java --- a/jdk/test/java/util/Spliterator/SpliteratorCollisions.java Fri Jul 12 20:44:34 2013 +0100 +++ b/jdk/test/java/util/Spliterator/SpliteratorCollisions.java Fri Jul 12 15:01:08 2013 -0700 @@ -48,7 +48,6 @@ import java.util.TreeSet; import java.util.function.Consumer; import java.util.function.Function; -import java.util.function.LongConsumer; import java.util.function.Supplier; import java.util.function.UnaryOperator; @@ -677,11 +676,11 @@ private static Map toBoxedMultiset(Iterable c) { Map result = new HashMap<>(); - c.forEach((Consumer) e -> { - if (result.containsKey((T)e)) { - result.put((T)e, new HashableInteger(((HashableInteger)result.get(e)).value + 1, 10)); + c.forEach(e -> { + if (result.containsKey(e)) { + result.put(e, new HashableInteger(result.get(e).value + 1, 10)); } else { - result.put((T)e, new HashableInteger(1, 10)); + result.put(e, new HashableInteger(1, 10)); } }); return result; diff -r 6c67701507df -r 9fa4af2af63d jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java --- a/jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java Fri Jul 12 20:44:34 2013 +0100 +++ b/jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java Fri Jul 12 15:01:08 2013 -0700 @@ -321,6 +321,21 @@ db.addCollection( c -> new AbstractSortedSetImpl(c)); + class IterableWrapper implements Iterable { + final Iterable it; + + IterableWrapper(Iterable it) { + this.it = it; + } + + @Override + public Iterator iterator() { + return it.iterator(); + } + } + db.add("new Iterable.spliterator()", + () -> new IterableWrapper(exp).spliterator()); + // db.add("Arrays.asList().spliterator()",