public class Future<E> extends java.lang.Object implements IFuture<E>, IForwardCommandFuture
IForwardCommandFuture.Type
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
CALLER_QUEUED
A caller is queued for suspension.
|
protected static java.lang.String |
CALLER_RESUMED
A caller is resumed.
|
protected static java.lang.String |
CALLER_SUSPENDED
A caller is suspended.
|
protected java.util.Map<ISuspendable,java.lang.String> |
callers
The blocked callers (caller->state).
|
protected java.lang.Exception |
creation
For capturing call stack of future creation.
|
static boolean |
DEBUG
Debug flag.
|
static IFuture<?> |
EMPTY
The empty future.
|
protected java.lang.Exception |
exception
The exception (if any).
|
protected java.util.Map<ICommand<java.lang.Object>,IFilter<java.lang.Object>> |
fcommands
The list of commands.
|
protected java.lang.Exception |
first
For capturing call stack of first setResult/Exception call.
|
protected IResultListener<E> |
listener
The first listener (for avoiding array creation).
|
protected java.util.List<IResultListener<E>> |
listeners
The listeners.
|
static boolean |
NO_STACK_COMPACTION
Disable Stack unfolding for easier debugging.
|
static long |
NONE
Constant for no timeout.
|
protected E |
result
The result.
|
protected boolean |
resultavailable
Flag indicating if result is available.
|
static java.lang.ThreadLocal<java.util.List<Tuple2<Future<?>,IResultListener<?>>>> |
STACK
Notification stack for unwinding call stack to topmost future.
|
protected boolean |
undone
The undone flag.
|
static long |
UNSET
Constant for unset.
|
Constructor and Description |
---|
Future()
Create a new future.
|
Future(E result)
Create a future that is already done.
|
Future(java.lang.Exception exception)
Create a future that is already failed.
|
Modifier and Type | Method and Description |
---|---|
void |
abortGet(ISuspendable caller)
Abort a blocking get call.
|
IFuture<java.lang.Void> |
acceptEither(IFuture<E> other,
Consumer<E> action,
java.lang.Class<?> futuretype)
The given consumer will be executed with either of the result of this and the given other future.
|
void |
addForwardCommand(IFilter<java.lang.Object> filter,
ICommand<java.lang.Object> command)
Add a forward command with a filter.
|
void |
addResultListener(IFunctionalResultListener<E> sucListener)
Add an functional result listener, which is only called on success.
|
void |
addResultListener(IFunctionalResultListener<E> sucListener,
IFunctionalExceptionListener exListener)
Add a result listener by combining an OnSuccessListener and an
OnExceptionListener.
|
void |
addResultListener(IResultListener<E> listener)
Add a result listener.
|
<U> IFuture<U> |
applyToEither(IFuture<E> other,
Function<E,U> fn,
java.lang.Class<?> futuretype)
The given function will be executed with either of the result of this and the given other future.
|
protected void |
doSetResult(E result)
Set the result without notifying listeners.
|
protected boolean |
doSetResultIfUndone(E result)
Set the result without notifying listeners.
|
E |
get()
Get the result - blocking call.
|
E |
get(long timeout)
Get the result - blocking call.
|
static <T> IFuture<T> |
getEmptyFuture()
Get the empty future of some type.
|
java.lang.Exception |
getException()
Get the exception, if any.
|
<T> Future<T> |
getFuture(java.lang.Class<?> futuretype)
Sequential execution of async methods via implicit delegation.
|
boolean |
hasResultListener()
Check, if the future has at least one listener.
|
boolean |
isDone()
Test if done, i.e.
|
protected void |
notifyListener(IResultListener<E> listener)
Notify a result listener.
|
protected void |
notifyListenerCommand(IResultListener<E> listener,
java.lang.Object command)
Notify the command listeners.
|
void |
removeForwardCommand(ICommand<java.lang.Object> command)
Add a command with a filter.
|
protected void |
resume()
Resume after result or exception has been set.
|
void |
sendForwardCommand(java.lang.Object command)
Send a (forward) command to the listeners.
|
void |
setException(java.lang.Exception exception)
Set the exception.
|
boolean |
setExceptionIfUndone(java.lang.Exception exception)
Set the exception.
|
void |
setResult(E result)
Set the result.
|
boolean |
setResultIfUndone(E result)
Set the result.
|
IFuture<java.lang.Void> |
thenAccept(Consumer<? super E> consumer)
Applies a function consuming the result after it is available.
|
IFuture<java.lang.Void> |
thenAccept(Consumer<? super E> consumer,
java.lang.Class<?> futuretype)
Applies a function consuming the result after it is available.
|
<T> IFuture<T> |
thenApply(Function<? super E,? extends T> function)
Applies a function after the result is available, using the result of this Future as input.
|
<T> IFuture<T> |
thenApply(Function<? super E,? extends T> function,
java.lang.Class<?> futuretype)
Applies a function after the result is available, using the result of this Future as input.
|
<U,V> IFuture<V> |
thenCombine(IFuture<U> other,
BiFunction<? super E,? super U,? extends V> function,
java.lang.Class<?> futuretype)
Combines this and another future and uses the given bifunction to calculate the result.
|
<T> IFuture<T> |
thenCompose(Function<? super E,IFuture<T>> function)
The result of this future is delegated to the given function.
|
<T> IFuture<T> |
thenCompose(Function<? super E,IFuture<T>> function,
java.lang.Class<?> futuretype)
The result of this future is delegated to the given function.
|
public static final java.lang.ThreadLocal<java.util.List<Tuple2<Future<?>,IResultListener<?>>>> STACK
protected static final java.lang.String CALLER_QUEUED
protected static final java.lang.String CALLER_RESUMED
protected static final java.lang.String CALLER_SUSPENDED
public static boolean DEBUG
public static boolean NO_STACK_COMPACTION
public static final IFuture<?> EMPTY
public static final long NONE
public static final long UNSET
protected E result
protected java.lang.Exception exception
protected boolean resultavailable
protected java.util.Map<ISuspendable,java.lang.String> callers
protected IResultListener<E> listener
protected java.util.List<IResultListener<E>> listeners
protected java.lang.Exception creation
protected java.lang.Exception first
protected boolean undone
public Future()
public Future(E result)
result
- The result, if any.public Future(java.lang.Exception exception)
exception
- The exception.public static <T> IFuture<T> getEmptyFuture()
public boolean isDone()
public java.lang.Exception getException()
getException
in interface IFuture<E>
public E get()
public E get(long timeout)
public void setException(java.lang.Exception exception)
exception
- The exception.public boolean setExceptionIfUndone(java.lang.Exception exception)
exception
- The exception.public void setResult(E result)
result
- The result.protected void doSetResult(E result)
public boolean setResultIfUndone(E result)
result
- The result.protected boolean doSetResultIfUndone(E result)
protected void resume()
public void abortGet(ISuspendable caller)
public void addResultListener(IFunctionalResultListener<E> sucListener)
addResultListener
in interface IFuture<E>
sucListener
- The listener.public void addResultListener(IFunctionalResultListener<E> sucListener, IFunctionalExceptionListener exListener)
addResultListener
in interface IFuture<E>
sucListener
- The listener that is called on success.exListener
- The listener that is called on exceptions. Passing
null
enables default exception logging.public void addResultListener(IResultListener<E> listener)
addResultListener
in interface IFuture<E>
listener
- The listener.protected void notifyListener(IResultListener<E> listener)
listener
- The listener.public void sendForwardCommand(java.lang.Object command)
sendForwardCommand
in interface IForwardCommandFuture
command
- The command.protected void notifyListenerCommand(IResultListener<E> listener, java.lang.Object command)
listener
- The listener.command
- The command.public void addForwardCommand(IFilter<java.lang.Object> filter, ICommand<java.lang.Object> command)
public void removeForwardCommand(ICommand<java.lang.Object> command)
public boolean hasResultListener()
public <T> IFuture<T> thenApply(Function<? super E,? extends T> function)
IFuture
public <T> IFuture<T> thenApply(Function<? super E,? extends T> function, java.lang.Class<?> futuretype)
IFuture
public <T> IFuture<T> thenCompose(Function<? super E,IFuture<T>> function)
IFuture
thenCompose
in interface IFuture<E>
function
- Function that takes the result of this future as input and delivers future(t).public <T> IFuture<T> thenCompose(Function<? super E,IFuture<T>> function, java.lang.Class<?> futuretype)
IFuture
thenCompose
in interface IFuture<E>
function
- Function that takes the result of this future as input and delivers future(t).futuretype
- The type of the return future. If null, a default future is created.public IFuture<java.lang.Void> thenAccept(Consumer<? super E> consumer)
IFuture
thenAccept
in interface IFuture<E>
consumer
- Consumer that takes the result of this future as input and consumes it.public IFuture<java.lang.Void> thenAccept(Consumer<? super E> consumer, java.lang.Class<?> futuretype)
IFuture
thenAccept
in interface IFuture<E>
consumer
- Consumer that takes the result of this future as input and consumes it.futuretype
- The type of the return future. If null, a default future is created.public <U,V> IFuture<V> thenCombine(IFuture<U> other, BiFunction<? super E,? super U,? extends V> function, java.lang.Class<?> futuretype)
IFuture
thenCombine
in interface IFuture<E>
function
- BiFunction that takes the result of this and given other future as input and produces output.futuretype
- The type of the return future. If null, a default future is created.public <U> IFuture<U> applyToEither(IFuture<E> other, Function<E,U> fn, java.lang.Class<?> futuretype)
IFuture
applyToEither
in interface IFuture<E>
other
- other futurefn
- function to receive resultfuturetype
- The type of the return future. If null, a default future is created.public IFuture<java.lang.Void> acceptEither(IFuture<E> other, Consumer<E> action, java.lang.Class<?> futuretype)
IFuture
acceptEither
in interface IFuture<E>
other
- other futurefuturetype
- The type of the return future. If null, a default future is created.public <T> Future<T> getFuture(java.lang.Class<?> futuretype)
futuretype
- The type of the result future (cannot be automatically determined).