提交 c93a4cd9 编写于 作者: H henryjen

8015320: Pull spliterator() up from Collection to Iterable

Reviewed-by: psandoz, mduigou
Contributed-by: brian.goetz@oracle.com
上级 29367018
......@@ -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);
}
}
......@@ -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);
}
......
......@@ -57,6 +57,7 @@ package java.util;
* @author Josh Bloch
* @see Collection
* @see Iterator
* @see Spliterator
* @see ListIterator
* @see Vector
* @see LinkedList
......
......@@ -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;
......
......@@ -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()",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册