barchart-util-concurrent
2.0.16 / 2012-12-19T12:07:36.441-0600

com.barchart.util.concurrent
Class FutureNotifierBase<V,T extends FutureCallback<V,T>>

java.lang.Object
  extended by com.barchart.util.concurrent.FutureNotifierBase<V,T>
Type Parameters:
V - The result type
T - The subclass being defined (for return value parameterization)
All Implemented Interfaces:
FutureCallback<V,T>, FutureListener<V>, Future<V>
Direct Known Subclasses:
CancellableFutureNotifier, FutureNotifier

public class FutureNotifierBase<V,T extends FutureCallback<V,T>>
extends Object
implements FutureCallback<V,T>, FutureListener<V>

An implementation of FutureCallback that does not do any actual computation, but just serves as a communication proxy between the executor and listeners. This is a base class suitable for subclassing to return the correct object type in succeed() and fail(). Ad-hoc notifiers can use the FutureNotifier subclass to avoid additional parameterization.


Constructor Summary
FutureNotifierBase()
          Creates a FutureNotifier that notifies any callbacks upon completion.
FutureNotifierBase(Thread runner)
          Creates a FutureNotifier that notifies any callbacks upon completion.
 
Method Summary
 T addResultListener(FutureListener<V> listener)
          Register a listener for this deferred result.
 boolean cancel(boolean mayInterruptIfRunning)
           
protected  void done()
          Protected method invoked when this task transitions to state isDone (whether normally or via cancellation).
 T fail(Throwable error)
          Notify listeners that an error occurred.
 V get()
           
 V get(long timeout, TimeUnit unit)
           
 V getUnchecked()
          Get the return value, or null if a checked exception was caught.
 boolean isCancelled()
           
 boolean isDone()
           
 void resultAvailable(Future<V> result)
          Result listener for chaining callbacks together.
protected  void set(V v)
          Sets the result of this Future to the given value unless this future has already been set or has been cancelled.
protected  void setException(Throwable t)
          Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been cancelled.
 T succeed(V result)
          Notify listeners that a result is available.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FutureNotifierBase

public FutureNotifierBase()
Creates a FutureNotifier that notifies any callbacks upon completion.


FutureNotifierBase

public FutureNotifierBase(Thread runner)
Creates a FutureNotifier that notifies any callbacks upon completion. If a runner thread is specified here, cancel() will attempt to interrupt it if requested.

Method Detail

isCancelled

public boolean isCancelled()
Specified by:
isCancelled in interface Future<V>

isDone

public boolean isDone()
Specified by:
isDone in interface Future<V>

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Specified by:
cancel in interface Future<V>

get

public V get()
      throws InterruptedException,
             ExecutionException
Specified by:
get in interface Future<V>
Throws:
CancellationException
InterruptedException
ExecutionException

getUnchecked

public V getUnchecked()
Description copied from interface: FutureCallback
Get the return value, or null if a checked exception was caught.

Specified by:
getUnchecked in interface FutureCallback<V,T extends FutureCallback<V,T>>

get

public V get(long timeout,
             TimeUnit unit)
      throws InterruptedException,
             ExecutionException,
             TimeoutException
Specified by:
get in interface Future<V>
Throws:
CancellationException
InterruptedException
ExecutionException
TimeoutException

resultAvailable

public void resultAvailable(Future<V> result)
Result listener for chaining callbacks together.

Specified by:
resultAvailable in interface FutureListener<V>

done

protected void done()
Protected method invoked when this task transitions to state isDone (whether normally or via cancellation). The default implementation does nothing. Subclasses may override this method to invoke completion callbacks or perform bookkeeping. Note that you can query status inside the implementation of this method to determine whether this task has been cancelled.


set

protected void set(V v)
Sets the result of this Future to the given value unless this future has already been set or has been cancelled. This method is invoked internally by the run method upon successful completion of the computation.

Parameters:
v - the value

setException

protected void setException(Throwable t)
Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been cancelled. This method is invoked internally by the run method upon failure of the computation.

Parameters:
t - the cause of failure

addResultListener

public T addResultListener(FutureListener<V> listener)
Description copied from interface: FutureCallback
Register a listener for this deferred result. If the result is already available, the listener may be called synchronously, so you should generally avoid any blocking code in the callback.

Specified by:
addResultListener in interface FutureCallback<V,T extends FutureCallback<V,T>>
Parameters:
listener - The listener object

fail

public T fail(Throwable error)
Description copied from interface: FutureCallback
Notify listeners that an error occurred. Returns the current FutureCallbackTask instance to allow simple synchronous returns when an error has already occurred:
return new FutureCallbackTask().fail(exception);

Specified by:
fail in interface FutureCallback<V,T extends FutureCallback<V,T>>

succeed

public T succeed(V result)
Description copied from interface: FutureCallback
Notify listeners that a result is available. Returns the current FutureCallbackTask instance to allow simple synchronous returns when the result is already available:
return new FutureCallbackTask().succeed(result);

Specified by:
succeed in interface FutureCallback<V,T extends FutureCallback<V,T>>
Returns:
This FutureCallbackTask object (for chaining calls)

barchart-util-concurrent
2.0.16 / 2012-12-19T12:07:36.441-0600

Copyright © 2011-2012 Barchart, Inc.. All Rights Reserved.