Package com.iizix

Class GenericListener<LISTENER_INTERFACE>

java.lang.Object
com.iizix.GenericListener<LISTENER_INTERFACE>
Direct Known Subclasses:
ServerConfigFile

public class GenericListener<LISTENER_INTERFACE> extends Object
Class to handle generic listeners in a thread-safe manner.
Author:
Christopher Mindus
  • Constructor Details

    • GenericListener

      public GenericListener(Class<LISTENER_INTERFACE> clazz)
      Constructs the listener.
      Parameters:
      clazz - The class instance of the listener interface.
  • Method Details

    • addListener

      public boolean addListener(LISTENER_INTERFACE listener)
      Adds a new listener. Calling this method multiple times with the same listener will have no effect (but to return false).
      Parameters:
      listener - The listener to add.
      Returns:
      true if the listener was added, false otherwise (nothing changed).
      Throws:
      NullPointerException - If the listener is null.
    • removeListener

      public boolean removeListener(LISTENER_INTERFACE listener)
      Removes a listener.
      Parameters:
      listener - The listener to remove.
      Returns:
      true if listener is successfully removed, false otherwise, i.e. if not previous an added listener instance.
    • hasListeners

      public boolean hasListeners()
      Checks if there are any current listeners.
      Returns:
      true if listeners are present, false otherwise.
    • clear

      public boolean clear()
      Clears all the listeners.
      Returns:
      true for cleared, false if no listeners were present.
    • listeners

      public LISTENER_INTERFACE[] listeners()
      Gets a the listeners array.
      Returns:
      A cached array of the listeners (do not modify the array).
    • forEach

      public void forEach(Consumer<? super LISTENER_INTERFACE> action)
      Performs the given action for all listeners. The call to the action is done inside a try-catch and logs potential exceptions.

      The default implementation behaves as if:

      
           for ( Listener listener: listeners() )
             action.accept(listener);
       

      Parameters:
      action - The action to be performed for each listener.
      Throws:
      NullPointerException - if the specified action is null.