Package com.iizix
Class GenericListener<LISTENER_INTERFACE>
java.lang.Object
com.iizix.GenericListener<LISTENER_INTERFACE>
- Direct Known Subclasses:
ServerConfigFile
Class to handle generic listeners in a thread-safe manner.
- Author:
- Christopher Mindus
Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionboolean
addListener
(LISTENER_INTERFACE listener) Adds a new listener.boolean
clear()
Clears all the listeners.void
forEach
(Consumer<? super LISTENER_INTERFACE> action) Performs the given action for all listeners.boolean
Checks if there are any current listeners.Gets a the listeners array.boolean
removeListener
(LISTENER_INTERFACE listener) Removes a listener.
Constructor Details
GenericListener
Constructs the listener.- Parameters:
clazz
- The class instance of the listener interface.
Method Details
addListener
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 thelistener
isnull
.
removeListener
Removes a listener.- Parameters:
listener
- The listener to remove.- Returns:
true
iflistener
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
Gets a the listeners array.- Returns:
- A cached array of the listeners (do not modify the array).
forEach
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 isnull
.