S - The type of the ListenerStore this provider uses.P - The type of the wrapped provider.public class StatisticsEventProvider<S extends ListenerStore,P extends EventProvider<S>> extends Object implements EventProvider<S>
| Constructor and Description |
|---|
StatisticsEventProvider(P wrapped)
Creates a new StatisticsEventProvider which counts dispatch action of the
given
EventProvider. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
canDispatch()
Gets whether this EventProvider is ready for dispatching.
|
void |
close()
Closes this EventProvider and its
ListenerStore (thus, removes
all registered Listeners from the store). |
void |
dispatch(DefaultDispatchable event)
Dispatches the given event by calling its
DefaultDispatchable.defaultDispatch(EventProvider, ExceptionCallback)
method, passing this as first argument and the currently set
ExceptionCallback as second argument. |
<L extends Listener,E extends Event<?,L>> |
dispatch(E event,
BiConsumer<L,E> bc)
Notifies all listeners of a certain kind about an occurred event.
|
<L extends Listener,E extends Event<?,L>> |
dispatch(E event,
BiConsumer<L,E> bc,
ExceptionCallback ec)
Notifies all listeners of a certain kind about an occurred event with
explicit error handling.
|
Map<Class<? extends Listener>,Integer> |
getNotificationStatistics()
Gets a map containing statistics about how often a certain kind of
listener has been notified.
|
P |
getWrapped()
Gets the underlying EventProvider which is wrapped by this
StatisticsEventProvider.
|
boolean |
isSequential()
Returns whether this EventProvider is sequential, which means it strictly
notifies listeners in the order in which they were registered for a
certain event.
|
S |
listeners()
Retrieves the
ListenerStore which supplies Listeners to this EventProvider. |
void |
setExceptionCallback(ExceptionCallback ec)
Sets the default
ExceptionCallback which will be notified about
exceptions when dispatching events without explicitly specifying an
ExceptionCallback. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconfigure, createDefault, dispatch, dispatch, dispatchpublic StatisticsEventProvider(P wrapped)
EventProvider.wrapped - The provider to wrap.public P getWrapped()
public Map<Class<? extends Listener>,Integer> getNotificationStatistics()
public <L extends Listener,E extends Event<?,L>> void dispatch(E event, BiConsumer<L,E> bc)
EventProviderEventProvider.canDispatch(), this method returns immediately without doing
anything. This method will stop notifying further listeners if the passed
event has been marked 'handled' using Event.setHandled(boolean).
Consider an UserListener interface:
public interface UserListener extends Listener {
public void userAdded(UserEvent e);
public void userDeleted(UserEvent e);
}
Notifying all registered UserListeners about an added user is as easy as
calling
eventProvider.dispatchEvent(event, UserListener::userAdded)
This method uses the global ExceptionCallback provided to
EventProvider.setExceptionCallback(ExceptionCallback) or an default instance
if none has been explicitly set.
Note on concurrency: This method operates on a copy of the list of targeted listeners. This allows you to add/remove listeners from within a listening method.
Please note that neither parameter to this method must be null.
dispatch in interface EventProvider<S extends ListenerStore>L - Type of the listeners which will be notified.E - Type of the event which will be passed to a listener.event - The occurred event which shall be passed to each listener.bc - Function to delegate the event to the specific callback method
of the listener.public <L extends Listener,E extends Event<?,L>> void dispatch(E event, BiConsumer<L,E> bc, ExceptionCallback ec)
EventProviderEventProvider.canDispatch(), this method returns immediately
without doing anything. This method will stop notifying further listeners
if the passed event has been marked 'handled' using
Event.setHandled(boolean).
Consider an UserListener interface:
public interface UserListener extends Listener {
public void userAdded(UserEvent e);
public void userDeleted(UserEvent e);
}
Notifying all registered UserListeners about an added user is as easy as
calling
final ExceptionCallback callBack = new EceptionCallback() { ... };
eventProvider.dispatchEvent(event, UserListener::userAdded, callBack);
The ExceptionCallback gets notified when any of the listeners
throws an unexpected exception. If the exception handler itself throws an
exception, it will be ignored. The callback provided to this method takes
precedence over the global callback provided by
EventProvider.setExceptionCallback(ExceptionCallback).
Note on concurrency: This method operates on a copy of the list of targeted listeners. This allows you to add/remove listeners from within a listening method.
Please note that neither parameter to this method must be null.
dispatch in interface EventProvider<S extends ListenerStore>L - Type of the listeners which will be notified.E - Type of the event which will be passed to a listener.event - The occurred event which shall be passed to each listener.bc - Function to delegate the event to the specific callback method
of the listener.ec - Callback to be notified when any of the listeners throws an
exception.public void dispatch(DefaultDispatchable event)
EventProviderDefaultDispatchable.defaultDispatch(EventProvider, ExceptionCallback)
method, passing this as first argument and the currently set
ExceptionCallback as second argument. The defaultDispatch method
will in return call
EventProvider.dispatch(Event, BiConsumer, ExceptionCallback) on this provider.dispatch in interface EventProvider<S extends ListenerStore>event - The event to dispatch using its default dispatch method.DefaultDispatchablepublic void close()
EventProviderListenerStore (thus, removes
all registered Listeners from the store). Depending on the actual
implementation, the EventProvider might not be able to dispatch further
events after closing. On some implementations closing might have no
additional effect.close in interface EventProvider<S extends ListenerStore>public S listeners()
EventProviderListenerStore which supplies Listeners to this EventProvider.listeners in interface EventProvider<S extends ListenerStore>public boolean canDispatch()
EventProvidercanDispatch in interface EventProvider<S extends ListenerStore>dispatchpublic void setExceptionCallback(ExceptionCallback ec)
EventProviderExceptionCallback which will be notified about
exceptions when dispatching events without explicitly specifying an
ExceptionCallback. The ExceptionCallback which is installed by default
simply prints the stack traces to the error console.
You can reset the ExceptionCallback to the default handler by providing
null as parameter.
setExceptionCallback in interface EventProvider<S extends ListenerStore>ec - The ExceptionCallback for handling event handler exceptions, or
null to use the default behavior.public boolean isSequential()
EventProvider
Note: Implementors must obey the result of the
isSequential property of the
currently used ListenerStore. If the store is not sequential, this
provider won't be either.
isSequential in interface EventProvider<S extends ListenerStore>Copyright © 2014–2015. All rights reserved.