8015320: Pull spliterator() up from Collection to Iterable

Reviewed-by: psandoz, mduigou
This commit is contained in:
Brian Goetz 2013-07-12 15:01:08 -07:00 committed by Henry Jen
parent 3300eed0bb
commit ace3a4d196
5 changed files with 48 additions and 5 deletions

View File

@ -26,6 +26,8 @@ package java.lang;
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 @@ public interface Iterable<T> {
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);
}
}

View File

@ -537,6 +537,7 @@ public interface Collection<E> extends Iterable<E> {
* @return a {@code Spliterator} over the elements in this collection
* @since 1.8
*/
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}

View File

@ -57,6 +57,7 @@ package java.util;
* @author Josh Bloch
* @see Collection
* @see Iterator
* @see Spliterator
* @see ListIterator
* @see Vector
* @see LinkedList

View File

@ -48,7 +48,6 @@ import java.util.Spliterator;
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 @@ public class SpliteratorCollisions {
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;

View File

@ -321,6 +321,21 @@ public class SpliteratorTraversingAndSplittingTest {
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()",