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 java.lang.Object
Apply a computation only once.
  • Method Summary

    Modifier and Type Method Description
    void applyOnce​(java.util.function.Consumer<T> consumer)
    Apply a computation on subject only once.
    static <T> OnceConsumer<T> of​(T t)
    Create a do once consumer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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​(java.util.function.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