io.vavr.control.Try.map()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(204)

本文整理了Java中io.vavr.control.Try.map()方法的一些代码示例,展示了Try.map()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Try.map()方法的具体详情如下:
包路径:io.vavr.control.Try
类名称:Try
方法名:map

Try.map介绍

[英]Shortcut for mapTry(mapper::apply), see #mapTry(CheckedFunction1).
[中]mapTry的快捷方式(mapper::apply),请参见#mapTry(CheckedFunction1)。

代码示例

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Try.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function<? super T1, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return ts1.map(f);
  11. }

代码示例来源:origin: vavr-io/vavr

  1. @Override
  2. default <U> Future<U> map(Function<? super T, ? extends U> mapper) {
  3. Objects.requireNonNull(mapper, "mapper is null");
  4. return transformValue(t -> t.map(mapper));
  5. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Trys.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(BiFunction<? super T1, ? super T2, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return
  11. ts1.flatMap(t1 ->
  12. ts2.map(t2 -> f.apply(t1, t2)));
  13. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Collects value that is in the domain of the given {@code partialFunction} by mapping the value to type {@code R}.
  3. *
  4. * <pre>{@code
  5. * partialFunction.isDefinedAt(value)
  6. * }</pre>
  7. *
  8. * If the element makes it through that filter, the mapped instance is wrapped in {@code Try}
  9. *
  10. * <pre>{@code
  11. * R newValue = partialFunction.apply(value)
  12. * }</pre>
  13. *
  14. *
  15. * @param partialFunction A function that is not necessarily defined on value of this try.
  16. * @param <R> The new value type
  17. * @return A new {@code Try} instance containing value of type {@code R}
  18. * @throws NullPointerException if {@code partialFunction} is null
  19. */
  20. @SuppressWarnings("unchecked")
  21. default <R> Try<R> collect(PartialFunction<? super T, ? extends R> partialFunction){
  22. Objects.requireNonNull(partialFunction, "partialFunction is null");
  23. return filter(partialFunction::isDefinedAt).map(partialFunction::apply);
  24. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Trys.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function3<? super T1, ? super T2, ? super T3, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return
  11. ts1.flatMap(t1 ->
  12. ts2.flatMap(t2 ->
  13. ts3.map(t3 -> f.apply(t1, t2, t3))));
  14. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Trys.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return
  11. ts1.flatMap(t1 ->
  12. ts2.flatMap(t2 ->
  13. ts3.flatMap(t3 ->
  14. ts4.map(t4 -> f.apply(t1, t2, t3, t4)))));
  15. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Trys.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function5<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return
  11. ts1.flatMap(t1 ->
  12. ts2.flatMap(t2 ->
  13. ts3.flatMap(t3 ->
  14. ts4.flatMap(t4 ->
  15. ts5.map(t5 -> f.apply(t1, t2, t3, t4, t5))))));
  16. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Trys.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function6<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return
  11. ts1.flatMap(t1 ->
  12. ts2.flatMap(t2 ->
  13. ts3.flatMap(t3 ->
  14. ts4.flatMap(t4 ->
  15. ts5.flatMap(t5 ->
  16. ts6.map(t6 -> f.apply(t1, t2, t3, t4, t5, t6)))))));
  17. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Returns a this and that Future result combined using a given combinator function.
  3. * <p>
  4. * If this Future failed the result contains this failure. Otherwise the result contains that failure or
  5. * a combination of both successful Future results.
  6. *
  7. * @param that Another Future
  8. * @param combinator The combinator function
  9. * @param <U> Result type of {@code that}
  10. * @param <R> Result type of {@code f}
  11. * @return A new Future that returns both Future results.
  12. * @throws NullPointerException if {@code that} is null
  13. */
  14. @SuppressWarnings({"deprecation", "unchecked"})
  15. default <U, R> Future<R> zipWith(Future<? extends U> that, BiFunction<? super T, ? super U, ? extends R> combinator) {
  16. Objects.requireNonNull(that, "that is null");
  17. Objects.requireNonNull(combinator, "combinator is null");
  18. return run(executor(), complete ->
  19. onComplete(res1 -> {
  20. if (res1.isFailure()) {
  21. complete.with((Try.Failure<R>) res1);
  22. } else {
  23. that.onComplete(res2 -> {
  24. final Try<R> result = res1.flatMap(t -> res2.map(u -> combinator.apply(t, u)));
  25. complete.with(result);
  26. });
  27. }
  28. })
  29. );
  30. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Trys.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function7<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return
  11. ts1.flatMap(t1 ->
  12. ts2.flatMap(t2 ->
  13. ts3.flatMap(t3 ->
  14. ts4.flatMap(t4 ->
  15. ts5.flatMap(t5 ->
  16. ts6.flatMap(t6 ->
  17. ts7.map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7))))))));
  18. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateFunction() throws Exception {
  3. Function<Integer, String> function = mock(Function.class);
  4. Function<Integer, String> decorated = RateLimiter.decorateFunction(limit, function);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try<String> decoratedFunctionResult = Try.success(1).map(decorated);
  8. then(decoratedFunctionResult.isFailure()).isTrue();
  9. then(decoratedFunctionResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(function, never()).apply(any());
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondFunctionResult = Try.success(1).map(decorated);
  14. then(secondFunctionResult.isSuccess()).isTrue();
  15. verify(function, times(1)).apply(1);
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void decorateSupplier() throws Exception {
  3. Supplier supplier = mock(Supplier.class);
  4. Supplier decorated = RateLimiter.decorateSupplier(limit, supplier);
  5. when(limit.getPermission(config.getTimeoutDuration()))
  6. .thenReturn(false);
  7. Try decoratedSupplierResult = Try.success(decorated).map(Supplier::get);
  8. then(decoratedSupplierResult.isFailure()).isTrue();
  9. then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class);
  10. verify(supplier, never()).get();
  11. when(limit.getPermission(config.getTimeoutDuration()))
  12. .thenReturn(true);
  13. Try secondSupplierResult = Try.success(decorated).map(Supplier::get);
  14. then(secondSupplierResult.isSuccess()).isTrue();
  15. verify(supplier, times(1)).get();
  16. }

代码示例来源:origin: vavr-io/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Trys.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function8<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return
  11. ts1.flatMap(t1 ->
  12. ts2.flatMap(t2 ->
  13. ts3.flatMap(t3 ->
  14. ts4.flatMap(t4 ->
  15. ts5.flatMap(t5 ->
  16. ts6.flatMap(t6 ->
  17. ts7.flatMap(t7 ->
  18. ts8.map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8)))))))));
  19. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldInvokeMap() {
  3. // tag::shouldInvokeMap[]
  4. // Given
  5. CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("testName");
  6. // When I decorate my function
  7. CheckedFunction0<String> decoratedSupplier = CircuitBreaker
  8. .decorateCheckedSupplier(circuitBreaker, () -> "This can be any method which returns: 'Hello");
  9. // and chain an other function with map
  10. Try<String> result = Try.of(decoratedSupplier)
  11. .map(value -> value + " world'");
  12. // Then the Try Monad returns a Success<String>, if all functions ran successfully.
  13. assertThat(result.isSuccess()).isTrue();
  14. assertThat(result.get()).isEqualTo("This can be any method which returns: 'Hello world'");
  15. // end::shouldInvokeMap[]
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldInvokeMap() {
  3. // tag::shouldInvokeMap[]
  4. // Given
  5. Bulkhead bulkhead = Bulkhead.of("testName", config);
  6. // When I decorate my function
  7. CheckedFunction0<String> decoratedSupplier = Bulkhead.decorateCheckedSupplier(bulkhead, () -> "This can be any method which returns: 'Hello");
  8. // and chain an other function with map
  9. Try<String> result = Try.of(decoratedSupplier)
  10. .map(value -> value + " world'");
  11. // Then the Try Monad returns a Success<String>, if all functions ran successfully.
  12. assertThat(result.isSuccess()).isTrue();
  13. assertThat(result.get()).isEqualTo("This can be any method which returns: 'Hello world'");
  14. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  15. // end::shouldInvokeMap[]
  16. }

代码示例来源:origin: resilience4j/resilience4j

  1. @Test
  2. public void shouldThrowCircuitBreakerOpenException() {
  3. // tag::shouldThrowCircuitBreakerOpenException[]
  4. // Given
  5. CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
  6. .ringBufferSizeInClosedState(2)
  7. .waitDurationInOpenState(Duration.ofMillis(1000))
  8. .build();
  9. CircuitBreaker circuitBreaker = CircuitBreaker.of("testName", circuitBreakerConfig);
  10. // Simulate a failure attempt
  11. circuitBreaker.onError(0, new RuntimeException());
  12. // CircuitBreaker is still CLOSED, because 1 failure is allowed
  13. assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.CLOSED);
  14. // Simulate a failure attempt
  15. circuitBreaker.onError(0, new RuntimeException());
  16. // CircuitBreaker is OPEN, because the failure rate is above 50%
  17. assertThat(circuitBreaker.getState()).isEqualTo(CircuitBreaker.State.OPEN);
  18. // When I decorate my function and invoke the decorated function
  19. Try<String> result = Try.of(CircuitBreaker.decorateCheckedSupplier(circuitBreaker, () -> "Hello"))
  20. .map(value -> value + " world");
  21. // Then the call fails, because CircuitBreaker is OPEN
  22. assertThat(result.isFailure()).isTrue();
  23. // Exception is CircuitBreakerOpenException
  24. assertThat(result.failed().get()).isInstanceOf(CircuitBreakerOpenException.class);
  25. // end::shouldThrowCircuitBreakerOpenException[]
  26. CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
  27. assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(2);
  28. assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(2);
  29. }

代码示例来源:origin: io.vavr/vavr

  1. /**
  2. * Yields a result for elements of the cross product of the underlying Try.
  3. *
  4. * @param f a function that maps an element of the cross product to a result
  5. * @param <R> type of the resulting {@code Try} elements
  6. * @return an {@code Try} of mapped results
  7. */
  8. public <R> Try<R> yield(Function<? super T1, ? extends R> f) {
  9. Objects.requireNonNull(f, "f is null");
  10. return ts1.map(f);
  11. }

代码示例来源:origin: io.vavr/vavr

  1. @Override
  2. default <U> Future<U> map(Function<? super T, ? extends U> mapper) {
  3. Objects.requireNonNull(mapper, "mapper is null");
  4. return transformValue(t -> t.map(mapper));
  5. }

代码示例来源:origin: Tristan971/Lyrebird

  1. public Try<Twitter> getCurrentTwitter() {
  2. return Try.of(() -> currentSession)
  3. .map(Property::getValue)
  4. .map(Session::getTwitterHandler)
  5. .map(TwitterHandler::getTwitter)
  6. .andThenTry(session -> LOG.trace(
  7. "Preparing request for user : {}",
  8. session.getScreenName()
  9. ));
  10. }

代码示例来源:origin: RoboZonky/robozonky

  1. private static <O> Either<Throwable, O> execute(final Supplier<O> operation) {
  2. LOGGER.trace("Will execute {}.", operation);
  3. return Try.ofSupplier(operation).map(Either::<Throwable, O>right)
  4. .recover(t -> {
  5. LOGGER.debug("Operation failed.", t);
  6. return Either.left(t);
  7. }).get();
  8. }

相关文章