net.tomp2p.futures
Class BaseFutureImpl<K extends BaseFuture>

java.lang.Object
  extended by net.tomp2p.futures.BaseFutureImpl<K>
Type Parameters:
K - The class that extends BaseFuture and is used to return back the type for method calls. E.g, if K is FutureDHT await() returns FutureDHT.
All Implemented Interfaces:
BaseFuture, Cancellable
Direct Known Subclasses:
FutureAsyncTask, FutureChannel, FutureChannelCreator, FutureDHT, FutureDiscover, FutureForkJoin, FutureLateJoin, FutureLaterJoin, FutureResponse, FutureRouting, FutureTask, FutureTracker, FutureWrapper

public abstract class BaseFutureImpl<K extends BaseFuture>
extends Object
implements BaseFuture

The base for all BaseFuture implementations. Be aware of possible deadlocks. Never await from a listener. This class is heavily inspired by MINA and Netty.

Author:
Thomas Bocek

Nested Class Summary
 
Nested classes/interfaces inherited from interface net.tomp2p.futures.BaseFuture
BaseFuture.FutureType
 
Field Summary
protected  boolean completed
           
protected  Object lock
           
protected  String reason
           
protected  BaseFuture.FutureType type
           
 
Constructor Summary
BaseFutureImpl()
          Default constructor that sets the lock object, which is used for synchronization to this instance.
 
Method Summary
 K addCancellation(Cancellable cancellable)
          Adds a cancel listener to this future, which is called when cancel is executed.
 K addListener(BaseFutureListener<? extends BaseFuture> listener)
          Adds a listener which is notified when the state of this future changes.
 K addListener(BaseFutureListener<? extends BaseFuture> listener, boolean last)
          Adds a listener which is notified when the state of this future changes.
 K await()
          Wait for the asynchronous operation to end.
 boolean await(long timeoutMillis)
          Wait for the asynchronous operation to end with the specified timeout.
 K awaitListeners()
          Waits until all the listener finished.
 K awaitUninterruptibly()
          Wait for the asynchronous operation to end without interruption.
 boolean awaitUninterruptibly(long timeoutMillis)
          Wait for the asynchronous operation to end with the specified timeout without interruption.
 void cancel()
          This method gets called if a future is cancelled.
 String getFailedReason()
          The default failed reason is Unknown.
 BaseFuture.FutureType getType()
          If the type is not OK, then something unexpected happened.
 boolean isCompleted()
          Checks if the asynchronous operation is finished.
 boolean isFailed()
          Checks if operation has failed.
 boolean isSuccess()
          Returns the opposite of isFailed (returns !isFailed).
protected  void notifyListerenrs()
          Always call this from outside synchronized(lock)!
 K removeListener(BaseFutureListener<? extends BaseFuture> listener)
          Removes a listener which is notified when the state of this future changes.
protected  void self(K self2)
           
protected  boolean setCompletedAndNotify()
          Make sure that the calling method has synchronized (lock)
 K setFailed(BaseFuture origin)
          Sets the failed flat to true and the completed flag to true.
 K setFailed(String failed)
          Sets the failed flat to true and the completed flag to true.
 K setFailed(String failed, BaseFuture origin)
          Sets the failed flat to true and the completed flag to true.
 K setFailed(String failed, Throwable t)
          Sets the failed flat to true and the completed flag to true.
 K setFailed(Throwable t)
          Sets the failed flat to true and the completed flag to true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lock

protected final Object lock

completed

protected boolean completed

type

protected BaseFuture.FutureType type

reason

protected String reason
Constructor Detail

BaseFutureImpl

public BaseFutureImpl()
Default constructor that sets the lock object, which is used for synchronization to this instance.

Method Detail

self

protected void self(K self2)
Parameters:
self2 - Set the type so that we are able to return it to the user. This is for making the API much more usable.

await

public K await()
                           throws InterruptedException
Description copied from interface: BaseFuture
Wait for the asynchronous operation to end.

Specified by:
await in interface BaseFuture
Returns:
this
Throws:
InterruptedException - if thread is interrupted

awaitUninterruptibly

public K awaitUninterruptibly()
Description copied from interface: BaseFuture
Wait for the asynchronous operation to end without interruption.

Specified by:
awaitUninterruptibly in interface BaseFuture
Returns:
this

await

public boolean await(long timeoutMillis)
              throws InterruptedException
Description copied from interface: BaseFuture
Wait for the asynchronous operation to end with the specified timeout.

Specified by:
await in interface BaseFuture
Parameters:
timeoutMillis - time to wait at most
Returns:
true if the operation is finished.
Throws:
InterruptedException - if thread is interrupted

awaitUninterruptibly

public boolean awaitUninterruptibly(long timeoutMillis)
Description copied from interface: BaseFuture
Wait for the asynchronous operation to end with the specified timeout without interruption.

Specified by:
awaitUninterruptibly in interface BaseFuture
Parameters:
timeoutMillis - to wait at most
Returns:
true if the operation is finished.

isCompleted

public boolean isCompleted()
Description copied from interface: BaseFuture
Checks if the asynchronous operation is finished.

Specified by:
isCompleted in interface BaseFuture
Returns:
true if operation is finished

isSuccess

public boolean isSuccess()
Description copied from interface: BaseFuture
Returns the opposite of isFailed (returns !isFailed). Use this method if you are an optimist ;) otherwise use isFailed

Specified by:
isSuccess in interface BaseFuture
Returns:
true if operation succeeded, false if there was no reply

isFailed

public boolean isFailed()
Description copied from interface: BaseFuture
Checks if operation has failed. As this is a P2P network, where peers can fail at any time, a failure is seen as a "normal" event. Thus, no exception is thrown.

Specified by:
isFailed in interface BaseFuture
Returns:
true if operation failed, which means the node did not reply. A get(key) operation on a node that does not have the key, returns false with this method as a response has been send.

setFailed

public K setFailed(BaseFuture origin)
Description copied from interface: BaseFuture
Sets the failed flat to true and the completed flag to true. This will notify listeners and set the reason based on the origin BaseFuture.

Specified by:
setFailed in interface BaseFuture
Parameters:
origin - The origin of failure
Returns:
this

setFailed

public K setFailed(String failed,
                   BaseFuture origin)
Description copied from interface: BaseFuture
Sets the failed flat to true and the completed flag to true. This will notify listeners and append the reason based on the origin BaseFuture.

Specified by:
setFailed in interface BaseFuture
Parameters:
failed - The reason of failure
origin - The origin of failure
Returns:
this

setFailed

public K setFailed(Throwable t)
Description copied from interface: BaseFuture
Sets the failed flat to true and the completed flag to true. This will notify listeners and append the reason based on the origin BaseFuture.

Specified by:
setFailed in interface BaseFuture
Parameters:
t - The stack trace where the failure happened
Returns:
this

setFailed

public K setFailed(String failed,
                   Throwable t)
Description copied from interface: BaseFuture
Sets the failed flat to true and the completed flag to true. This will notify listeners and append the reason based on the origin BaseFuture.

Specified by:
setFailed in interface BaseFuture
Parameters:
failed - The reason of failure
t - The stack trace where the failure happened
Returns:
this

setFailed

public K setFailed(String failed)
Description copied from interface: BaseFuture
Sets the failed flat to true and the completed flag to true. This will notify listeners and set the reason

Specified by:
setFailed in interface BaseFuture
Parameters:
failed - The reason of failure
Returns:
this

getFailedReason

public String getFailedReason()
Description copied from interface: BaseFuture
The default failed reason is Unknown.

Specified by:
getFailedReason in interface BaseFuture
Returns:
Returns the reason why a future failed.

getType

public BaseFuture.FutureType getType()
Description copied from interface: BaseFuture
If the type is not OK, then something unexpected happened.

Specified by:
getType in interface BaseFuture
Returns:
The fail type

setCompletedAndNotify

protected boolean setCompletedAndNotify()
Make sure that the calling method has synchronized (lock)

Returns:
True if notified. It will notify if completed is not set yet.

awaitListeners

public K awaitListeners()
                                    throws InterruptedException
Description copied from interface: BaseFuture
Waits until all the listener finished. This may include the release of resources.

Specified by:
awaitListeners in interface BaseFuture
Returns:
this
Throws:
InterruptedException - If interrupted from outside

addListener

public K addListener(BaseFutureListener<? extends BaseFuture> listener)
Description copied from interface: BaseFuture
Adds a listener which is notified when the state of this future changes. All notifications are performed in a thread, which means that this method returns immediately. If a future is complete, then all listeners are called and after that, the listener list is cleared, so there is no need to call removeListener if a future has been completed.

Specified by:
addListener in interface BaseFuture
Parameters:
listener - The listener extends the BaseFuture
Returns:
this

addListener

public K addListener(BaseFutureListener<? extends BaseFuture> listener,
                     boolean last)
Description copied from interface: BaseFuture
Adds a listener which is notified when the state of this future changes. All notifications are performed in a thread, which means that this method returns immediately. If a future is complete, then all listeners are called and after that, the listener list is cleared, so there is no need to call removeListener if a future has been completed. A flag decides if the listener is added at the end of the list or at the beginning.

Specified by:
addListener in interface BaseFuture
Parameters:
listener - The listener extends the BaseFuture
last - Set to true if the listener should be added at the end of the list, true if it should be added first
Returns:
this

notifyListerenrs

protected void notifyListerenrs()
Always call this from outside synchronized(lock)!


removeListener

public K removeListener(BaseFutureListener<? extends BaseFuture> listener)
Description copied from interface: BaseFuture
Removes a listener which is notified when the state of this future changes. If a future is complete, then all listeners are called and after that, the listener list is cleared, so there is no need to call removeListener if a future has been completed. The listener can be called from the caller thread if the future is already finished or from a different thread if the future is not ready yet.

Specified by:
removeListener in interface BaseFuture
Parameters:
listener - The listener extends the BaseFuture
Returns:
this

addCancellation

public K addCancellation(Cancellable cancellable)
Description copied from interface: BaseFuture
Adds a cancel listener to this future, which is called when cancel is executed. There is no need to call removeCancellation if a future has been completed, because the cancellable list is cleared after the future has been completed. An example usage for a cancelation is if a TCP connection is being created, but the user shuts down the peer.

Specified by:
addCancellation in interface BaseFuture
Parameters:
cancellable - A cancellable class
Returns:
this

cancel

public void cancel()
Description copied from interface: Cancellable
This method gets called if a future is cancelled.

Specified by:
cancel in interface Cancellable


Copyright © 2013. All Rights Reserved.