From 92f7603051aca10f672c454f2ed56c034104f789 Mon Sep 17 00:00:00 2001 From: Eric Fenderbosch Date: Fri, 24 Mar 2017 14:05:39 -0400 Subject: [PATCH] added DecorateConsumer syntatic sugar --- .../resilience4j/decorators/Decorators.java | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/resilience4j-all/src/main/java/io/github/resilience4j/decorators/Decorators.java b/resilience4j-all/src/main/java/io/github/resilience4j/decorators/Decorators.java index d8d5bf6406..f0de30a25b 100644 --- a/resilience4j-all/src/main/java/io/github/resilience4j/decorators/Decorators.java +++ b/resilience4j-all/src/main/java/io/github/resilience4j/decorators/Decorators.java @@ -9,11 +9,13 @@ import java.util.concurrent.CompletionStage; import java.util.concurrent.ScheduledExecutorService; +import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; /** - * A Decorator builder which can be used to apply multiple decorators to a (Checked-)Supplier, (Checked-)Function or (Checked-)Runnable. + * A Decorator builder which can be used to apply multiple decorators to a (Checked-)Supplier, (Checked-)Function, + * (Checked-)Runnable, (Checked-)CompletionStage or (Checked-)Consumer */ public interface Decorators{ @@ -45,6 +47,9 @@ static DecorateCompletionStage ofCompletionStage(Supplier(stageSupplier); } + public static DecorateConsumer ofConsumer(Consumer consumer) { + return new DecorateConsumer<>(consumer); + } class DecorateSupplier{ private Supplier supplier; @@ -268,4 +273,31 @@ public CompletionStage get() { return stageSupplier.get(); } } -} \ No newline at end of file + + class DecorateConsumer { + + private Consumer consumer; + + private DecorateConsumer(Consumer consumer) { + this.consumer = consumer; + } + + public DecorateConsumer withCircuitBreaker(CircuitBreaker circuitBreaker) { + consumer = CircuitBreaker.decorateConsumer(circuitBreaker, consumer); + return this; + } + + public DecorateConsumer withRateLimiter(RateLimiter rateLimiter) { + consumer = RateLimiter.decorateConsumer(rateLimiter, consumer); + return this; + } + + public Consumer decorate() { + return consumer; + } + + public void accept(T obj) { + consumer.accept(obj); + } + } +}