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 ofNotifierfunctionality. ANotifierImplhandles 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
ExperimentListenerused byExperiments.- 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.voidfire(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()intnumListener()Returns the number of currently registered listeners.voidremoveCurrentListener()This method can be used to unregister the currently active listener while firing.booleanremoveListener(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 approacheventHandlercan 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 addingTypeRefas 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 returnstrueon success andfalsewhen 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:
removeCurrentListenerin 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:
getListenerin interfaceNotifier<SUBJECT extends Notifier<SUBJECT,MESSAGE>,MESSAGE>- See Also:
numListener()
-
-