Module aeonics.core
Package aeonics.util

Class Callback<V,​T>

  • Type Parameters:
    V - the type of data that triggered this callback
    T - the type of the target class of this callback
    All Implemented Interfaces:
    java.lang.Iterable<Functions.BiConsumer<V,​T>>

    public class Callback<V,​T>
    extends java.lang.Object
    implements java.lang.Iterable<Functions.BiConsumer<V,​T>>
    This class can be used to set a callback method.

    The callback will call every registered handlers with the trigger value and a reference to the target object instance. This way, you do not have to keep a reference to the outer scope from a lambda expression.

     
     // DO THIS : use the provided target
     
     MyClass instance = new MyClass();
     instance.callback.then((value, target) -> { target.something(); });
     
     // AVOID THIS : reference the outer scope
     
     MyClass instance = new MyClass();
     instance.callback.then((value, target) -> { instance.something(); });
     
     

    Consider using one of the once(BiConsumer) variants for handlers that should execute only once.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Callback.Once<U,​S>
      This class specified that a callback handler should only run once and then be removed from the list of handlers.
    • Constructor Summary

      Constructors 
      Constructor Description
      Callback​(Functions.Supplier<T> target)
      Creates a new callback for the specified target supplier.
      Callback​(T target)
      Creates a new callback for the specified target
    • Constructor Detail

      • Callback

        public Callback​(T target)
        Creates a new callback for the specified target
        Parameters:
        target - the target of this callback
      • Callback

        public Callback​(Functions.Supplier<T> target)
        Creates a new callback for the specified target supplier. Upon trigger, the target value is fetched once before returning and cached during the handler propagation.
        Parameters:
        target - the getter for the target of this callback
    • Method Detail

      • once

        public static <U,​S> Callback.Once<U,​S> once​(Functions.BiConsumer<U,​S> handler)
        Returns a handler that will only run once.
        Type Parameters:
        U - the type of data that triggered this callback
        S - the type of the target class of this callback
        Parameters:
        handler - the handler that should only run once. You may provide a Functions.BiConsumer that will be given the callback target as second argument.
        Returns:
        a handler that will only run once
      • once

        public static <U,​S> Callback.Once<U,​S> once​(Functions.Runnable handler)
        Returns a handler that will only run once.
        Type Parameters:
        U - the type of data that triggered this callback
        S - the type of the target class of this callback
        Parameters:
        handler - the handler that should only run once
        Returns:
        a handler that will only run once
      • then

        public void then​(Functions.BiConsumer<V,​T> handler)
        Adds a handler for this callback.

        Consider Callback.Once if the handler should only run once.

        Parameters:
        handler - the handler
      • remove

        public void remove​(Functions.BiConsumer<V,​T> handler)
        Removes the specified handler from this callback.
        Parameters:
        handler - the handler
      • clear

        public void clear()
        Removes all handlers from this callback.
      • trigger

        public void trigger()
        Triggers the handlers with a null value
      • trigger

        public void trigger​(V value)
        Triggers the handlers with the specified value. All handlers are executed synchronously and sequentially
        Parameters:
        value - the value
      • iterator

        public java.util.Iterator<Functions.BiConsumer<V,​T>> iterator()
        Provides an unmodifiable iterator over registered handlers for this callback. Note that the iterator is not thread safe in case handlers are added or removed during iteration.
        Specified by:
        iterator in interface java.lang.Iterable<V>