Search in sources :

Example 6 with LongStream

use of com.annimon.stream.LongStream 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]++;
        }
    };
    LongStream stream = LongStream.rangeClosed(2, 4).onClose(runnable).onClose(runnable).flatMap(new LongFunction<LongStream>() {

        @Override
        public LongStream apply(final long i) {
            return LongStream.rangeClosed(2, 4).onClose(runnable);
        }
    });
    stream.count();
    assertThat(counter[0], is(3));
    stream.close();
    assertThat(counter[0], is(5));
}
Also used : LongStream(com.annimon.stream.LongStream) Test(org.junit.Test)

Example 7 with LongStream

use of com.annimon.stream.LongStream in project Lightweight-Stream-API by aNNiMON.

the class OfPrimitiveIteratorTest method testStreamOfPrimitiveIterator.

@Test
public void testStreamOfPrimitiveIterator() {
    LongStream stream = LongStream.of(new PrimitiveIterator.OfLong() {

        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < 3;
        }

        @Override
        public long nextLong() {
            return ++index;
        }
    });
    assertThat(stream, elements(arrayContaining(1L, 2L, 3L)));
}
Also used : PrimitiveIterator(com.annimon.stream.iterator.PrimitiveIterator) LongStream(com.annimon.stream.LongStream) Test(org.junit.Test)

Example 8 with LongStream

use of com.annimon.stream.LongStream in project Lightweight-Stream-API by aNNiMON.

the class ConcatTest method testStreamConcat.

@Test
public void testStreamConcat() {
    LongStream a1 = LongStream.empty();
    LongStream b1 = LongStream.empty();
    LongStream.concat(a1, b1).custom(assertIsEmpty());
    LongStream a2 = LongStream.of(100200300L, 1234567L);
    LongStream b2 = LongStream.empty();
    LongStream.concat(a2, b2).custom(assertElements(arrayContaining(100200300L, 1234567L)));
    LongStream a3 = LongStream.of(100200300L, 1234567L);
    LongStream b3 = LongStream.empty();
    LongStream.concat(a3, b3).custom(assertElements(arrayContaining(100200300L, 1234567L)));
    LongStream a4 = LongStream.of(-5L, 1234567L, -Integer.MAX_VALUE, Long.MAX_VALUE);
    LongStream b4 = LongStream.of(Integer.MAX_VALUE, 100200300L);
    LongStream.concat(a4, b4).custom(assertElements(arrayContaining(-5L, 1234567L, (long) -Integer.MAX_VALUE, Long.MAX_VALUE, (long) Integer.MAX_VALUE, 100200300L)));
}
Also used : LongStream(com.annimon.stream.LongStream) Test(org.junit.Test)

Example 9 with LongStream

use of com.annimon.stream.LongStream in project Lightweight-Stream-API by aNNiMON.

the class OnCloseTest method testOnCloseWithOtherOperators.

@Test
public void testOnCloseWithOtherOperators() {
    final boolean[] state = new boolean[] { false };
    LongStream stream = LongStream.of(0, 1, 2, 2, 3, 4, 4, 5).filter(new LongPredicate() {

        @Override
        public boolean test(long 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 : LongStream(com.annimon.stream.LongStream) LongPredicate(com.annimon.stream.function.LongPredicate) Test(org.junit.Test)

Example 10 with LongStream

use of com.annimon.stream.LongStream in project Lightweight-Stream-API by aNNiMON.

the class OnCloseTest method testOnClose.

@Test
public void testOnClose() {
    final boolean[] state = new boolean[] { false };
    LongStream stream = LongStream.of(0, 1, 2).onClose(new Runnable() {

        @Override
        public void run() {
            state[0] = true;
        }
    });
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}
Also used : LongStream(com.annimon.stream.LongStream) Test(org.junit.Test)

Aggregations

LongStream (com.annimon.stream.LongStream)11 Test (org.junit.Test)9 CustomOperators (com.annimon.stream.CustomOperators)1 Function (com.annimon.stream.function.Function)1 LongBinaryOperator (com.annimon.stream.function.LongBinaryOperator)1 LongPredicate (com.annimon.stream.function.LongPredicate)1 LongSupplier (com.annimon.stream.function.LongSupplier)1 PrimitiveIterator (com.annimon.stream.iterator.PrimitiveIterator)1