@Override public String getMessage() { //skip the "error has been observed" traceback if mapped traceback is empty synchronized (chainOrder) { if (chainOrder.isEmpty()) { return super.getMessage(); } StringBuilder sb = new StringBuilder(super.getMessage()).append( "Error has been observed by the following operator(s):\n"); for(Tuple3<Integer, String, Integer> t : chainOrder) { //noinspection ConstantConditions mapLine(t.getT3(), sb, t.getT2()); } return sb.toString(); } } }
/** * Combine query params and form data for multipart form data from the body * of the request into a {@code Map<String, Object>} of values to use for * data binding purposes. * @param exchange the current exchange * @return a {@code Mono} with the values to bind * @see org.springframework.http.server.reactive.ServerHttpRequest#getQueryParams() * @see ServerWebExchange#getFormData() * @see ServerWebExchange#getMultipartData() */ public static Mono<Map<String, Object>> extractValuesToBind(ServerWebExchange exchange) { MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams(); Mono<MultiValueMap<String, String>> formData = exchange.getFormData(); Mono<MultiValueMap<String, Part>> multipartData = exchange.getMultipartData(); return Mono.zip(Mono.just(queryParams), formData, multipartData) .map(tuple -> { Map<String, Object> result = new TreeMap<>(); tuple.getT1().forEach((key, values) -> addBindValue(result, key, values)); tuple.getT2().forEach((key, values) -> addBindValue(result, key, values)); tuple.getT3().forEach((key, values) -> addBindValue(result, key, values)); return result; }); }
@Test public void fn3() { Integer[] source = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Tuple3<Object, Object, Object> tuple = Tuples.fn3().apply(source); assertThat(tuple.getT1()).isEqualTo(1); assertThat(tuple.getT2()).isEqualTo(2); assertThat(tuple.getT3()).isEqualTo(3); assertThat(tuple) .isInstanceOf(Tuple8.class) .containsExactly(1, 2, 3, 4, 5, 6, 7, 8); }
@Test public void fn3Delegate() { Integer[] source = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 }; Function<Tuple3<Integer, Integer, Integer>, Tuple3> invert = t3 -> new Tuple3<>(t3.getT3(), t3.getT2(), t3.getT1()); Tuple3 tuple = Tuples.fn3(invert).apply(source); assertThat(tuple.getT1()).isEqualTo(3); assertThat(tuple.getT2()).isEqualTo(2); assertThat(tuple.getT3()).isEqualTo(1); assertThat((Object) tuple).isExactlyInstanceOf(Tuple3.class); }
Mono<Request> createDefaultedRequest(String clientRegistrationId, Authentication authentication, ServerWebExchange exchange) { Mono<Authentication> defaultedAuthentication = Mono.justOrEmpty(authentication) .switchIfEmpty(currentAuthentication()); Mono<String> defaultedRegistrationId = Mono.justOrEmpty(clientRegistrationId) .switchIfEmpty(Mono.justOrEmpty(this.defaultClientRegistrationId)) .switchIfEmpty(clientRegistrationId(defaultedAuthentication)); Mono<Optional<ServerWebExchange>> defaultedExchange = Mono.justOrEmpty(exchange) .switchIfEmpty(currentServerWebExchange()).map(Optional::of) .defaultIfEmpty(Optional.empty()); return Mono.zip(defaultedRegistrationId, defaultedAuthentication, defaultedExchange) .map(t3 -> new Request(t3.getT1(), t3.getT2(), t3.getT3().orElse(null))); }
Mono<Request> createDefaultedRequest(String clientRegistrationId, Authentication authentication, ServerWebExchange exchange) { Mono<Authentication> defaultedAuthentication = Mono.justOrEmpty(authentication) .switchIfEmpty(currentAuthentication()); Mono<String> defaultedRegistrationId = Mono.justOrEmpty(clientRegistrationId) .switchIfEmpty(Mono.justOrEmpty(this.defaultClientRegistrationId)) .switchIfEmpty(clientRegistrationId(defaultedAuthentication)) .switchIfEmpty(Mono.error(() -> new IllegalArgumentException("The clientRegistrationId could not be resolved. Please provide one"))); Mono<Optional<ServerWebExchange>> defaultedExchange = Mono.justOrEmpty(exchange) .switchIfEmpty(currentServerWebExchange()).map(Optional::of) .defaultIfEmpty(Optional.empty()); return Mono.zip(defaultedRegistrationId, defaultedAuthentication, defaultedExchange) .map(t3 -> new Request(t3.getT1(), t3.getT2(), t3.getT3().orElse(null))); }
/** * Combine query params and form data for multipart form data from the body * of the request into a {@code Map<String, Object>} of values to use for * data binding purposes. * @param exchange the current exchange * @return a {@code Mono} with the values to bind * @see org.springframework.http.server.reactive.ServerHttpRequest#getQueryParams() * @see ServerWebExchange#getFormData() * @see ServerWebExchange#getMultipartData() */ public static Mono<Map<String, Object>> extractValuesToBind(ServerWebExchange exchange) { MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams(); Mono<MultiValueMap<String, String>> formData = exchange.getFormData(); Mono<MultiValueMap<String, Part>> multipartData = exchange.getMultipartData(); return Mono.zip(Mono.just(queryParams), formData, multipartData) .map(tuple -> { Map<String, Object> result = new TreeMap<>(); tuple.getT1().forEach((key, values) -> addBindValue(result, key, values)); tuple.getT2().forEach((key, values) -> addBindValue(result, key, values)); tuple.getT3().forEach((key, values) -> addBindValue(result, key, values)); return result; }); }
@Test public void whenMonoJust3() { MonoProcessor<Tuple3<Integer, Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zip(Mono.just(1), Mono.just(2), Mono.just(3)) .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).isTrue()) .verifyComplete(); }
@Test public void whenDelayJustMono3() { MonoProcessor<Tuple3<Integer, Integer, Integer>> mp = MonoProcessor.create(); StepVerifier.create(Mono.zipDelayError(Mono.just(1), Mono.just(2), Mono.just(3)) .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).isTrue()) .verifyComplete(); }
@Test public void tupleProvidesTypeSafeMethods() { Tuple3<String, Long, Integer> t3 = Tuples.of("string", 1L, 10); assertThat(t3.getT1()).as("first value is a string").isInstanceOf(String.class); assertThat(t3.getT2()).as("second value is a long").isInstanceOf(Long.class); assertThat(t3.getT3()).as("third value is an int").isInstanceOf(Integer.class); }
@Test public void mapT3() { Tuple3<Integer, Integer, String> base = Tuples.of(100, 200, "Foo"); Tuple2<?,?> mapped = base.mapT3(String::length); assertThat(mapped).isNotSameAs(base) .hasSize(3) .containsExactly(base.getT1(), base.getT2(), 3); }
@Test public void mapT1() { Tuple3<String, Integer, Integer> base = Tuples.of("Foo", 200, 300); Tuple2<?,?> mapped = base.mapT1(String::length); assertThat(mapped).isNotSameAs(base) .hasSize(3) .containsExactly(3, base.getT2(), base.getT3()); }
assertThat(finalizeStats).areAtLeast(5, hasFinalized); assertThat(finalizeStats).allMatch(t3 -> t3.getT2() <= 3, "max 3 windows in flight");
@Test public void nonPairWisePairWise() { Flux<Tuple2<Tuple3<Integer, String, String>, String>> f = Flux.zip(Flux.just(1), Flux.just("test"), Flux.just("test0")) .zipWith(Flux.just("test2")); Assert.assertTrue(f instanceof FluxZip); FluxZip<?, ?> s = (FluxZip<?, ?>) f; Assert.assertTrue(s.sources != null); Assert.assertTrue(s.sources.length == 2); Flux<Tuple2<Integer, String>> ff = f.map(t -> Tuples.of(t.getT1() .getT1(), t.getT1() .getT2() + t.getT2())); ff.subscribeWith(AssertSubscriber.create()) .assertValues(Tuples.of(1, "testtest2")) .assertComplete(); }
public U resetPassword(Tuple3<U, JWTClaimsSet, String> tuple) { log.debug("Resetting password ..."); U user = tuple.getT1(); JWTClaimsSet claims = tuple.getT2(); String newPassword = tuple.getT3(); LerUtils.ensureCredentialsUpToDate(claims, user); // sets the password user.setPassword(passwordEncoder.encode(newPassword)); user.setCredentialsUpdatedMillis(System.currentTimeMillis()); log.debug("Password reset."); return user; }
/** * Returns a {@link Consumer} of {@link Tuple3} 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 * @return the wrapper consumer */ public static <T1, T2, T3> Consumer<Tuple3<T1, T2, T3>> consumer(Consumer3<T1, T2, T3> consumer) { return tuple -> consumer.accept(tuple.getT1(), tuple.getT2(), tuple.getT3()); }
/** * Returns a {@link Predicate} of {@link Tuple3} 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 * @return the wrapper predicate */ public static <T1, T2, T3> Predicate<Tuple3<T1, T2, T3>> predicate(Predicate3<T1, T2, T3> predicate) { return tuple -> predicate.test(tuple.getT1(), tuple.getT2(), tuple.getT3()); }
public Mono<UserDto> updateUser(ID userId, Mono<String> patch) { return Mono.zip(findUserById(userId), LecrUtils.currentUser(), patch) .doOnNext(this::ensureEditable) .map((Tuple3<U, Optional<UserDto>, String> tuple3) -> this.updateUser(tuple3.getT1(), tuple3.getT2(), tuple3.getT3())) .flatMap(userRepository::save) .map(user -> { UserDto userDto = user.toUserDto(); userDto.setPassword(null); return userDto; }); }
public Flux<Tuple2<IType, Double>> fuzzySearchTypes(String searchTerm, Predicate<IType> typeFilter) { return fuzzySearchTypes(searchTerm) .map(match -> Tuples.of(createType(Tuples.of(match.getT1(), match.getT2())), match.getT3())) .filter(t -> typeFilter == null || typeFilter.test(t.getT1())); }
public static void main(String[] args) { Publisher<Integer> source = Px.just(1).hide(); Flux.zip(source, source, source).doOnNext(v -> System.out.println(v.getT1() + ", " + v.getT2() + ", " + v.getT3())).subscribe(); } }