T - Type of the source of this event.L - Type of the listener which can handle this event.public class Event<T,L extends Listener> extends Object
This class is the base of all events that can be fired. It holds the source
of the event and provides methods to stop delegation to further listeners if
this event has been handled by one listener. Events are meant to be short
living objects which are only used once - one Event instance for each call to
dispatch
. Any different usage might result in undefined behavior, especially when
using the isHandled() property. Also, to prevent memory leaks, Event
objects should never be stored over longer time.
Events explicitly belong to one kind of Listener implementation which
is able to handle it. The class of this listener is passed to the constructor
and queried by the EventProvider when collecting a list of targeted
listeners for a dispatch action.
Events are used in conjunction with the EventProvider and its
dispatch method. The dispatch method serves for notifying all registered
listeners with a certain event. The EventProvider will stop notifying further
listeners as soon as one listener sets this class' isHandled() to
true.
Event objects are not thread safe! Some EventProviders dispatch events asynchronously. If the same event instance is used within different threads, avoid modifying properties of the Event. Those modifications will result in undefined behavior.
| Constructor and Description |
|---|
Event(T source,
Class<L> listenerClass)
Creates a new event with a given source.
|
| Modifier and Type | Method and Description |
|---|---|
Class<L> |
getListenerClass()
Gets the type of the listener which can handle this event.
|
protected ListenerStore |
getListenerStore()
Gets the
ListenerStore from which the currently notified listener
has been retrieved. |
Map<String,Object> |
getProperties()
Returns the Map that is used to store properties in this event instance.
|
T |
getSource()
Gets the source of this event.
|
<E> Optional<E> |
getValue(String key)
Returns a value that has been stored using
setValue(String, Object). |
<E> Optional<E> |
getValueAs(String key,
Class<E> type)
Returns a value that has been stored using
setValue(String, Object). |
boolean |
isHandled()
Gets whether this event was already handled.
|
boolean |
isPrevented()
Whether this event was prevented the last time it has been passed to any
dispatch method of an
EventProvider. |
void |
setHandled(boolean isHandled)
Sets whether this event was already handled.
|
void |
setListenerStore(ListenerStore store)
Sets the ListenerStore from which the currently dispatching EventProvider
retrieves its Listeners.
|
void |
setPrevented(boolean prevented)
Called only by
EventProvider's dispatch method if
this event was prevented from being dispatched. |
void |
setValue(String key,
Object value)
Adds the given key-value mapping to this Event.
|
void |
stopNotifying(L listener)
Removes the provided listener from the
ListenerStore from which
it was supplied to the EventProvider which is currently dispatching this
event. |
public Map<String,Object> getProperties()
public <E> Optional<E> getValue(String key)
setValue(String, Object). As this method is generic, the value
will automatically be casted to the target type of the expression in
which this method is called. So this method has to be used with caution
in respect to type safety.E - Type of the resulting value.key - The key of the value to retrieve.getValueAs(String, Class)public <E> Optional<E> getValueAs(String key, Class<E> type)
setValue(String, Object). The value will be casted to the given
type.E - Type of the resulting value.key - The key of the value to retrieve.type - The type that the value will be casted to.getValue(String)public void setValue(String key, Object value)
getValue(String).
Note: Storing properties on events is generally discouraged in favor of creating explicit attributes with getters. However, there might arise situations in which you must attach further properties to an event without being able to modify its original source code. In these situations, storing properties with a String key might be useful.
key - The key under which the value is stored.value - The value to store.getValue(String),
getProperties()public T getSource()
public boolean isPrevented()
EventProvider.public void setPrevented(boolean prevented)
EventProvider's dispatch method if
this event was prevented from being dispatched.prevented - Whether the event was prevented.public Class<L> getListenerClass()
public void stopNotifying(L listener)
ListenerStore from which
it was supplied to the EventProvider which is currently dispatching this
event. Hence this method can only be called from within a listening
method while the event is being dispatched. Calling this method on an
Event instance which is not currently dispatched will raise an exception.
public class OneTimeUserListener extends UserListener {
@Override
public void userAdded(UserEvent e) {
// logic goes here
// ...
// this listener should not be notified any more about this kind of
// event.
e.stopNotifying(this);
}
}
Removing the listener will have no effect on the current dispatch action.
Even if you remove a different listener than this, it will be
notified anyway during this run, because the EventProvider collects the
Listeners before starting to dispatch the event.listener - The listener to remove from the currently dispatching
EventProviderprotected ListenerStore getListenerStore()
ListenerStore from which the currently notified listener
has been retrieved. If this Event is not currently dispatched, an
exception will be thrown.public void setListenerStore(ListenerStore store)
store - The listener store.public boolean isHandled()
true, no further listeners will be notified about this
event.public void setHandled(boolean isHandled)
Note that setting an event to be handled might have unintended side
effects when using an EventProvider which is not
sequential.
isHandled - Whether this event was handled.Copyright © 2014–2015. All rights reserved.