Package jasima.core.util.observer
Class NotifierImpl<SUBJECT extends Notifier<SUBJECT,MESSAGE>,MESSAGE>
- java.lang.Object
-
- jasima.core.util.observer.NotifierImpl<SUBJECT,MESSAGE>
-
- All Implemented Interfaces:
Notifier<SUBJECT,MESSAGE>
public class NotifierImpl<SUBJECT extends Notifier<SUBJECT,MESSAGE>,MESSAGE> extends Object implements Notifier<SUBJECT,MESSAGE>
Example implementation ofNotifier
functionality. ANotifierImpl
handles notifier functionality for some realNotifier
(subject).It is safe to fire new events while a current one is executing. In that case the new message will be processed after processing the current one finished.
Listeners can also be removed while a message is send/processed.
As an example of how to use this class, have a look at
ExperimentListener
used byExperiment
s.- Author:
- Torsten Hildebrandt
- See Also:
Notifier
,NotifierListener
-
-
Constructor Summary
Constructors Constructor Description NotifierImpl(SUBJECT subject)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends NotifierListener<SUBJECT,MESSAGE>>
voidaddListener(TypeHint<T> listenerType, T eventHandler)
Adds a new listener.<T extends NotifierListener<SUBJECT,MESSAGE>>
voidaddListener(Class<T> listenerType, T eventHandler)
Adds a new listener.<T extends NotifierListener<SUBJECT,MESSAGE>>
TaddListener(T l)
Adds a new listener.void
fire(MESSAGE msg)
Send a message to all registered listeners.NotifierListener<SUBJECT,MESSAGE>
getListener(int idx)
Returns the listener with the given index (0-based).Notifier<SUBJECT,MESSAGE>
notifierImpl()
int
numListener()
Returns the number of currently registered listeners.void
removeCurrentListener()
This method can be used to unregister the currently active listener while firing.boolean
removeListener(NotifierListener<SUBJECT,MESSAGE> l)
Removes the listener given as a parameter.
-
-
-
Constructor Detail
-
NotifierImpl
public NotifierImpl(SUBJECT subject)
-
-
Method Detail
-
numListener
public int numListener()
Returns the number of currently registered listeners.
-
addListener
public <T extends NotifierListener<SUBJECT,MESSAGE>> T addListener(T l)
Adds a new listener.
-
addListener
public <T extends NotifierListener<SUBJECT,MESSAGE>> void addListener(Class<T> listenerType, T eventHandler)
Adds a new listener. In contrast toaddListener(NotifierListener)
this method has an additional type parameter, usually representing a functional interface. Using this approacheventHandler
can be a lambda expression or method reference.
-
addListener
public <T extends NotifierListener<SUBJECT,MESSAGE>> void addListener(TypeHint<T> listenerType, T eventHandler)
Adds a new listener. This is the same asaddListener(Class, NotifierListener)
, only addingTypeRef
as a super type token. This can be used when the listener is a parameterized type.
-
removeListener
public boolean removeListener(NotifierListener<SUBJECT,MESSAGE> l)
Removes the listener given as a parameter. This method returnstrue
on success andfalse
when the listener could not be found.
-
removeCurrentListener
public void removeCurrentListener()
This method can be used to unregister the currently active listener while firing. This is particularly useful when using lambda's as listeners (seeaddListener(Class, NotifierListener)
), as their reference is otherwise hard to find (not accessible usingthis
).- Specified by:
removeCurrentListener
in interfaceNotifier<SUBJECT extends Notifier<SUBJECT,MESSAGE>,MESSAGE>
- Throws:
NullPointerException
- If called while not firing.
-
getListener
public NotifierListener<SUBJECT,MESSAGE> getListener(int idx)
Returns the listener with the given index (0-based).- Specified by:
getListener
in interfaceNotifier<SUBJECT extends Notifier<SUBJECT,MESSAGE>,MESSAGE>
- See Also:
numListener()
-
-