use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.
the class IntPrimitiveOpsTests method testForEach.
@Test(groups = { "serialization-hostile" })
public void testForEach() {
final int[] sum = new int[1];
IntStreams.range(1, 10).filter(new IntPredicate() {
@Override
public boolean test(int i) {
return i % 2 == 0;
}
}).forEach(new IntConsumer() {
@Override
public void accept(int i) {
sum[0] = sum[0] + i;
}
});
assertEquals(sum[0], 20);
}
use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.
the class IntPrimitiveOpsTests method testParForEach.
@Test(groups = { "serialization-hostile" })
public void testParForEach() {
final AtomicInteger ai = new AtomicInteger(0);
IntStreams.range(1, 10).parallel().filter(new IntPredicate() {
@Override
public boolean test(int i) {
return i % 2 == 0;
}
}).forEach((IntConsumer) new IntConsumer() {
@Override
public void accept(int i) {
ai.addAndGet(i);
}
});
assertEquals(ai.get(), 20);
}
use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.
the class IntPrimitiveOpsTests method testTee.
@Test(groups = { "serialization-hostile" })
public void testTee() {
final int[] teeSum = new int[1];
long sum;
sum = IntStreams.range(1, 10).filter(new IntPredicate() {
@Override
public boolean test(int i) {
return i % 2 == 0;
}
}).peek(new IntConsumer() {
@Override
public void accept(int i) {
teeSum[0] = teeSum[0] + i;
}
}).sum();
assertEquals(teeSum[0], sum);
}
use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.
the class Spliterators method iterator.
/**
* Creates an {@code PrimitiveIterator.OfInt} from a
* {@code Spliterator.OfInt}.
*
* <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 spliterator The spliterator
* @return An iterator
* @throws NullPointerException if the given spliterator is {@code null}
*/
public static PrimitiveIterator.OfInt iterator(Spliterator.OfInt spliterator) {
Objects.requireNonNull(spliterator);
class Adapter implements PrimitiveIterator.OfInt, IntConsumer {
boolean valueReady = false;
int nextElement;
@Override
public void accept(int t) {
valueReady = true;
nextElement = t;
}
@Override
public boolean hasNext() {
if (!valueReady)
spliterator.tryAdvance(this);
return valueReady;
}
@Override
public int nextInt() {
if (!valueReady && !hasNext())
throw new NoSuchElementException();
else {
valueReady = false;
return nextElement;
}
}
@Override
public Integer next() {
return nextInt();
}
@Override
public void forEachRemaining(IntConsumer action) {
Iterators.forEachRemaining(this, action);
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
}
return new Adapter();
}
use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.
the class IntPipeline method forEachWithCancel.
@Override
final boolean forEachWithCancel(Spliterator<Integer> spliterator, Sink<Integer> sink) {
Spliterator.OfInt spl = adapt(spliterator);
IntConsumer adaptedSink = adapt(sink);
boolean cancelled;
do {
} while (!(cancelled = sink.cancellationRequested()) && spl.tryAdvance(adaptedSink));
return cancelled;
}
Aggregations