public class RunnableListenerHelper
extends java.lang.Object
For listener designs which are not using runnables, look at ListenerHelper
.
ListenerHelper
allows you to create similar designs while using any any interface to
call back on.
Constructor and Description |
---|
RunnableListenerHelper(boolean callListenersOnce)
Constructs a new
RunnableListenerHelper . |
Modifier and Type | Method and Description |
---|---|
void |
addListener(java.lang.Runnable listener)
Adds a listener to be called.
|
void |
addListener(java.lang.Runnable listener,
java.util.concurrent.Executor executor)
Adds a listener to be called.
|
void |
callListeners()
Will call all listeners that are registered with this helper.
|
void |
clearListeners()
Removes all listeners currently registered.
|
java.util.Collection<java.lang.Runnable> |
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 to
callListeners . |
boolean |
removeListener(java.lang.Runnable listener)
Attempts to remove a listener waiting to be called.
|
public RunnableListenerHelper(boolean callListenersOnce)
RunnableListenerHelper
. This can call listeners only once, or every
time callListeners()
is called.callListenersOnce
- true
if listeners should only be called oncepublic java.util.Collection<java.lang.Runnable> getSubscribedListeners()
public void callListeners()
If calling multiple times, this will only have an effect if constructed with a false
,
indicating that listeners can expect to be called multiple times. In which case all
listeners that have registered will be called again. If this was constructed with the
expectation of only calling once an IllegalStateException
will be thrown on
subsequent calls.
public void addListener(java.lang.Runnable listener)
RunnableListenerHelper
was constructed with
true
(listeners can only be called once) then this listener will be called
immediately. This just defers to the other addListener call, providing null for the executor.
So when the listener runs, it will be on the same thread as the one invoking
callListeners()
.listener
- runnable to call when trigger event calledpublic void addListener(java.lang.Runnable listener, java.util.concurrent.Executor executor)
RunnableListenerHelper
was constructed with
true
(listeners can only be called once) then this listener will be called
immediately. If the executor is null it will be called either on this thread or the thread
calling callListeners()
(depending on the previous condition).
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
- runnable to call when trigger event calledexecutor
- executor listener should run on, or null
public boolean removeListener(java.lang.Runnable listener)
listener
- listener instance to be removedtrue
if the listener was removedpublic void clearListeners()
public int registeredListenerCount()
callListeners
. If this was constructed to only run once, all listeners will be
removed after called, and thus this will report zero after callListeners has been invoked.