T - The type of the decorated store.public abstract class AbstractSynchronizedListenerStore<T extends ListenerStore> extends Object implements ListenerStore
ListenerStore.synchronizedView()
method. This class provides a thread safe interface to the decorated store
which is passed in the constructor.
The implementation of this class should always be assignment compatible with the store it decorates. Thus in general, you would define an interface for your store which will then be implemented by the actual store implementation and the implementation of this class (which is typically a private class of your store).
| Modifier and Type | Class and Description |
|---|---|
protected static interface |
AbstractSynchronizedListenerStore.Transaction
Represents an atomic write transaction.
|
| Constructor and Description |
|---|
AbstractSynchronizedListenerStore(T wrapped)
Creates a new AbstractSynchronizedListenerStore which wraps the given
store.
|
| Modifier and Type | Method and Description |
|---|---|
<L extends Listener> |
add(Class<L> listenerClass,
L listener)
Adds a listener which will be notified for every event represented by the
given listener class.
|
<L extends Listener> |
add(L listener)
Adds the given object for all listener classes it implements.
|
void |
clearAll()
Removes all registered listeners from this EventProvider.
|
<L extends Listener> |
clearAll(Class<L> listenerClass)
Removes all listeners which have been registered for the provided
listener class.
|
void |
close()
Removes all registered Listeners from this store (
Listener.onUnregister(RegistrationEvent) will be called on each
Listener). |
<L extends Listener> |
get(Class<L> listenerClass)
Gets all listeners that have been registered using
ListenerStore.add(Class, Listener) for the given listener class. |
boolean |
isSequential()
States whether this ListenerStore implementation is sequential.
|
protected void |
modify(AbstractSynchronizedListenerStore.Transaction t)
Executes the given transaction within the context of a write lock.
|
protected <E> E |
read(Supplier<E> sup)
Executes the given supplier within the context of a read lock.
|
<L extends Listener> |
remove(Class<L> listenerClass,
L listener)
Removes a listener.
|
<L extends Listener> |
remove(L listener)
Removes the given object for all listener classes it implements.
|
abstract T |
synchronizedView()
Returns a thread safe view of this store.
|
protected final T extends ListenerStore wrapped
public AbstractSynchronizedListenerStore(T wrapped)
wrapped - The wrapped store.protected void modify(AbstractSynchronizedListenerStore.Transaction t)
t - The transaction to execute.protected <E> E read(Supplier<E> sup)
E - The result type.sup - The supplier.Supplier.get().public abstract T synchronizedView()
Returns a thread safe view of this store. The returned store will be backed by this one, thus changes on the one object are automatically reflected to the other. This also implies that unsynchronized access is still possible through the original object.
Subsequent calls to this method must return a cached instance of the
synchronized view. Store implementations that are natively thread safe
may simply return this.
Note: The actual returned object must be assignment compatible with this store, thus for the implementation 'MyListenerStore' the following statement must compile:
MyListenerStore store = new MyListenerStore().synchronizedView();
Note: This method should be implemented to return this store itself.
synchronizedView in interface ListenerStorepublic <L extends Listener> void add(Class<L> listenerClass, L listener)
ListenerStoreonRegister method gets
called to notify the listener about being added to a new parent. The
onRegister method is not subject to the dispatching strategy
implemented by this EventProvider and is called from the current
thread.
Note on concurrency: This method can safely be called from within a listening method during event handling to add a listener. This will have no impact on the current event delegation process.
add in interface ListenerStoreL - Type of the listener to add.listenerClass - The class representing the event(s) to listen on.listener - The listener to add.public <L extends Listener> void add(L listener)
ListenerStoreAdds the given object for all listener classes it implements. This recursively traverses super classes and super interfaces of the given listener's class. When encountering a super interface X which
Listener.class butListener.class andListenerKind.TAGGINGthen the given listener will be registered for X. Otherwise, all super interfaces of X are processed in the same way. When all super interfaces have been processed, the same process is recursively applied to the super class of the given listener if it has one. The "otherwise" implies, that the inheritance hierarchy of classes which have already been recognized as target listener class will not be traversed. If the same listener class is encountered multiple times while traversing the hierarchy, the listener will still only be registered once for that class.
Note that for each class the listener is being added its
onRegister method is
called.
add in interface ListenerStoreL - Type of the listener.listener - The listener to add.public <L extends Listener> void remove(L listener)
ListenerStoreListenerStore.add(Listener) to learn how these classes are collected from the
given listener. If the same listener has been added multiple times for
the same listener class, only one reference will be removed. This is
compatible with the definition of ListenerStore.remove(Class, Listener), which
also only removes one reference of the listener.
Note that for each class the listener is being removed its
onUnregister method is
called.
remove in interface ListenerStoreL - Type of the listener.listener - The listener to remove.public <L extends Listener> void remove(Class<L> listenerClass, L listener)
ListenerStoreonUnregister
method gets called to notify the listener about being removed from a
parent. The onUnregister method is not subject to the dispatching
strategy implemented by this EventProvider and is called from the
current thread.
If any of the arguments is null, this method returns with no
effect.
Note on concurrency: This method can safely be called from within a listening method during event handling to remove a listener. This will have no impact on the current event delegation process.
remove in interface ListenerStoreL - Type of the listener to remove.listenerClass - The class representing the event(s) for which the
listener should be removed.listener - The listener to remove.public <L extends Listener> Stream<L> get(Class<L> listenerClass)
ListenerStoreListenerStore.add(Class, Listener) for the given listener class.
Note on concurrency: The returned Stream will not be backed by the
internal list which is also modified via ListenerStore.add(Class, Listener) or
ListenerStore.remove(Class, Listener). This allows to add or remove listeners
to this store while the Stream is being iterated.
get in interface ListenerStoreL - Type of the listeners to return.listenerClass - The class representing the event for which the
listeners should be retrieved.public <L extends Listener> void clearAll(Class<L> listenerClass)
ListenerStoreonUnregister method will
be called. The actual order in which listeners are removed is undefined.
If listenerClass is null this method returns with no effect.
Note on concurrency: This method can safely be called from within a listening method during event handling to remove listeners. This will have no impact on the current event delegation process.
clearAll in interface ListenerStoreL - Type of the listeners to remove.listenerClass - The class representing the event for which the
listeners should be removedpublic void clearAll()
ListenerStoreonUnregister method will
be called. The actual order in which listeners are removed is undefined.
Note on concurrency: This method can safely be called from within a listening method during event handling to remove all listeners. This will have no impact on the current event delegation process.
clearAll in interface ListenerStorepublic boolean isSequential()
ListenerStoreListenerStore.get(Class)
returns the registered Listeners in FIFO order regarding their time of
registration.isSequential in interface ListenerStorepublic void close()
ListenerStoreListener.onUnregister(RegistrationEvent) will be called on each
Listener). Implementors may also release additional resource held by
their implementations.
Note: This method is automatically called upon closing an
EventProvider which uses this store.
close in interface ListenerStoreCopyright © 2014–2015. All rights reserved.