V
- the type of the valuepublic class CommandFuture<V> extends Object implements Future<V>
Basically, this class is a container for a server response which will arrive at some time in the future. It also accounts for the possibility that a command might fail and that a future might be cancelled by a user.
ACommandFuture
can therefore have 4 different states:
isDone()
,
isSuccessful()
, isFailed()
and isCancelled()
.
A CommandFuture
's value can be retrieved by calling get()
or get(long, TimeUnit)
, which block the current thread until the
server response arrives. The method with a timeout should be preferred
as there's no guarantee that a proper response (or an error message)
will ever arrive, e.g. in case of a permanent disconnect.
There are also variations of these methods which ignore thread interrupts,
getUninterruptibly()
and getUninterruptibly(long, TimeUnit)
.
Note that these methods all wait for the response to arrive and thereby revert to synchronous execution. If you want to handle the server response asynchronously, you need to register success and failure listeners. These listeners will be called in a separate thread once a response arrives.
Each CommandFuture
can only ever have one CommandFuture.SuccessListener
and
one CommandFuture.FailureListener
registered. All TS3ApiAsync
methods are
guaranteed to return a CommandFuture
with no listeners registered.
To set the value of a CommandFuture
, the set(Object)
method is used;
to notify it of a failure, fail(QueryError)
is used. You usually
shouldn't call these methods yourself, however. That's the job of the API.
CommandFuture
s are thread-safe. All state-changing methods are synchronized.
TS3ApiAsync
Modifier and Type | Class and Description |
---|---|
static interface |
CommandFuture.FailureListener
A listener which will be notified if the
CommandFuture failed. |
static interface |
CommandFuture.SuccessListener<V>
A listener which will be notified if the
CommandFuture succeeded. |
Constructor and Description |
---|
CommandFuture() |
Modifier and Type | Method and Description |
---|---|
boolean |
cancel(boolean mayInterruptIfRunning)
Cancelling a
CommandFuture will not actually cancel the
execution of the command which was sent to the TeamSpeak server. |
boolean |
fail(QueryError error)
Notifies this future that the command has failed.
|
CommandFuture<V> |
forwardFailure(CommandFuture<?> otherFuture)
Forwards a failure to another future by calling
fail(QueryError)
on that future with the error that caused this future to fail. |
void |
forwardResult(CommandFuture<V> otherFuture)
Forwards both a success as well as a failure to another
CommandFuture . |
CommandFuture<V> |
forwardSuccess(CommandFuture<? super V> otherFuture)
Forwards a success to another future by calling
set(Object) on
that future with the value this future was set to. |
V |
get()
Waits indefinitely until the command completes
and returns the result of the command.
|
V |
get(long timeout,
TimeUnit unit)
Waits for at most the given time until the command completes
and returns the result of the command.
|
V |
getUninterruptibly()
Waits indefinitely until the command completes
and returns the result of the command.
|
V |
getUninterruptibly(long timeout,
TimeUnit unit)
Waits for at most the given time until the command completes
and returns the result of the command.
|
boolean |
isCancelled() |
boolean |
isDone() |
boolean |
isFailed()
Returns
true if the command failed and threw a TS3CommandFailedException . |
boolean |
isSuccessful()
Returns
true if this command completed successfully,
i.e. the future wasn't cancelled and the command completed without throwing an exception. |
static <F> CommandFuture<List<F>> |
ofAll(Collection<CommandFuture<F>> futures)
Combines a collection of
CommandFuture s into a single future, which will
succeed if all futures succeed and fail as soon as one future fails. |
static <F> CommandFuture<List<F>> |
ofAll(CommandFuture<F>... futures)
Combines multiple
CommandFuture s into a single future, which will
succeed if all futures succeed and fail as soon as one future fails. |
static <F> CommandFuture<F> |
ofAny(Collection<CommandFuture<F>> futures)
Combines a collection of
CommandFuture s into a single future, which will
succeed as soon as one of the futures succeeds and fail if all futures fail. |
static <F> CommandFuture<F> |
ofAny(CommandFuture<F>... futures)
Combines multiple
CommandFuture s into a single future, which will
succeed if any of the futures succeeds and fail if all of the futures fail. |
CommandFuture<V> |
onFailure(CommandFuture.FailureListener listener)
Sets a
CommandFuture.FailureListener which will be notified when this future
fails because of a error returned by the TeamSpeak server. |
CommandFuture<V> |
onSuccess(CommandFuture.SuccessListener<? super V> listener)
Sets a
CommandFuture.SuccessListener which will be notified when this future
succeeded and a value has been set. |
boolean |
set(V value)
Sets the value of this future.
|
public CommandFuture()
public V get() throws InterruptedException
If the thread is interrupted while waiting for the command
to complete, this method will throw an InterruptedException
and the thread's interrupt flag will be cleared.
get
in interface Future<V>
InterruptedException
- if the method is interrupted by calling Thread.interrupt()
.
The interrupt flag will be clearedCancellationException
- if the CommandFuture
was cancelled before the command completedTS3CommandFailedException
- if the command failspublic V get(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
If the thread is interrupted while waiting for the command
to complete, this method will throw an InterruptedException
and the thread's interrupt flag will be cleared.
get
in interface Future<V>
timeout
- the maximum amount of the given time unit to waitunit
- the time unit of the timeout argumentInterruptedException
- if the method is interrupted by calling Thread.interrupt()
.
The interrupt flag will be clearedTimeoutException
- if the given time elapsed without the command completingCancellationException
- if the CommandFuture
was cancelled before the command completedTS3CommandFailedException
- if the command failspublic V getUninterruptibly()
If the thread is interrupted while waiting for the command
to complete, the interrupt is simply ignored and no
InterruptedException
is thrown.
CancellationException
- if the CommandFuture
was cancelled before the command completedTS3CommandFailedException
- if the command failspublic V getUninterruptibly(long timeout, TimeUnit unit) throws TimeoutException
If the thread is interrupted while waiting for the command
to complete, the interrupt is simply ignored and no
InterruptedException
is thrown.
timeout
- the maximum amount of the given time unit to waitunit
- the time unit of the timeout argumentTimeoutException
- if the given time elapsed without the command completingCancellationException
- if the CommandFuture
was cancelled before the command completedTS3CommandFailedException
- if the command failspublic boolean isSuccessful()
true
if this command completed successfully,
i.e. the future wasn't cancelled and the command completed without throwing an exception.true
if the command completed successfullypublic boolean isCancelled()
isCancelled
in interface Future<V>
public boolean isFailed()
true
if the command failed and threw a TS3CommandFailedException
.true
if the command failedpublic boolean set(V value)
Furthermore, this will run the CommandFuture.SuccessListener
, if one is registered.
All exceptions thrown from the body of the SuccessListener
are caught
so no exceptions can leak into user code.
Note that a future's value can only be set once. Subsequent calls to this method will be ignored.
value
- the value to set this future totrue
if the command was marked as successfulpublic boolean fail(QueryError error)
Furthermore, this will run the CommandFuture.FailureListener
, if one is registered.
All exceptions thrown from the body of the FailureListener
are caught
so no exceptions can leak into user code.
Note that a future can only fail once. Subsequent calls to this method will be ignored.
error
- the error that was returned from the servertrue
if the command was marked as failedpublic boolean cancel(boolean mayInterruptIfRunning)
Cancelling a CommandFuture
will not actually cancel the
execution of the command which was sent to the TeamSpeak server.
It will, however, prevent the CommandFuture.SuccessListener
and the
CommandFuture.FailureListener
from firing, provided a response from the
server has not yet arrived.
public CommandFuture<V> onSuccess(CommandFuture.SuccessListener<? super V> listener)
CommandFuture.SuccessListener
which will be notified when this future
succeeded and a value has been set.
If this future has already succeeded, this method will immediately call the listener method, which will be executed synchronously.
listener
- the listener to notify of a successpublic CommandFuture<V> onFailure(CommandFuture.FailureListener listener)
CommandFuture.FailureListener
which will be notified when this future
fails because of a error returned by the TeamSpeak server.
If this future has already failed, this method will immediately call the listener method, which will be executed synchronously.
listener
- the listener to notify of a failurepublic CommandFuture<V> forwardSuccess(CommandFuture<? super V> otherFuture)
set(Object)
on
that future with the value this future was set to.
This will register a CommandFuture.SuccessListener
, meaning that you will not
be able to register another SuccessListener
.
otherFuture
- the future to forward a success topublic CommandFuture<V> forwardFailure(CommandFuture<?> otherFuture)
fail(QueryError)
on that future with the error that caused this future to fail.
This will register a CommandFuture.FailureListener
, meaning that you will not
be able to register another FailureListener
.
otherFuture
- the future to forward a failure topublic void forwardResult(CommandFuture<V> otherFuture)
CommandFuture
.
This method just calls both forwardSuccess(CommandFuture)
and
forwardFailure(CommandFuture)
.
This will set both a CommandFuture.SuccessListener
as well as a CommandFuture.FailureListener
,
so no other listeners can be registered.
otherFuture
- the future which should be notified about@SafeVarargs public static <F> CommandFuture<List<F>> ofAll(CommandFuture<F>... futures)
CommandFuture
s into a single future, which will
succeed if all futures succeed and fail as soon as one future fails.F
- the common return type of the futuresfutures
- the futures to combinepublic static <F> CommandFuture<List<F>> ofAll(Collection<CommandFuture<F>> futures)
CommandFuture
s into a single future, which will
succeed if all futures succeed and fail as soon as one future fails.F
- the common return type of the futuresfutures
- the futures to combine@SafeVarargs public static <F> CommandFuture<F> ofAny(CommandFuture<F>... futures)
CommandFuture
s into a single future, which will
succeed if any of the futures succeeds and fail if all of the futures fail.F
- the common return type of the futuresfutures
- the futures to combinepublic static <F> CommandFuture<F> ofAny(Collection<CommandFuture<F>> futures)
CommandFuture
s into a single future, which will
succeed as soon as one of the futures succeeds and fail if all futures fail.F
- the common return type of the futuresfutures
- the futures to combineCopyright © 2015. All rights reserved.