Search in sources :

Example 1 with IntSupplier

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

the class LimitTest method testLimit.

@Test
public void testLimit() {
    assertTrue(IntStream.of(1, 2, 3, 4, 5, 6).limit(3).count() == 3);
    assertTrue(IntStream.generate(new IntSupplier() {

        int current = 42;

        @Override
        public int getAsInt() {
            current = current + current << 1;
            return current;
        }
    }).limit(6).count() == 6);
}
Also used : IntSupplier(com.annimon.stream.function.IntSupplier) Test(org.junit.Test)

Example 2 with IntSupplier

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

the class CollectorsTest method testFlatMapping.

@Test
public void testFlatMapping() {
    Function<Integer, Stream<Integer>> repeaterFunction = new Function<Integer, Stream<Integer>>() {

        @Override
        public Stream<Integer> apply(final Integer value) {
            if (value < 0)
                return null;
            if (value == 0)
                return Stream.empty();
            return IntStream.generate(new IntSupplier() {

                @Override
                public int getAsInt() {
                    return value;
                }
            }).limit(value).boxed();
        }
    };
    List<Integer> list;
    list = Stream.of(1, 2, 3, 4).collect(Collectors.flatMapping(repeaterFunction, Collectors.<Integer>toList()));
    assertThat(list, contains(1, 2, 2, 3, 3, 3, 4, 4, 4, 4));
    list = Stream.of(-1, -1).collect(Collectors.flatMapping(repeaterFunction, Collectors.<Integer>toList()));
    assertThat(list, is(empty()));
    list = Stream.of(0, 0).collect(Collectors.flatMapping(repeaterFunction, Collectors.<Integer>toList()));
    assertThat(list, is(empty()));
    list = Stream.of(2, 0, 3, -4).collect(Collectors.flatMapping(repeaterFunction, Collectors.<Integer>toList()));
    assertThat(list, contains(2, 2, 3, 3, 3));
}
Also used : ToDoubleFunction(com.annimon.stream.function.ToDoubleFunction) ToLongFunction(com.annimon.stream.function.ToLongFunction) ToIntFunction(com.annimon.stream.function.ToIntFunction) Function(com.annimon.stream.function.Function) IntSupplier(com.annimon.stream.function.IntSupplier) Test(org.junit.Test)

Example 3 with IntSupplier

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

the class ToArrayTest method testToArray.

@Test
public void testToArray() {
    assertEquals(IntStream.empty().toArray().length, 0);
    assertEquals(IntStream.of(100).toArray()[0], 100);
    assertEquals(IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).skip(4).toArray().length, 5);
    assertEquals(IntStream.generate(new IntSupplier() {

        @Override
        public int getAsInt() {
            return -1;
        }
    }).limit(14).toArray().length, 14);
    assertEquals(IntStream.of(IntStream.generate(new IntSupplier() {

        @Override
        public int getAsInt() {
            return -1;
        }
    }).limit(14).toArray()).sum(), -14);
}
Also used : IntSupplier(com.annimon.stream.function.IntSupplier) Test(org.junit.Test)

Example 4 with IntSupplier

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

the class PeekTest method testPeek.

@Test
public void testPeek() {
    assertTrue(IntStream.empty().peek(new IntConsumer() {

        @Override
        public void accept(int value) {
            throw new IllegalStateException();
        }
    }).count() == 0);
    assertTrue(IntStream.generate(new IntSupplier() {

        int value = 2;

        @Override
        public int getAsInt() {
            int v = value;
            value *= 2;
            return v;
        }
    }).peek(new IntConsumer() {

        int curValue = 1;

        @Override
        public void accept(int value) {
            if (value != curValue * 2)
                throw new IllegalStateException();
            curValue = value;
        }
    }).limit(10).count() == 10);
}
Also used : IntSupplier(com.annimon.stream.function.IntSupplier) IntConsumer(com.annimon.stream.function.IntConsumer) Test(org.junit.Test)

Example 5 with IntSupplier

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

the class GenerateTest method testStreamGenerate.

@Test
public void testStreamGenerate() {
    IntSupplier s = new IntSupplier() {

        @Override
        public int getAsInt() {
            return 42;
        }
    };
    assertTrue(IntStream.generate(s).findFirst().getAsInt() == 42);
}
Also used : IntSupplier(com.annimon.stream.function.IntSupplier) Test(org.junit.Test)

Aggregations

IntSupplier (com.annimon.stream.function.IntSupplier)5 Test (org.junit.Test)5 Function (com.annimon.stream.function.Function)1 IntConsumer (com.annimon.stream.function.IntConsumer)1 ToDoubleFunction (com.annimon.stream.function.ToDoubleFunction)1 ToIntFunction (com.annimon.stream.function.ToIntFunction)1 ToLongFunction (com.annimon.stream.function.ToLongFunction)1