@Test public void fn4() { Integer[] source = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Tuple4<Object, Object, Object, Object> tuple = Tuples.fn4().apply(source); assertThat(tuple.getT1()).isEqualTo(1); assertThat(tuple.getT2()).isEqualTo(2); assertThat(tuple.getT3()).isEqualTo(3); assertThat(tuple.getT4()).isEqualTo(4); assertThat(tuple) .isInstanceOf(Tuple8.class) .containsExactly(1, 2, 3, 4, 5, 6, 7, 8); }
@Test public void fn4Delegate() { Integer[] source = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Function<Tuple4<Integer, Integer, Integer, Integer>, Tuple4> invert = t4 -> new Tuple4<>(t4.getT4(), t4.getT3(), t4.getT2(), t4.getT1()); Tuple4 tuple = Tuples.fn4(invert).apply(source); assertThat(tuple.getT1()).isEqualTo(4); assertThat(tuple.getT2()).isEqualTo(3); assertThat(tuple.getT3()).isEqualTo(2); assertThat(tuple.getT4()).isEqualTo(1); assertThat((Object) tuple).isExactlyInstanceOf(Tuple4.class); }
@Test public void whenDelayMonoJust4() { MonoProcessor<Tuple4<Integer, Integer, Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zipDelayError(Mono.just(1), Mono.just(2), Mono.just(3), Mono.just(4)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3 && v.getT4() == 4).isTrue()) .verifyComplete(); }
@Test public void whenMonoJust4() { MonoProcessor<Tuple4<Integer, Integer, Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2), Mono.just(3), Mono.just(4)) .subscribeWith(mp)) .then(() -> assertThat(mp.isError()).isFalse()) .then(() -> assertThat(mp.isSuccess()).isTrue()) .then(() -> assertThat(mp.isTerminated()).isTrue()) .assertNext(v -> assertThat(v.getT1() == 1 && v.getT2() == 2 && v.getT3() == 3 && v.getT4() == 4).isTrue()) .verifyComplete(); }
@Test public void mapT1() { Tuple4<String, Integer, Integer, Integer> base = Tuples.of("Foo", 200, 300, 400); Tuple2<?,?> mapped = base.mapT1(String::length); assertThat(mapped).isNotSameAs(base) .hasSize(4) .containsExactly(3, base.getT2(), base.getT3(), base.getT4()); }
@Test public void mapT4() { Tuple4<Integer, Integer, Integer, String> base = Tuples.of(100, 200, 300, "Foo"); Tuple2<?,?> mapped = base.mapT4(String::length); assertThat(mapped).isNotSameAs(base) .hasSize(4) .containsExactly(base.getT1(), base.getT2(), base.getT3(), 3); }
@Test public void mapT3() { Tuple4<Integer, Integer, String, Integer> base = Tuples.of(100, 200, "Foo", 400); Tuple2<?,?> mapped = base.mapT3(String::length); assertThat(mapped).isNotSameAs(base) .hasSize(4) .containsExactly(base.getT1(), base.getT2(), 3, base.getT4()); }
/** * Returns a {@link Function} of {@link Tuple4} that wraps a function of the component values of the tuple * * @param function the component value function * @param <T1> the type of the first value * @param <T2> the type of the second value * @param <T3> the type of the third value * @param <T4> the type of the fourth value * @param <R> the type of the result of the function * @return the wrapper function */ public static <T1, T2, T3, T4, R> Function<Tuple4<T1, T2, T3, T4>, R> function(Function4<T1, T2, T3, T4, R> function) { return tuple -> function.apply(tuple.getT1(), tuple.getT2(), tuple.getT3(), tuple.getT4()); }
/** * Returns a {@link Predicate} of {@link Tuple4} that wraps a predicate of the component values of the tuple * * @param predicate the component value predicate * @param <T1> the type of the first value * @param <T2> the type of the second value * @param <T3> the type of the third value * @param <T4> the type of the fourth value * @return the wrapper predicate */ public static <T1, T2, T3, T4> Predicate<Tuple4<T1, T2, T3, T4>> predicate(Predicate4<T1, T2, T3, T4> predicate) { return tuple -> predicate.test(tuple.getT1(), tuple.getT2(), tuple.getT3(), tuple.getT4()); }
/** * Returns a {@link Consumer} of {@link Tuple4} that wraps a consumer of the component values of the tuple * * @param consumer the component value consumer * @param <T1> the type of the first value * @param <T2> the type of the second value * @param <T3> the type of the third value * @param <T4> the type of the fourth value * @return the wrapper consumer */ public static <T1, T2, T3, T4> Consumer<Tuple4<T1, T2, T3, T4>> consumer(Consumer4<T1, T2, T3, T4> consumer) { return tuple -> consumer.accept(tuple.getT1(), tuple.getT2(), tuple.getT3(), tuple.getT4()); }