0

I was going through the resilience4j code to find out how they implement the decorators, I can see Supplier/Consumer functional interfaces. i am still trying to understand on how to utilize these interfaces while coding. My question is based on this method,

static <T> Supplier<Future<T>> decorateFuture(Bulkhead bulkhead, Supplier<Future<T>> supplier) {
    return () -> {
        if (!bulkhead.tryAcquirePermission()) {
            final CompletableFuture<T> promise = new CompletableFuture<>();
            promise.completeExceptionally(BulkheadFullException.createBulkheadFullException(bulkhead));
            return promise;
        }
        try {
            return new BulkheadFuture<>(bulkhead, supplier.get());
        } catch (Throwable e) {
            bulkhead.onComplete();
            throw e;
        }
    };

Have couple of questions, Is it returning a Supplier<Future> because of its defined as a lambda?

How do these decorators are constructed, please can someone provide a simple example,

 Supplier<String> decoratedSupplier = Bulkhead
            .decorateSupplier(bulkhead, () -> "This can be any method which returns: 'Hello");
Umar
  • 1,002
  • 3
  • 12
  • 29
  • Does this answer your question? [Java lambda expression - how interface name was omitted?](https://stackoverflow.com/questions/57696871/java-lambda-expression-how-interface-name-was-omitted) – Phu Ngo Feb 24 '21 at 03:45
  • No. I would like to understand why a supplier is used here – Umar Feb 25 '21 at 23:23
  • Supplier is used as the type of the lambda (see in the above link how functional interface and lambda work together), here because the lambda matches its functional interface method [Supplier#get](https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html#get--) which takes no argument and returns a value of generic type `T` (which is in this case a `Future`) – Phu Ngo Mar 02 '21 at 05:54

0 Answers0