use of java8.util.Spliterators in project streamsupport by stefan-zobel.
the class Collection8Test method testLateBindingStyle.
/**
* Spliterators are either IMMUTABLE or truly late-binding or, if
* concurrent, use the same "late-binding style" of returning
* elements added between creation and first use.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(dataProvider = "Source")
public void testLateBindingStyle(String description, Supplier<CollectionImplementation> sci) {
if (!testImplementationDetails)
return;
CollectionImplementation impl = sci.get();
// for jdk8
if (impl.klazz() == ArrayList.class)
return;
// Immutable (snapshot) spliterators are exempt
if (Spliterators.spliterator(impl.emptyCollection()).hasCharacteristics(Spliterator.IMMUTABLE))
return;
final Object one = impl.makeElement(1);
{
final Collection c = impl.emptyCollection();
final Spliterator<?> split = Spliterators.spliterator(c);
c.add(one);
assertTrue(split.tryAdvance(e -> {
assertSame(e, one);
}));
assertFalse(split.tryAdvance(e -> {
throw new AssertionError();
}));
assertTrue(c.contains(one));
}
{
final AtomicLong count = new AtomicLong(0);
final Collection c = impl.emptyCollection();
final Spliterator<?> split = Spliterators.spliterator(c);
c.add(one);
split.forEachRemaining(e -> {
assertSame(e, one);
count.getAndIncrement();
});
assertEquals(1L, count.get());
assertFalse(split.tryAdvance(e -> {
throw new AssertionError();
}));
assertTrue(c.contains(one));
}
}
use of java8.util.Spliterators in project streamsupport by stefan-zobel.
the class Collection8Test method testStickySpliteratorExhaustion.
/**
* Concurrent Spliterators, once exhausted, stay exhausted.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test(dataProvider = "Source")
public void testStickySpliteratorExhaustion(String description, Supplier<CollectionImplementation> sci) throws Throwable {
CollectionImplementation impl = sci.get();
if (HAS_JAVA8_SPLITERATOR_BUG && PriorityBlockingQueue.class.equals(impl.klazz())) {
// https://bugs.openjdk.java.net/browse/JDK-8172023
return;
}
if (!impl.isConcurrent())
return;
if (!testImplementationDetails)
return;
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final Consumer<?> alwaysThrows = e -> {
throw new AssertionError();
};
final Collection c = impl.emptyCollection();
final Spliterator s = Spliterators.spliterator(c);
if (rnd.nextBoolean()) {
assertFalse(s.tryAdvance(alwaysThrows));
} else {
s.forEachRemaining(alwaysThrows);
}
final Object one = impl.makeElement(1);
// Spliterator should not notice added element
c.add(one);
if (rnd.nextBoolean()) {
assertFalse(s.tryAdvance(alwaysThrows));
} else {
s.forEachRemaining(alwaysThrows);
}
}
use of java8.util.Spliterators in project streamsupport by stefan-zobel.
the class Spliterators method iterator.
// Iterators from Spliterators
/**
* Creates an {@code Iterator} from a {@code Spliterator}.
*
* <p>Traversal of elements should be accomplished through the iterator.
* The behaviour of traversal is undefined if the spliterator is operated
* after the iterator is returned.
*
* @param <T> Type of elements
* @param spliterator The spliterator
* @return An iterator
* @throws NullPointerException if the given spliterator is {@code null}
*/
public static <T> Iterator<T> iterator(Spliterator<? extends T> spliterator) {
Objects.requireNonNull(spliterator);
class Adapter implements Iterator<T>, Consumer<T> {
boolean valueReady = false;
T nextElement;
@Override
public void accept(T t) {
valueReady = true;
nextElement = t;
}
@Override
public boolean hasNext() {
if (!valueReady)
spliterator.tryAdvance(this);
return valueReady;
}
@Override
public T next() {
if (!valueReady && !hasNext())
throw new NoSuchElementException();
else {
valueReady = false;
return nextElement;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
}
return new Adapter();
}
Aggregations