Class 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

      All Methods Static Methods Instance Methods Concrete Methods 
      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 Detail

      • 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