/** * 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); }
/** * 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); }
/** * 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); }
/** * 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)); }
/** * 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); }
/** * 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); }
/** * 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)); }
/** * 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)); }
/** * 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(); }
/** * 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(); }
/** * 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(); }
/** * 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)); }
/** * 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))); }
/** * 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(); }
/** * 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(); }
/** * 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; }
private V computeAndPut(K cacheKey, CheckedFunction0<V> supplier) { return Try.of(supplier) .andThen(value -> putValueIntoCache(cacheKey, value)) .get(); }
@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); }
@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); }
@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); }