8015320: Pull spliterator() up from Collection to Iterable
authorhenryjen
Fri, 12 Jul 2013 15:01:08 -0700
changeset 18824 9fa4af2af63d
parent 18823 6c67701507df
child 18825 06636235cd12
8015320: Pull spliterator() up from Collection to Iterable Reviewed-by: psandoz, mduigou Contributed-by: brian.goetz@oracle.com
jdk/src/share/classes/java/lang/Iterable.java
jdk/src/share/classes/java/util/Collection.java
jdk/src/share/classes/java/util/ConcurrentModificationException.java
jdk/test/java/util/Spliterator/SpliteratorCollisions.java
jdk/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.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
+     * <em><a href="Spliterator.html#binding">early-binding</a></em>
+     * spliterator from the iterable's {@code Iterator}.  The spliterator
+     * inherits the <em>fail-fast</em> 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<T> spliterator() {
+        return Spliterators.spliteratorUnknownSize(iterator(), 0);
+    }
 }
 
--- 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<E> spliterator() {
         return Spliterators.spliterator(this, 0);
     }
--- 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
--- 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 <T> Map<T, HashableInteger> toBoxedMultiset(Iterable<T> c) {
         Map<T, HashableInteger> 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;
--- 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<Integer> {
+                final Iterable<Integer> it;
+
+                IterableWrapper(Iterable<Integer> it) {
+                    this.it = it;
+                }
+
+                @Override
+                public Iterator<Integer> iterator() {
+                    return it.iterator();
+                }
+            }
+            db.add("new Iterable.spliterator()",
+                   () -> new IterableWrapper(exp).spliterator());
+
             //
 
             db.add("Arrays.asList().spliterator()",