T
- Interface for listeners to implement and called into withpublic class ListenerHelper<T>
extends java.lang.Object
For listener designs which do NOT need to provide arguments for their listeners, look at using
RunnableListenerHelper
. RunnableListenerHelper
is more efficient and flexible
for listeners of that type. It also has a cleaner and easier to use interface.
Constructor and Description |
---|
ListenerHelper(java.lang.Class<? super T> listenerInterface)
Constructs a new
ListenerHelper that will handle listeners with the provided
interface. |
Modifier and Type | Method and Description |
---|---|
void |
addListener(T listener)
Adds a listener to be executed on the next
call() to this instance. |
void |
addListener(T listener,
java.util.concurrent.Executor executor)
Adds a listener to be executed on the next
call() to this instance. |
T |
call()
Calls to notify the subscribed listeners with the given call.
|
void |
clearListeners()
Removes all listeners currently registered.
|
java.util.Collection<T> |
getSubscribedListeners()
Return a collection of the currently subscribed listener instances.
|
int |
registeredListenerCount()
Returns how many listeners were added, and will be ran on the next
call() invocation. |
boolean |
removeListener(T listener)
Attempts to remove a listener waiting to be called.
|
public ListenerHelper(java.lang.Class<? super T> listenerInterface)
ListenerHelper
that will handle listeners with the provided
interface. The provided class MUST be an interface.listenerInterface
- Interface that listeners need to implementpublic java.util.Collection<T> getSubscribedListeners()
public T call()
Any calls off the returned instance will execute on all subscribed listeners. If those listeners were provided with an executor the execution for calling that listener will happen on the provided executor. If no executor was provided, the execution of the listener will happen on the thread invoking this call.
public void addListener(T listener)
call()
to this instance. This is the same
as adding a listener and providing null for the Executor
.listener
- Listener to be called when call()
is invokedpublic void addListener(T listener, java.util.concurrent.Executor executor)
call()
to this instance. If an executor
is provided, on the next call()
a task will be put on the executor to call this
listener. If none is provided, the listener will be executed on the thread that is invoking
the call()
.
If an Executor
is provided, and that Executor
is NOT single threaded, the
listener may be called concurrently. You can ensure this wont happen by using the
KeyDistributedExecutor
to get an executor from a
single key, or by using the ExecutorLimiter
with a limit of one, or an instance of the
SingleThreadScheduler
.
listener
- Listener to be called when call()
is invokedexecutor
- Executor
to call listener on, or null
public boolean removeListener(T listener)
listener
- listener instance to be removedtrue
if the listener was removedpublic void clearListeners()
public int registeredListenerCount()
call()
invocation.