Class ListenerHelper<T>
- java.lang.Object
-
- org.threadly.concurrent.event.ListenerHelper<T>
-
- Type Parameters:
T
- Interface for listeners to implement and called into with
- Direct Known Subclasses:
AsyncCallListenerHelper
,DefaultExecutorListenerHelper
public class ListenerHelper<T> extends java.lang.Object
Class which assist with holding and calling to listeners of any interface. In parallel designs it is common to have things subscribe for actions to occur (to later be alerted once an action occurs). This class makes it easy to allow things to register as a listener.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.- Since:
- 2.2.0
-
-
Constructor Summary
Constructors Constructor Description ListenerHelper(java.lang.Class<? super T> listenerInterface)
Constructs a newListenerHelper
that will handle listeners with the provided interface.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(T listener)
Adds a listener to be executed on the nextcall()
to this instance.void
addListener(T listener, java.util.concurrent.Executor executor)
Adds a listener to be executed on the nextcall()
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 nextcall()
invocation.boolean
removeListener(T listener)
Attempts to remove a listener waiting to be called.
-
-
-
Constructor Detail
-
ListenerHelper
public ListenerHelper(java.lang.Class<? super T> listenerInterface)
Constructs a newListenerHelper
that will handle listeners with the provided interface. The provided class MUST be an interface.- Parameters:
listenerInterface
- Interface that listeners need to implement
-
-
Method Detail
-
getSubscribedListeners
public java.util.Collection<T> getSubscribedListeners()
Return a collection of the currently subscribed listener instances. This returned collection can NOT be modified.- Returns:
- A non-null collection of currently subscribed listeners
-
call
public T call()
Calls to notify the subscribed listeners with the given call. This returns an implementation of the listener interface, you can then call to the function you wish to have called on the listeners (of course providing the arguments you want the listeners to be called with).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.
- Returns:
- Implementation of listener interface to have call subscribed listeners
-
addListener
public void addListener(T listener)
Adds a listener to be executed on the nextcall()
to this instance. This is the same as adding a listener and providing null for theExecutor
.- Parameters:
listener
- Listener to be called whencall()
is invoked
-
addListener
public void addListener(T listener, java.util.concurrent.Executor executor)
Adds a listener to be executed on the nextcall()
to this instance. If an executor is provided, on the nextcall()
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 thecall()
.If an
Executor
is provided, and thatExecutor
is NOT single threaded, the listener may be called concurrently. You can ensure this wont happen by using theKeyDistributedExecutor
to get an executor from a single key, or by using theExecutorLimiter
with a limit of one, or an instance of theSingleThreadScheduler
.- Parameters:
listener
- Listener to be called whencall()
is invokedexecutor
-Executor
to call listener on, ornull
-
removeListener
public boolean removeListener(T listener)
Attempts to remove a listener waiting to be called.- Parameters:
listener
- listener instance to be removed- Returns:
true
if the listener was removed
-
clearListeners
public void clearListeners()
Removes all listeners currently registered.
-
registeredListenerCount
public int registeredListenerCount()
Returns how many listeners were added, and will be ran on the nextcall()
invocation.- Returns:
- number of listeners registered to be called
-
-