S - The type of the ListenerStore this provider uses.public abstract class AbstractEventProvider<S extends ListenerStore> extends Object implements EventProvider<S>
EventProvider methods. All implementations
are thread-safe.
Note about thread safe interface: All publicly accessible methods are thread safe, internal and protected helper methods are not thread safe.
| Modifier and Type | Field and Description |
|---|---|
protected ExceptionCallback |
defaultHandler
The default
ExceptionCallback which prints some information about
the occurred error to logger. |
protected ExceptionCallback |
exceptionHandler
Callback to handle event handler exceptions.
|
| Constructor and Description |
|---|
AbstractEventProvider(S store)
Creates a new
AbstractEventProvider. |
| Modifier and Type | Method and Description |
|---|---|
protected <L extends Listener,E extends Event<?,?>> |
checkDispatchArgs(E event,
Object bc,
ExceptionCallback ec)
Helper method which serves for throwing
IllegalArgumentException
if any of the passed arguments is null. |
void |
close()
Closes this EventProvider and its
ListenerStore (thus, removes
all registered Listeners from the store). |
protected <L extends Listener,E extends Event<?,L>> |
createInvocation(L listener,
E event,
BiConsumer<L,E> bc,
ExceptionCallback ec)
Creates a new
EventInvocation object for dispatching the given event to
the given listener. |
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.
|
protected abstract boolean |
isImplementationSequential()
Whether this EventProvider implementation is sequential.
|
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. |
protected <L extends Listener,E extends Event<?,L>> |
notifyListeners(E event,
BiConsumer<L,E> bc,
ExceptionCallback ec)
Notifies all listeners registered for the provided class with the
provided event.
|
protected <L extends Listener,E extends Event<?,L>> |
notifySingle(L listener,
E event,
BiConsumer<L,E> bc,
ExceptionCallback ec)
Notifies a single listener and internally handles exceptions using the
ExceptionCallback. |
void |
setExceptionCallback(ExceptionCallback callBack)
Sets the default
ExceptionCallback which will be notified about
exceptions when dispatching events without explicitly specifying an
ExceptionCallback. |
String |
toString() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitcanDispatch, configure, createDefault, dispatch, dispatch, dispatchprotected ExceptionCallback exceptionHandler
protected final ExceptionCallback defaultHandler
ExceptionCallback which prints some information about
the occurred error to logger. The exact format is not specified.public AbstractEventProvider(S store)
AbstractEventProvider.store - Responsible for storing and retrieving listeners of this
provider.public S listeners()
EventProviderListenerStore which supplies Listeners to this EventProvider.listeners in interface EventProvider<S extends ListenerStore>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.DefaultDispatchableprotected <L extends Listener,E extends Event<?,?>> void checkDispatchArgs(E event, Object bc, ExceptionCallback ec)
IllegalArgumentException
if any of the passed arguments is null.L - Type of the listeners which will be notified.E - Type of the event which will be passed to a listener.event - The event.bc - The method to call on the listenerec - The ExceptionCallbackIllegalArgumentException - If any argument is null.public void setExceptionCallback(ExceptionCallback callBack)
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>callBack - The ExceptionCallback for handling event handler exceptions, or
null to use the default behavior.protected <L extends Listener,E extends Event<?,L>> void notifyListeners(E event, BiConsumer<L,E> bc, ExceptionCallback ec)
ExceptionCallback.
This method does not check whether this provider is ready for dispatching and might thus throw an exception when trying to dispatch an event while the provider is not ready.
L - Type of the listeners which will be notified.E - Type of the event which will be passed to a listener.event - The event to pass to each listener.bc - The method of the listener to call.ec - The callback which gets notified about exceptions.AbortionException - If the ExceptionCallback threw an
AbortionExceptionprotected <L extends Listener,E extends Event<?,L>> void notifySingle(L listener, E event, BiConsumer<L,E> bc, ExceptionCallback ec)
ExceptionCallback.L - Type of the listeners which will be notified.E - Type of the event which will be passed to a listener.listener - The single listener to notify.event - The event to pass to the listener.bc - The method of the listener to call.ec - The callback which gets notified about exceptions.AbortionException - If the ExceptionCallback or the
listener threw an AbortionException.protected <L extends Listener,E extends Event<?,L>> EventInvocation createInvocation(L listener, E event, BiConsumer<L,E> bc, ExceptionCallback ec)
EventInvocation object for dispatching the given event to
the given listener.L - Type of the listeners which will be notified.E - Type of the event which will be passed to a listener.listener - The listener to notify.event - The event to pass to the listenerbc - The method of the listener to call.ec - The callback which gets notified about exceptions.public final 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>protected abstract boolean isImplementationSequential()
isSequential() method considers the result of this method and
the result of the current ListenerStore's
isSequential method for determining
whether dispatch events of this provider are sequential.public 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>Copyright © 2014–2015. All rights reserved.