net.tomp2p.futures
Class BaseFutureImpl

java.lang.Object
  extended by net.tomp2p.futures.BaseFutureImpl
All Implemented Interfaces:
BaseFuture, Cancellable
Direct Known Subclasses:
FutureChannelCreator, FutureDHT, FutureDiscover, FutureForkJoin, FutureLateJoin, FutureLaterJoin, FutureMap, FutureMapReduce, FutureReduce, FutureResponse, FutureRouting, FutureTracker, FutureWrapper

public abstract class BaseFutureImpl
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()
           
 
Method Summary
 BaseFuture addCancellation(Cancellable cancellable)
          Adds a cancel listener to this future, which is called when cancel is executed.
 BaseFuture addListener(BaseFutureListener<? extends BaseFuture> listener)
          Adds a listener which is notified when the state of this future changes.
 BaseFuture await()
          Wait for the asynchronous operation to end.
 boolean await(long timeoutMillis)
          Wait for the asynchronous operation to end with the specified timeout.
 BaseFuture 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)!
 BaseFuture removeCancellation(Cancellable cancellable)
          Remove a listener.
 BaseFuture removeListener(BaseFutureListener<? extends BaseFuture> listener)
          Removes a listener which is notified when the state of this future changes.
protected  boolean setCompletedAndNotify()
          Make sure that the calling method has synchronized (lock)
 void setFailed(BaseFuture origin)
          Sets the failed flat to true and the completed flag to true.
 void setFailed(String reason)
          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()
Method Detail

await

public BaseFuture 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 BaseFuture 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
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 void 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

setFailed

public void setFailed(String reason)
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

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.

addListener

public BaseFuture 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

notifyListerenrs

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


removeListener

public BaseFuture 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 BaseFuture 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

removeCancellation

public BaseFuture removeCancellation(Cancellable cancellable)
Description copied from interface: BaseFuture
Remove a listener. After a future is completed, all cancellables are removed. 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 anyway.

Specified by:
removeCancellation in interface BaseFuture
Parameters:
cancellable - A cancellable class

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 © 2012. All Rights Reserved.