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));
}
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(", "))))));
}
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(", "))))));
}
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(", "))))));
}
Aggregations