net.tomp2p.futures
Interface BaseFuture

All Superinterfaces:
Cancellable
All Known Subinterfaces:
FutureBootstrap
All Known Implementing Classes:
BaseFutureImpl, FutureAsyncTask, FutureChannel, FutureChannelCreator, FutureDHT, FutureDiscover, FutureForkJoin, FutureLateJoin, FutureLaterJoin, FutureResponse, FutureRouting, FutureTask, FutureTracker, FutureWrappedBootstrap, FutureWrapper

public interface BaseFuture
extends Cancellable

Represents the result of an asynchronous operation.

Author:
Thomas Bocek

Nested Class Summary
static class BaseFuture.FutureType
          The first state is always INIT and will always end in either OK, FAILED, or CANCEl
 
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 addListener(BaseFutureListener<? extends BaseFuture> listener, boolean last)
          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 awaitListeners()
          Waits until all the listener finished.
 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.
 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).
 BaseFuture removeListener(BaseFutureListener<? extends BaseFuture> listener)
          Removes a listener which is notified when the state of this future changes.
 BaseFuture setFailed(BaseFuture origin)
          Sets the failed flat to true and the completed flag to true.
 BaseFuture setFailed(String reason)
          Sets the failed flat to true and the completed flag to true.
 BaseFuture setFailed(String reason, BaseFuture origin)
          Sets the failed flat to true and the completed flag to true.
 BaseFuture setFailed(String reason, Throwable t)
          Sets the failed flat to true and the completed flag to true.
 BaseFuture setFailed(Throwable t)
          Sets the failed flat to true and the completed flag to true.
 
Methods inherited from interface net.tomp2p.futures.Cancellable
cancel
 

Method Detail

await

BaseFuture await()
                 throws InterruptedException
Wait for the asynchronous operation to end.

Returns:
this
Throws:
InterruptedException - if thread is interrupted

await

boolean await(long timeoutMillis)
              throws InterruptedException
Wait for the asynchronous operation to end with the specified timeout.

Parameters:
timeoutMillis - time to wait at most
Returns:
true if the operation is finished.
Throws:
InterruptedException - if thread is interrupted

awaitUninterruptibly

BaseFuture awaitUninterruptibly()
Wait for the asynchronous operation to end without interruption.

Returns:
this

awaitUninterruptibly

boolean awaitUninterruptibly(long timeoutMillis)
Wait for the asynchronous operation to end with the specified timeout without interruption.

Parameters:
timeoutMillis - to wait at most
Returns:
true if the operation is finished.

isCompleted

boolean isCompleted()
Checks if the asynchronous operation is finished.

Returns:
true if operation is finished

isSuccess

boolean isSuccess()
Returns the opposite of isFailed (returns !isFailed). Use this method if you are an optimist ;) otherwise use isFailed

Returns:
true if operation succeeded, false if there was no reply

isFailed

boolean isFailed()
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.

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

BaseFuture setFailed(String reason)
Sets the failed flat to true and the completed flag to true. This will notify listeners and set the reason

Parameters:
reason - The reason of failure
Returns:
this

setFailed

BaseFuture setFailed(BaseFuture origin)
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.

Parameters:
origin - The origin of failure
Returns:
this

setFailed

BaseFuture setFailed(String reason,
                     BaseFuture origin)
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.

Parameters:
reason - The reason of failure
origin - The origin of failure
Returns:
this

setFailed

BaseFuture setFailed(Throwable t)
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.

Parameters:
t - The stack trace where the failure happened
Returns:
this

setFailed

BaseFuture setFailed(String reason,
                     Throwable t)
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.

Parameters:
reason - The reason of failure
t - The stack trace where the failure happened
Returns:
this

getFailedReason

String getFailedReason()
The default failed reason is Unknown.

Returns:
Returns the reason why a future failed.

getType

BaseFuture.FutureType getType()
If the type is not OK, then something unexpected happened.

Returns:
The fail type

awaitListeners

BaseFuture awaitListeners()
                          throws InterruptedException
Waits until all the listener finished. This may include the release of resources.

Returns:
this
Throws:
InterruptedException - If interrupted from outside

addListener

BaseFuture addListener(BaseFutureListener<? extends BaseFuture> listener)
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.

Parameters:
listener - The listener extends the BaseFuture
Returns:
this

addListener

BaseFuture addListener(BaseFutureListener<? extends BaseFuture> listener,
                       boolean last)
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.

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

removeListener

BaseFuture removeListener(BaseFutureListener<? extends BaseFuture> listener)
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.

Parameters:
listener - The listener extends the BaseFuture
Returns:
this

addCancellation

BaseFuture addCancellation(Cancellable cancellable)
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.

Parameters:
cancellable - A cancellable class
Returns:
this


Copyright © 2013. All Rights Reserved.