changeset 8903:a35de55b9a42

Add Iterable.spliterator()
author briangoetz
date Wed, 26 Jun 2013 15:41:05 -0400
parents 567458424515
children 473ca46f3096
files src/share/classes/java/lang/Iterable.java src/share/classes/java/util/Collection.java test-ng/build.xml test/java/util/Spliterator/SpliteratorCollisions.java test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java
diffstat 5 files changed, 45 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/lang/Iterable.java	Wed Jun 26 15:38:29 2013 -0400
+++ b/src/share/classes/java/lang/Iterable.java	Wed Jun 26 15:41:05 2013 -0400
@@ -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,26 @@
             action.accept(t);
         }
     }
+
+    /**
+     * Creates a {@link Spliterator} over the elements described by this
+     * {@code Iterable}.
+     *
+     * @implNote
+     * <p>The default implementation should usually be overridden.  The
+     * spliterator returned by the default implementation has poor splitting
+     * characteristics, is unsized, does not report any other spliterator
+     * characteristics, and is <em><a href="Spliterator.html#binding">early-binding</a></em>.
+     * Implementing classes can nearly always provide a better implementation.
+     * The returned spliterator inherits the <em>fail-fast</em> properties of the
+     * collection's iterator.
+     *
+     * @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/src/share/classes/java/util/Collection.java	Wed Jun 26 15:38:29 2013 -0400
+++ b/src/share/classes/java/util/Collection.java	Wed Jun 26 15:41:05 2013 -0400
@@ -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/test-ng/build.xml	Wed Jun 26 15:38:29 2013 -0400
+++ b/test-ng/build.xml	Wed Jun 26 15:41:05 2013 -0400
@@ -37,6 +37,7 @@
     <property name="bootlib.classes.dir" value="${build.dir}/test-bootlib"/>
     <property name="test.reports.dir" value="${build.dir}/test-reports"/>
     <property name="lambda.test.dir" value="../test/jdk/lambda" />
+    <property name="spliterator.test.dir" value="../test/java/util/Spliterator" />
     <property name="tests.src.dir" value="../test/java/util/stream/test"/>
     <property name="boottests.src.dir" value="../test/java/util/stream/boottest"/>
     <property name="bootlib.src.dir" value="../test/java/util/stream/bootlib"/>
@@ -92,7 +93,7 @@
             <compilerarg value="-Xlint:all"/>
             <compilerarg value="-XDignore.symbol.file"/>
         </javac>
-        <javac destdir="${tests.classes.dir}" debug="on" srcdir="${tests.src.dir}:${lambda.test.dir}" fork="true"
+        <javac destdir="${tests.classes.dir}" debug="on" srcdir="${tests.src.dir}:${lambda.test.dir}:${spliterator.test.dir}" fork="true"
                classpathref="compile.class.path">
             <compilerarg value="-Xlint:all"/>
             <compilerarg value="-XDignore.symbol.file"/>
--- a/test/java/util/Spliterator/SpliteratorCollisions.java	Wed Jun 26 15:38:29 2013 -0400
+++ b/test/java/util/Spliterator/SpliteratorCollisions.java	Wed Jun 26 15:41:05 2013 -0400
@@ -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/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java	Wed Jun 26 15:38:29 2013 -0400
+++ b/test/java/util/Spliterator/SpliteratorTraversingAndSplittingTest.java	Wed Jun 26 15:41:05 2013 -0400
@@ -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()",