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:
java.util.concurrent.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>
TheAbstractListenableFuture
implements common functionality shared by all futures that implement theListenableFuture
. 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 bothaddToListeners(GenericCompletionListener)
andremoveFromListeners(GenericCompletionListener)
. See theOperationFuture
as an example.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<GenericCompletionListener<? extends java.util.concurrent.Future<T>>>
listeners
Holds the list of listeners which will be notified upon completion.private java.util.concurrent.ExecutorService
service
TheExecutorService
in which the notifications will be handled.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractListenableFuture(java.util.concurrent.ExecutorService executor)
Creates a newAbstractListenableFuture
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.util.concurrent.Future<T>
addToListeners(GenericCompletionListener<? extends java.util.concurrent.Future<T>> listener)
Add the given listener to the total list of listeners to be notified.protected java.util.concurrent.ExecutorService
executor()
Returns the current executor.protected void
notifyListener(java.util.concurrent.ExecutorService executor, java.util.concurrent.Future<?> future, GenericCompletionListener listener)
Notify a specific listener of completion.protected void
notifyListeners()
Notify all registered listeners of future completion.protected void
notifyListeners(java.util.concurrent.Future<?> future)
Notify all registered listeners with a special future on completion.protected java.util.concurrent.Future<T>
removeFromListeners(GenericCompletionListener<? extends java.util.concurrent.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 net.spy.memcached.internal.ListenableFuture
addListener, removeListener
-
-
-
-
Field Detail
-
service
private final java.util.concurrent.ExecutorService service
TheExecutorService
in which the notifications will be handled.
-
listeners
private java.util.List<GenericCompletionListener<? extends java.util.concurrent.Future<T>>> listeners
Holds the list of listeners which will be notified upon completion.
-
-
Constructor Detail
-
AbstractListenableFuture
protected AbstractListenableFuture(java.util.concurrent.ExecutorService executor)
Creates a newAbstractListenableFuture
.- Parameters:
executor
- the executor in which the callbacks will be executed in.
-
-
Method Detail
-
executor
protected java.util.concurrent.ExecutorService executor()
Returns the current executor.- Returns:
- the current executor service.
-
addToListeners
protected java.util.concurrent.Future<T> addToListeners(GenericCompletionListener<? extends java.util.concurrent.Future<T>> listener)
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(java.util.concurrent.ExecutorService executor, java.util.concurrent.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
protected void notifyListeners(java.util.concurrent.Future<?> future)
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
protected java.util.concurrent.Future<T> removeFromListeners(GenericCompletionListener<? extends java.util.concurrent.Future<T>> listener)
Remove a listener from the list of registered listeners.- Parameters:
listener
- the listener to remove.- Returns:
- the current future to allow for chaining.
-
-