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

x33g5p2x  于2022-01-26 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(393)

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

Try.of介绍

[英]Creates a Try of a CheckedFunction0.
[中]创建CheckedFunction0的一次尝试。

代码示例

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

  1. /**
  2. * Alias for {@link Try#of(CheckedFunction0)}
  3. *
  4. * @param <T> Component type
  5. * @param supplier A checked supplier
  6. * @return {@link Try.Success} if no exception occurs, otherwise {@link Try.Failure} if an
  7. * exception occurs calling {@code supplier.get()}.
  8. */
  9. public static <T> Try<T> Try(CheckedFunction0<? extends T> supplier) {
  10. return Try.of(supplier);
  11. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)}
  7. * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise.
  8. */
  9. static <R> Function0<Try<R>> liftTry(Supplier<? extends R> partialFunction) {
  10. return () -> Try.of(partialFunction::get);
  11. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)}
  7. * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise.
  8. */
  9. static <R> Function0<Try<R>> liftTry(CheckedFunction0<? extends R> partialFunction) {
  10. return () -> Try.of(partialFunction::apply);
  11. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @param <T1> 1st argument
  7. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)}
  8. * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise.
  9. */
  10. static <T1, R> Function1<T1, Try<R>> liftTry(Function<? super T1, ? extends R> partialFunction) {
  11. return t1 -> Try.of(() -> partialFunction.apply(t1));
  12. }

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

  1. /**
  2. * Creates a Try of a Supplier.
  3. *
  4. * @param supplier A supplier
  5. * @param <T> Component type
  6. * @return {@code Success(supplier.get())} if no exception occurs, otherwise {@code Failure(throwable)} if an
  7. * exception occurs calling {@code supplier.get()}.
  8. */
  9. static <T> Try<T> ofSupplier(Supplier<? extends T> supplier) {
  10. Objects.requireNonNull(supplier, "supplier is null");
  11. return of(supplier::get);
  12. }

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

  1. /**
  2. * Creates a Try of a Callable.
  3. *
  4. * @param callable A callable
  5. * @param <T> Component type
  6. * @return {@code Success(callable.call())} if no exception occurs, otherwise {@code Failure(throwable)} if an
  7. * exception occurs calling {@code callable.call()}.
  8. */
  9. static <T> Try<T> ofCallable(Callable<? extends T> callable) {
  10. Objects.requireNonNull(callable, "callable is null");
  11. return of(callable::call);
  12. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @param <T1> 1st argument
  7. * @param <T2> 2nd argument
  8. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)}
  9. * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise.
  10. */
  11. static <T1, T2, R> Function2<T1, T2, Try<R>> liftTry(BiFunction<? super T1, ? super T2, ? extends R> partialFunction) {
  12. return (t1, t2) -> Try.of(() -> partialFunction.apply(t1, t2));
  13. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @param <T1> 1st argument
  7. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)}
  8. * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise.
  9. */
  10. static <T1, R> Function1<T1, Try<R>> liftTry(CheckedFunction1<? super T1, ? extends R> partialFunction) {
  11. return t1 -> Try.of(() -> partialFunction.apply(t1));
  12. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)}
  7. * if the function is defined for the given arguments, and {@code None} otherwise.
  8. */
  9. @SuppressWarnings("RedundantTypeArguments")
  10. static <R> Function0<Option<R>> lift(CheckedFunction0<? extends R> partialFunction) {
  11. return () -> Try.<R>of(partialFunction::apply).toOption();
  12. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)}
  7. * if the function is defined for the given arguments, and {@code None} otherwise.
  8. */
  9. @SuppressWarnings("RedundantTypeArguments")
  10. static <R> Function0<Option<R>> lift(Supplier<? extends R> partialFunction) {
  11. return () -> Try.<R>of(partialFunction::get).toOption();
  12. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @param <T1> 1st argument
  7. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)}
  8. * if the function is defined for the given arguments, and {@code None} otherwise.
  9. */
  10. @SuppressWarnings("RedundantTypeArguments")
  11. static <T1, R> Function1<T1, Option<R>> lift(Function<? super T1, ? extends R> partialFunction) {
  12. return t1 -> Try.<R>of(() -> partialFunction.apply(t1)).toOption();
  13. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @param <T1> 1st argument
  7. * @param <T2> 2nd argument
  8. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)}
  9. * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise.
  10. */
  11. static <T1, T2, R> Function2<T1, T2, Try<R>> liftTry(CheckedFunction2<? super T1, ? super T2, ? extends R> partialFunction) {
  12. return (t1, t2) -> Try.of(() -> partialFunction.apply(t1, t2));
  13. }

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

  1. /**
  2. * Starts an asynchronous computation, backed by the given {@link Executor}.
  3. *
  4. * @param executor An {@link Executor}.
  5. * @param computation A computation.
  6. * @param <T> Type of the computation result.
  7. * @return A new Future instance.
  8. * @throws NullPointerException if one of executor or computation is null.
  9. */
  10. static <T> Future<T> of(Executor executor, CheckedFunction0<? extends T> computation) {
  11. Objects.requireNonNull(executor, "executor is null");
  12. Objects.requireNonNull(computation, "computation is null");
  13. return FutureImpl.async(executor, complete -> complete.with(Try.of(computation)));
  14. }

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

  1. /**
  2. * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result.
  3. *
  4. * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing)
  5. * @param <R> return type
  6. * @param <T1> 1st argument
  7. * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)}
  8. * if the function is defined for the given arguments, and {@code None} otherwise.
  9. */
  10. @SuppressWarnings("RedundantTypeArguments")
  11. static <T1, R> Function1<T1, Option<R>> lift(CheckedFunction1<? super T1, ? extends R> partialFunction) {
  12. return t1 -> Try.<R>of(() -> partialFunction.apply(t1)).toOption();
  13. }

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

  1. /**
  2. * Returns the underlying value if present, otherwise returns the result of {@code Try.of(supplier).get()}.
  3. *
  4. * @param supplier An alternative value supplier.
  5. * @return A value of type {@code T}.
  6. * @throws NullPointerException if supplier is null
  7. */
  8. default T getOrElseTry(CheckedFunction0<? extends T> supplier) {
  9. Objects.requireNonNull(supplier, "supplier is null");
  10. return isEmpty() ? Try.of(supplier).get() : get();
  11. }

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

  1. /**
  2. * Converts this to a {@link CompletableFuture}
  3. *
  4. * @return A new {@link CompletableFuture} containing the value
  5. */
  6. @GwtIncompatible
  7. default CompletableFuture<T> toCompletableFuture() {
  8. final CompletableFuture<T> completableFuture = new CompletableFuture<>();
  9. Try.of(this::get)
  10. .onSuccess(completableFuture::complete)
  11. .onFailure(completableFuture::completeExceptionally);
  12. return completableFuture;
  13. }

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

  1. private V computeAndPut(K cacheKey, CheckedFunction0<V> supplier) {
  2. return Try.of(supplier)
  3. .andThen(value -> putValueIntoCache(cacheKey, value))
  4. .get();
  5. }

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

  1. @Override
  2. public Response<T> execute() throws IOException {
  3. CheckedFunction0<Response<T>> restrictedSupplier = RateLimiter.decorateCheckedSupplier(rateLimiter, call::execute);
  4. final Try<Response<T>> response = Try.of(restrictedSupplier);
  5. return response.isSuccess() ? response.get() : handleFailure(response);
  6. }

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

  1. @Test
  2. public void shouldDecorateFunctionAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithName("Tom")).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. Function<String, String> function = Bulkhead.decorateFunction(bulkhead, helloWorldService::returnHelloWorldWithName);
  8. Try<String> result = Try.of(() -> function.apply("Tom"));
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. }

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

  1. @Test
  2. public void shouldDecorateCheckedFunctionAndReturnWithException() throws Throwable {
  3. // Given
  4. Bulkhead bulkhead = Bulkhead.of("test", config);
  5. BDDMockito.given(helloWorldService.returnHelloWorldWithNameWithException("Tom")).willThrow(new RuntimeException("BAM!"));
  6. // When
  7. CheckedFunction1<String, String> function = Bulkhead.decorateCheckedFunction(bulkhead, helloWorldService::returnHelloWorldWithNameWithException);
  8. Try<String> result = Try.of(() -> function.apply("Tom"));
  9. // Then
  10. assertThat(result.isFailure()).isTrue();
  11. assertThat(result.failed().get()).isInstanceOf(RuntimeException.class);
  12. assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1);
  13. }

相关文章