use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.
the class OnCloseTest method testOnCloseConcat.
@Test
public void testOnCloseConcat() {
final int[] counter = new int[] { 0 };
final Runnable runnable = new Runnable() {
@Override
public void run() {
counter[0]++;
}
};
IntStream stream1 = IntStream.range(0, 2).onClose(runnable);
IntStream stream2 = IntStream.range(0, 2).onClose(runnable);
IntStream stream = IntStream.concat(stream1, stream2).onClose(runnable);
stream.count();
assertThat(counter[0], is(0));
stream.close();
assertThat(counter[0], is(3));
}
use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.
the class OnCloseTest method testOnCloseWithOtherOperators.
@Test
public void testOnCloseWithOtherOperators() {
final boolean[] state = new boolean[] { false };
IntStream stream = IntStream.of(0, 1, 2, 2, 3, 4, 4, 5).filter(new IntPredicate() {
@Override
public boolean test(int value) {
return value < 4;
}
}).onClose(new Runnable() {
@Override
public void run() {
state[0] = true;
}
}).distinct().limit(2);
stream.findFirst();
stream.close();
assertTrue(state[0]);
}
use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.
the class OnCloseTest method testOnCloseFlatMap.
@Test
public void testOnCloseFlatMap() {
final int[] counter = new int[] { 0 };
final Runnable runnable = new Runnable() {
@Override
public void run() {
counter[0]++;
}
};
IntStream stream = IntStream.rangeClosed(2, 4).onClose(runnable).onClose(runnable).flatMap(new IntFunction<IntStream>() {
@Override
public IntStream apply(final int i) {
return IntStream.rangeClosed(2, 4).onClose(runnable);
}
});
stream.count();
assertThat(counter[0], is(3));
stream.close();
assertThat(counter[0], is(5));
}
use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.
the class OfPrimitiveIteratorTest method testStreamOfPrimitiveIterator.
@Test
public void testStreamOfPrimitiveIterator() {
int[] expected = { 0, 1 };
IntStream stream = IntStream.of(new PrimitiveIterator.OfInt() {
int index = 0;
@Override
public boolean hasNext() {
return index < 2;
}
@Override
public int nextInt() {
return index++;
}
});
assertThat(stream.toArray(), is(expected));
}
use of com.annimon.stream.IntStream in project Lightweight-Stream-API by aNNiMON.
the class ConcatTest method testStreamConcat.
@Test
public void testStreamConcat() {
IntStream a1 = IntStream.empty();
IntStream b1 = IntStream.empty();
IntStream.concat(a1, b1).custom(assertIsEmpty());
IntStream a2 = IntStream.of(1, 2, 3);
IntStream b2 = IntStream.empty();
IntStream.concat(a2, b2).custom(assertElements(arrayContaining(1, 2, 3)));
IntStream a3 = IntStream.empty();
IntStream b3 = IntStream.of(42);
IntStream.concat(a3, b3).custom(assertElements(arrayContaining(42)));
IntStream a4 = IntStream.of(2, 4, 6, 8);
IntStream b4 = IntStream.of(1, 3, 5, 7, 9);
IntStream.concat(a4, b4).custom(assertElements(arrayContaining(2, 4, 6, 8, 1, 3, 5, 7, 9)));
}
Aggregations