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 java.lang.Object
Class to handle generic listeners in a thread-safe manner.- Author:
- Christopher Mindus
Constructor Summary
Constructors Constructor Description GenericListener(java.lang.Class<LISTENER_INTERFACE> clazz)
Constructs the listener.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addListener(LISTENER_INTERFACE listener)
Adds a new listener.boolean
clear()
Clears all the listeners.void
forEach(java.util.function.Consumer<? super LISTENER_INTERFACE> action)
Performs the given action for all listeners.boolean
hasListeners()
Checks if there are any current listeners.LISTENER_INTERFACE[]
listeners()
Gets a the listeners array.boolean
removeListener(LISTENER_INTERFACE listener)
Removes a listener.
Constructor Detail
GenericListener
public GenericListener(java.lang.Class<LISTENER_INTERFACE> clazz)
Constructs the listener.- Parameters:
clazz
- The class instance of the listener interface.
Method Detail
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.
- Throws:
java.lang.NullPointerException
- If the listener isnull
.
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.
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(java.util.function.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:
java.lang.NullPointerException
- if the specified action isnull
.