/** * Returns a tupled version of this function. * * @return a tupled function equivalent to this. */ default CheckedFunction1<Tuple0, R> tupled() { return t -> apply(); }
public T get() throws Throwable { return supplier.apply(); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes seven {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction7<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply(); T6 t6 = t6Supplier.apply(); T7 t7 = t7Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5, t6, t7); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes eight {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try"/* https://bugs.openjdk.java.net/browse/JDK-8155591 */) public <R> Try<R> of(CheckedFunction8<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply(); T6 t6 = t6Supplier.apply(); T7 t7 = t7Supplier.apply(); T8 t8 = t8Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5, t6, t7, t8); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes six {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction6<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply(); T6 t6 = t6Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5, t6); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes five {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction5<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply(); T5 t5 = t5Supplier.apply()) { return f.apply(t1, t2, t3, t4, t5); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes four {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply(); T4 t4 = t4Supplier.apply()) { return f.apply(t1, t2, t3, t4); } }); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes three {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction3<? super T1, ? super T2, ? super T3, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply(); T3 t3 = t3Supplier.apply()) { return f.apply(t1, t2, t3); } }); } }
/** * Returns a composed function that first applies this CheckedFunction0 to the given argument and then applies * {@linkplain CheckedFunction1} {@code after} to the result. * * @param <V> return type of after * @param after the function applied after this * @return a function composed of this and after * @throws NullPointerException if after is null */ default <V> CheckedFunction0<V> andThen(CheckedFunction1<? super R, ? extends V> after) { Objects.requireNonNull(after, "after is null"); return () -> after.apply(apply()); }
/** * Return a composed function that first applies this CheckedFunction0 to the given arguments and in case of throwable * try to get value from {@code recover} function with same arguments and throwable information. * * @param recover the function applied in case of throwable * @return a function composed of this and recover * @throws NullPointerException if recover is null */ default Function0<R> recover(Function<? super Throwable, ? extends Supplier<? extends R>> recover) { Objects.requireNonNull(recover, "recover is null"); return () -> { try { return this.apply(); } catch (Throwable throwable) { final Supplier<? extends R> func = recover.apply(throwable); Objects.requireNonNull(func, () -> "recover return null for " + throwable.getClass() + ": " + throwable.getMessage()); return func.get(); } }; }
/** * Returns an unchecked function that will <em>sneaky throw</em> if an exceptions occurs when applying the function. * * @return a new Function0 that throws a {@code Throwable}. */ default Function0<R> unchecked() { return () -> { try { return apply(); } catch(Throwable t) { return sneakyThrow(t); } }; }
/** * Creates a supplier which is restricted by a RateLimiter. * * @param rateLimiter the RateLimiter * @param supplier the original supplier * @param <T> the type of results supplied supplier * @return a supplier which is restricted by a RateLimiter. */ static <T> CheckedFunction0<T> decorateCheckedSupplier(RateLimiter rateLimiter, CheckedFunction0<T> supplier) { return () -> { waitForPermission(rateLimiter); return supplier.apply(); }; }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes two {@code AutoClosable} resources. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction2<? super T1, ? super T2, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply(); T2 t2 = t2Supplier.apply()) { return f.apply(t1, t2); } }); } }
/** * Creates a Try of a CheckedFunction0. * * @param supplier A checked supplier * @param <T> Component type * @return {@code Success(supplier.apply())} if no exception occurs, otherwise {@code Failure(throwable)} if an * exception occurs calling {@code supplier.apply()}. */ static <T> Try<T> of(CheckedFunction0<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); try { return new Success<>(supplier.apply()); } catch (Throwable t) { return new Failure<>(t); } }
/** * Wraps the result of a computation that may fail in a {@code Try}. * * @param f A computation that takes one {@code AutoClosable} resource. * @param <R> Result type of the computation. * @return A new {@code Try} instance. */ @SuppressWarnings("try")/* https://bugs.openjdk.java.net/browse/JDK-8155591 */ public <R> Try<R> of(CheckedFunction1<? super T1, ? extends R> f) { return Try.of(() -> { try (T1 t1 = t1Supplier.apply()) { return f.apply(t1); } }); } }
/** * Returns a supplier which is decorated by a bulkhead. * * @param bulkhead the Bulkhead * @param supplier the original supplier * @param <T> the type of results supplied by this supplier * @return a supplier which is decorated by a Bulkhead. */ static <T> CheckedFunction0<T> decorateCheckedSupplier(Bulkhead bulkhead, CheckedFunction0<T> supplier){ return () -> { BulkheadUtils.isCallPermitted(bulkhead); try { return supplier.apply(); } finally { bulkhead.onComplete(); } }; }
/** * Creates a timed checked supplier. * @param timer the timer to use * @param supplier the original supplier * @return a timed supplier */ static <T> CheckedFunction0<T> decorateCheckedSupplier(Timer timer, CheckedFunction0<T> supplier){ return () -> { final Timer.Context context = timer.context(); try { T returnValue = supplier.apply(); context.onSuccess(); return returnValue; }catch (Throwable e){ context.onError(); throw e; } }; }
@Test public void decorateCheckedSupplier() throws Throwable { CheckedFunction0 supplier = mock(CheckedFunction0.class); CheckedFunction0 decorated = RateLimiter.decorateCheckedSupplier(limit, supplier); when(limit.getPermission(config.getTimeoutDuration())) .thenReturn(false); Try decoratedSupplierResult = Try.of(decorated); then(decoratedSupplierResult.isFailure()).isTrue(); then(decoratedSupplierResult.getCause()).isInstanceOf(RequestNotPermitted.class); verify(supplier, never()).apply(); when(limit.getPermission(config.getTimeoutDuration())) .thenReturn(true); Try secondSupplierResult = Try.of(decorated); then(secondSupplierResult.isSuccess()).isTrue(); verify(supplier, times(1)).apply(); }
@Test public void shouldDecorateCheckedSupplier() throws Throwable { // Given the HelloWorldService returns Hello world BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world"); // And measure the call with a Timer CheckedFunction0<String> timedSupplier = Timer.decorateCheckedSupplier(timer, helloWorldService::returnHelloWorldWithException); String value = timedSupplier.apply(); assertThat(timer.getMetrics().getNumberOfTotalCalls()).isEqualTo(1); assertThat(timer.getMetrics().getNumberOfSuccessfulCalls()).isEqualTo(1); assertThat(timer.getMetrics().getNumberOfFailedCalls()).isEqualTo(0); assertThat(metricRegistry.getCounters().size()).isEqualTo(2); assertThat(metricRegistry.getTimers().size()).isEqualTo(1); assertThat(value).isEqualTo("Hello world"); // Then the helloWorldService should be invoked 1 time BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException(); }
@Test public void shouldDecorateCheckedSupplierAndReturnWithSuccess() throws Throwable { // Given Bulkhead bulkhead = Bulkhead.of("test", config); BDDMockito.given(helloWorldService.returnHelloWorldWithException()).willReturn("Hello world"); // When CheckedFunction0<String> checkedSupplier = Bulkhead.decorateCheckedSupplier(bulkhead, helloWorldService::returnHelloWorldWithException); // Then assertThat(checkedSupplier.apply()).isEqualTo("Hello world"); assertThat(bulkhead.getMetrics().getAvailableConcurrentCalls()).isEqualTo(1); BDDMockito.then(helloWorldService).should(times(1)).returnHelloWorldWithException(); }