Skip to content

Commit

Permalink
added DecorateConsumer syntatic sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
efenderbosch authored and RobWin committed Mar 25, 2017
1 parent 7cad651 commit 92f7603
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand Down Expand Up @@ -45,6 +47,9 @@ static <T> DecorateCompletionStage<T> ofCompletionStage(Supplier<CompletionStage
return new DecorateCompletionStage<>(stageSupplier);
}

public static <T> DecorateConsumer<T> ofConsumer(Consumer<T> consumer) {
return new DecorateConsumer<>(consumer);
}

class DecorateSupplier<T>{
private Supplier<T> supplier;
Expand Down Expand Up @@ -268,4 +273,31 @@ public CompletionStage<T> get() {
return stageSupplier.get();
}
}
}

class DecorateConsumer<T> {

private Consumer<T> consumer;

private DecorateConsumer(Consumer<T> consumer) {
this.consumer = consumer;
}

public DecorateConsumer<T> withCircuitBreaker(CircuitBreaker circuitBreaker) {
consumer = CircuitBreaker.decorateConsumer(circuitBreaker, consumer);
return this;
}

public DecorateConsumer<T> withRateLimiter(RateLimiter rateLimiter) {
consumer = RateLimiter.decorateConsumer(rateLimiter, consumer);
return this;
}

public Consumer<T> decorate() {
return consumer;
}

public void accept(T obj) {
consumer.accept(obj);
}
}
}

0 comments on commit 92f7603

Please sign in to comment.