Search in sources :

Example 6 with IntStream

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));
}
Also used : IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Example 7 with IntStream

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]);
}
Also used : IntPredicate(com.annimon.stream.function.IntPredicate) IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Example 8 with IntStream

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));
}
Also used : IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Example 9 with IntStream

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));
}
Also used : PrimitiveIterator(com.annimon.stream.iterator.PrimitiveIterator) IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Example 10 with IntStream

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)));
}
Also used : IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Aggregations

IntStream (com.annimon.stream.IntStream)13 Test (org.junit.Test)11 CustomOperators (com.annimon.stream.CustomOperators)2 ArrayList (java.util.ArrayList)2 DoubleStream (com.annimon.stream.DoubleStream)1 DoubleBinaryOperator (com.annimon.stream.function.DoubleBinaryOperator)1 Function (com.annimon.stream.function.Function)1 IntBinaryOperator (com.annimon.stream.function.IntBinaryOperator)1 IntPredicate (com.annimon.stream.function.IntPredicate)1 PrimitiveIterator (com.annimon.stream.iterator.PrimitiveIterator)1