Class OnceConsumer<T>

java.lang.Object
io.github.resilience4j.core.functions.OnceConsumer<T>
Type Parameters:
T - the type of the input which is passed to the consumer and executed once.

public final class OnceConsumer<T> extends Object
Apply a computation only once.
  • Method Details

    • of

      public static <T> OnceConsumer<T> of(T t)
      Create a do once consumer.
      Type Parameters:
      T - type of input on which operation is applied.
      Parameters:
      t - input which is passed to operation
      Returns:
    • applyOnce

      public void applyOnce(Consumer<T> consumer)
      Apply a computation on subject only once.
      
       List<String> lst = new ArrayList<>();
      
       OnceConsumer<List<String>> once = OnceConsumer.of(lst);
       once.applyOnce((l) -> l.add("Hello World"));
       once.applyOnce((l) -> l.add("Hello World"));
      
       assertThat(lst).hasSize(1).contains("Hello World");
      
       
      Parameters:
      consumer - computation run once with input t