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

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

本文整理了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

/**
 * Alias for {@link Try#of(CheckedFunction0)}
 *
 * @param <T>      Component type
 * @param supplier A checked supplier
 * @return {@link Try.Success} if no exception occurs, otherwise {@link Try.Failure} if an
 * exception occurs calling {@code supplier.get()}.
 */
public static <T> Try<T> Try(CheckedFunction0<? extends T> supplier) {
  return Try.of(supplier);
}

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

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

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

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

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

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

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

/**
 * Creates a Try of a Supplier.
 *
 * @param supplier A supplier
 * @param <T>      Component type
 * @return {@code Success(supplier.get())} if no exception occurs, otherwise {@code Failure(throwable)} if an
 * exception occurs calling {@code supplier.get()}.
 */
static <T> Try<T> ofSupplier(Supplier<? extends T> supplier) {
  Objects.requireNonNull(supplier, "supplier is null");
  return of(supplier::get);
}

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

/**
 * Creates a Try of a Callable.
 *
 * @param callable A callable
 * @param <T>      Component type
 * @return {@code Success(callable.call())} if no exception occurs, otherwise {@code Failure(throwable)} if an
 * exception occurs calling {@code callable.call()}.
 */
static <T> Try<T> ofCallable(Callable<? extends T> callable) {
  Objects.requireNonNull(callable, "callable is null");
  return of(callable::call);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
 * Starts an asynchronous computation, backed by the given {@link Executor}.
 *
 * @param executor    An {@link Executor}.
 * @param computation A computation.
 * @param <T>         Type of the computation result.
 * @return A new Future instance.
 * @throws NullPointerException if one of executor or computation is null.
 */
static <T> Future<T> of(Executor executor, CheckedFunction0<? extends T> computation) {
  Objects.requireNonNull(executor, "executor is null");
  Objects.requireNonNull(computation, "computation is null");
  return FutureImpl.async(executor, complete -> complete.with(Try.of(computation)));
}

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

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

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

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

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

/**
 * Converts this to a {@link CompletableFuture}
 *
 * @return A new {@link CompletableFuture} containing the value
 */
@GwtIncompatible
default CompletableFuture<T> toCompletableFuture() {
  final CompletableFuture<T> completableFuture = new CompletableFuture<>();
  Try.of(this::get)
      .onSuccess(completableFuture::complete)
      .onFailure(completableFuture::completeExceptionally);
  return completableFuture;
}

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

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

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

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

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

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

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

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

相关文章