Class CompletableFuture<R,S,T extends CompletableTask<R,S>>
java.lang.Object
de.linusdev.lutils.async.AbstractFuture<R,S,T>
de.linusdev.lutils.async.completeable.CompletableFuture<R,S,T>
- All Implemented Interfaces:
Future<R,,S> HasAsyncManager
A
For the usage in combination with a
Example:
Future which can be completed with the complete(Object, Object, AsyncError) method.
This method will then call all listeners (see, ...) in the current Thread.
For the usage in combination with a
CompletableTask, see CompletableTask.
Example:
//Create the future
var future = CompletableFuture.<String, Nothing>create(asyncManager);
new Thread(() -> {
try {
Thread.sleep(3000);
//start the future, if it was not canceled
if(future.startIfNotCanceled()) return;
//Complete the future in a different Thread
future.complete("Hello", Nothing.INSTANCE, null);
} catch (Throwable e) {
future.complete(null, Nothing.INSTANCE,
new ThrowableAsyncError(e));
}
}).start();
//End User:
Future<String, Nothing> returnedFuture = future;
String res = returnedFuture.getResult();
System.out.println("Result: " + res);- See Also:
-
Field Summary
Fields inherited from class de.linusdev.lutils.async.AbstractFuture
asyncManager, before, canceled, done, lock, result, started, task, then -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCompletableFuture(@NotNull AsyncManager asyncManager, boolean supportsBeforeExecutionListener) CompletableFuture(T task, @NotNull AsyncManager asyncManager, boolean supportsBeforeExecutionListener) -
Method Summary
Modifier and TypeMethodDescriptionbeforeExecution(@NotNull Consumer<Future<R, S>> consumer) Given consumer will be called before theFuturewill be executed.voidcomplete(R result, S secondary, @Nullable AsyncError error) static <R,S> @NotNull CompletableFuture<R, S, CompletableTask<R, S>> create(@NotNull AsyncManager asyncManager, boolean supportsBeforeExecutionListener) booleanbooleanMethods inherited from class de.linusdev.lutils.async.AbstractFuture
cancel, get, getAsyncManager, getTask, hasStarted, isCanceled, isDone, then
-
Constructor Details
-
CompletableFuture
protected CompletableFuture(@NotNull @NotNull AsyncManager asyncManager, boolean supportsBeforeExecutionListener) -
CompletableFuture
public CompletableFuture(@Nullable T task, @NotNull @NotNull AsyncManager asyncManager, boolean supportsBeforeExecutionListener)
-
-
Method Details
-
create
@NotNull public static <R,S> @NotNull CompletableFuture<R,S, createCompletableTask<R, S>> (@NotNull @NotNull AsyncManager asyncManager, boolean supportsBeforeExecutionListener) -
complete
public void complete(@Nullable R result, @NotNull S secondary, @Nullable @Nullable AsyncError error) -
startIfNotCanceled
public boolean startIfNotCanceled()Set the future's state tostartedif it was notcanceledor alreadystartedbefore.
Tocomplete(Object, Object, AsyncError)a future it is not required to call this function before. (but it is recommended) -
beforeExecution
@NotNull public @NotNull Future<R,S> beforeExecution(@NotNull @NotNull Consumer<Future<R, S>> consumer) Description copied from interface:Future- Specified by:
beforeExecutionin interfaceFuture<R,S> - Overrides:
beforeExecutionin classAbstractFuture<R,S, T extends CompletableTask<R, S>> - Parameters:
consumer-Consumerthat will be called before execution- Returns:
- the
Futureitself.
-
supportsBeforeExecutionListener
public boolean supportsBeforeExecutionListener()- Returns:
trueif this future supports abeforeExecution(Consumer)listener.
-