Search in sources :

Example 1 with Function

use of com.annimon.stream.function.Function 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 2 with Function

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

the class DoubleStreamMatcherTest method testElements.

@Test
public void testElements() {
    final DoubleStream stream = DoubleStream.of(-0.987, 1.234, Math.PI, 1.618);
    final Double[] expected = new Double[] { -0.987, 1.234, Math.PI, 1.618 };
    final Matcher<DoubleStream> matcher = elements(arrayContaining(expected));
    assertThat(stream, matcher);
    assertTrue(matcher.matches(stream));
    assertFalse(elements(arrayContaining(expected)).matches(DoubleStream.empty()));
    assertThat(matcher, description(allOf(containsString("DoubleStream elements"), containsString(Stream.of(expected).map(new Function<Double, String>() {

        @Override
        public String apply(Double t) {
            return String.format("<%s>", t.toString());
        }
    }).collect(Collectors.joining(", "))))));
}
Also used : Function(com.annimon.stream.function.Function) DoubleStream(com.annimon.stream.DoubleStream) Test(org.junit.Test)

Example 3 with Function

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

the class LongStreamMatcherTest method testElements.

@Test
public void testElements() {
    final LongStream stream = LongStream.of(-813, 123456, Integer.MAX_VALUE);
    final Long[] expected = new Long[] { -813L, 123456L, (long) Integer.MAX_VALUE };
    final Matcher<LongStream> matcher = elements(arrayContaining(expected));
    assertThat(stream, matcher);
    assertTrue(matcher.matches(stream));
    assertFalse(elements(arrayContaining(expected)).matches(LongStream.empty()));
    assertThat(matcher, description(allOf(containsString("LongStream elements"), containsString(Stream.of(expected).map(new Function<Long, String>() {

        @Override
        public String apply(Long t) {
            return String.format("<%sL>", t.toString());
        }
    }).collect(Collectors.joining(", "))))));
}
Also used : Function(com.annimon.stream.function.Function) LongStream(com.annimon.stream.LongStream) Test(org.junit.Test)

Example 4 with Function

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

the class IntStreamMatcherTest method testElements.

@Test
public void testElements() {
    final IntStream stream = IntStream.of(-813, 123456, Short.MAX_VALUE);
    final Integer[] expected = new Integer[] { -813, 123456, (int) Short.MAX_VALUE };
    final Matcher<IntStream> matcher = elements(arrayContaining(expected));
    assertThat(stream, matcher);
    assertTrue(matcher.matches(stream));
    assertFalse(elements(arrayContaining(expected)).matches(IntStream.empty()));
    assertThat(matcher, description(allOf(containsString("IntStream elements"), containsString(Stream.of(expected).map(new Function<Integer, String>() {

        @Override
        public String apply(Integer t) {
            return String.format("<%s>", t.toString());
        }
    }).collect(Collectors.joining(", "))))));
}
Also used : Function(com.annimon.stream.function.Function) IntStream(com.annimon.stream.IntStream) Test(org.junit.Test)

Aggregations

Function (com.annimon.stream.function.Function)4 Test (org.junit.Test)4 DoubleStream (com.annimon.stream.DoubleStream)1 IntStream (com.annimon.stream.IntStream)1 LongStream (com.annimon.stream.LongStream)1 IntSupplier (com.annimon.stream.function.IntSupplier)1 ToDoubleFunction (com.annimon.stream.function.ToDoubleFunction)1 ToIntFunction (com.annimon.stream.function.ToIntFunction)1 ToLongFunction (com.annimon.stream.function.ToLongFunction)1