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.
|
protected java.lang.Exception |
exception
The exception (if any).
|
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 ICommand<IResultListener<E>> |
notcommand |
protected java.util.Queue<Tuple2<IResultListener<E>,ICommand<IResultListener<E>>>> |
notifications
The scheduled notifications.
|
protected E |
result
The result.
|
protected boolean |
resultavailable
Flag indicating if result is available.
|
static java.lang.ThreadLocal<java.util.Queue<Tuple3<Future<?>,IResultListener<?>,ICommand<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 |
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.
|
void |
delegate(Future<E> delegate) |
protected boolean |
doSetException(java.lang.Exception exception,
boolean undone)
Set the exception (internal implementation for normal and if-undone).
|
protected boolean |
doSetResult(E result,
boolean undone)
Set the result without notifying listeners.
|
IFuture<E> |
exceptionally(Function<? super java.lang.Exception,IFuture<E>> function) |
IFuture<E> |
exceptionally(Function<? super java.lang.Exception,IFuture<E>> function,
java.lang.Class<?> futuretype) |
<T> void |
exceptionally(Future<T> delegate)
Sequential execution of async methods via implicit delegation.
|
protected void |
executeNotification(IResultListener<E> listener,
ICommand<IResultListener<E>> command)
Execute a notification.
|
E |
get()
Get the result - blocking call.
|
E |
get(boolean realtime)
Get the result - blocking call.
|
E |
get(long timeout)
Get the result - blocking call.
|
E |
get(long timeout,
boolean realtime)
Get the result - blocking call.
|
E |
get(ThreadSuspendable sus)
Deprecated.
- From 3.0. Use the version without suspendable.
Will NOT use the suspendable given as parameter.
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.
|
protected ICommand<IResultListener<E>> |
getNotificationCommand()
Get the notification command.
|
boolean |
hasResultListener()
Check, if the future has at least one listener.
|
boolean |
isDone()
Test if done, i.e.
|
protected void |
notifyListener()
Notify all result listeners of the finished future (result or exception).
|
protected void |
notifyListener(IResultListener<E> listener)
Notify a specific result listener of the finished future (result or exception).
|
protected void |
resume()
Resume after result or exception has been set.
|
protected void |
scheduleNotification(ICommand<IResultListener<E>> command)
Schedule a notification for all listeners.
|
protected void |
scheduleNotification(IFilter<IResultListener<E>> filter,
ICommand<IResultListener<E>> command)
Schedule a listener notification.
|
protected void |
scheduleNotification(IResultListener<E> listener,
ICommand<IResultListener<E>> command)
Schedule a listener notification for a specific listener.
|
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.
|
protected void |
startScheduledNotifications()
Start scheduled listener notifications.
|
IFuture<java.lang.Void> |
thenAccept(Consumer<? super E> consumer)
Applies a synchronous 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 (future-returning) 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 (future-returning) function.
|
public static final java.lang.ThreadLocal<java.util.Queue<Tuple3<Future<?>,IResultListener<?>,ICommand<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 long NONE
public static final long UNSET
protected E result
protected java.lang.Exception exception
protected volatile 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
protected java.util.Queue<Tuple2<IResultListener<E>,ICommand<IResultListener<E>>>> notifications
protected ICommand<IResultListener<E>> notcommand
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(ThreadSuspendable sus)
public E get()
public E get(boolean realtime)
public E get(long timeout)
public E get(long timeout, boolean realtime)
protected boolean doSetException(java.lang.Exception exception, boolean undone)
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.public boolean setResultIfUndone(E result)
result
- The result.protected boolean doSetResult(E result, boolean undone)
protected void resume()
public void abortGet(ISuspendable caller)
protected void scheduleNotification(IFilter<IResultListener<E>> filter, ICommand<IResultListener<E>> command)
filter
- Optional filter to select only specific listener (e.g. for forward commands). Otherwise uses all listeners.command
- The notification command to execute for each selected listener.protected void scheduleNotification(ICommand<IResultListener<E>> command)
command
- The notification command to execute for each currently registered listener.protected void scheduleNotification(IResultListener<E> listener, ICommand<IResultListener<E>> command)
listener
- The listener to notify.command
- The notification command to execute for the listener.protected final void startScheduledNotifications()
protected void executeNotification(IResultListener<E> listener, ICommand<IResultListener<E>> command)
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()
protected void notifyListener(IResultListener<E> listener)
listener
- The listener.protected ICommand<IResultListener<E>> getNotificationCommand()
public void sendForwardCommand(java.lang.Object command)
sendForwardCommand
in interface IForwardCommandFuture
command
- The 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> void exceptionally(Future<T> delegate)
exceptionally
in interface IFuture<E>
function
- Function that takes the result of this future as input and delivers future(t).ret
- Thepublic IFuture<E> exceptionally(Function<? super java.lang.Exception,IFuture<E>> function)
exceptionally
in interface IFuture<E>
public IFuture<E> exceptionally(Function<? super java.lang.Exception,IFuture<E>> function, java.lang.Class<?> futuretype)
exceptionally
in interface IFuture<E>
public <T> Future<T> getFuture(java.lang.Class<?> futuretype)
futuretype
- The type of the result future (cannot be automatically determined).