Package net.spy.memcached.internal
Class AbstractListenableFuture<T,L extends GenericCompletionListener>
java.lang.Object
net.spy.memcached.compat.SpyObject
net.spy.memcached.internal.AbstractListenableFuture<T,L>
- All Implemented Interfaces:
Future<T>
,ListenableFuture<T,
L>
- Direct Known Subclasses:
BulkGetFuture
,GetFuture
,OperationFuture
public abstract class AbstractListenableFuture<T,L extends GenericCompletionListener>
extends SpyObject
implements ListenableFuture<T,L>
The
AbstractListenableFuture
implements common functionality shared
by all futures that implement the ListenableFuture
.
It handles storage and notification of listeners and making sure this is
done in a thread-safe manner. The subclassing methods need to implement
the abstract methods, which in turn need to call both
addToListeners(GenericCompletionListener)
and
removeFromListeners(GenericCompletionListener)
. See the
OperationFuture
as an example.-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.concurrent.Future
Future.State
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate List
<GenericCompletionListener<? extends Future<T>>> Holds the list of listeners which will be notified upon completion.private final ExecutorService
TheExecutorService
in which the notifications will be handled. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractListenableFuture
(ExecutorService executor) Creates a newAbstractListenableFuture
. -
Method Summary
Modifier and TypeMethodDescriptionaddToListeners
(GenericCompletionListener<? extends Future<T>> listener) Add the given listener to the total list of listeners to be notified.protected ExecutorService
executor()
Returns the current executor.protected void
notifyListener
(ExecutorService executor, Future<?> future, GenericCompletionListener listener) Notify a specific listener of completion.protected void
Notify all registered listeners of future completion.protected void
notifyListeners
(Future<?> future) Notify all registered listeners with a special future on completion.removeFromListeners
(GenericCompletionListener<? extends Future<T>> listener) Remove a listener from the list of registered listeners.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.concurrent.Future
cancel, exceptionNow, get, get, isCancelled, isDone, resultNow, state
Methods inherited from interface net.spy.memcached.internal.ListenableFuture
addListener, removeListener
-
Field Details
-
service
TheExecutorService
in which the notifications will be handled. -
listeners
Holds the list of listeners which will be notified upon completion.
-
-
Constructor Details
-
AbstractListenableFuture
Creates a newAbstractListenableFuture
.- Parameters:
executor
- the executor in which the callbacks will be executed in.
-
-
Method Details
-
executor
Returns the current executor.- Returns:
- the current executor service.
-
addToListeners
Add the given listener to the total list of listeners to be notified.If the future is already done, the listener will be notified immediately.
- Parameters:
listener
- the listener to add.- Returns:
- the current future to allow chaining.
-
notifyListener
protected void notifyListener(ExecutorService executor, Future<?> future, GenericCompletionListener listener) Notify a specific listener of completion.- Parameters:
executor
- the executor to use.future
- the future to hand over.listener
- the listener to notify.
-
notifyListeners
protected void notifyListeners()Notify all registered listeners of future completion. -
notifyListeners
Notify all registered listeners with a special future on completion. This method can be used if a different future should be used for notification than the current one (for example if an enclosing future is used, but the enclosed future should be notified on).- Parameters:
future
- the future to pass on to the listeners.
-
removeFromListeners
Remove a listener from the list of registered listeners.- Parameters:
listener
- the listener to remove.- Returns:
- the current future to allow for chaining.
-