Class ContentSourceCompletableFuture<X>
java.lang.Object
java.util.concurrent.CompletableFuture<X>
org.eclipse.jetty.io.content.ContentSourceCompletableFuture<X>
- All Implemented Interfaces:
Runnable, CompletionStage<X>, Future<X>, Invocable, Invocable.Task
- Direct Known Subclasses:
FormFields
public abstract class ContentSourceCompletableFuture<X>
extends CompletableFuture<X>
implements Invocable.Task
A utility class to convert content from a Content.Source to an instance
available via a CompletableFuture.
An example usage to asynchronously read UTF-8 content is:
public static class CompletableUTF8String extends ContentSourceCompletableFuture<String>;
{
private final Utf8StringBuilder builder = new Utf8StringBuilder();
public CompletableUTF8String(Content.Source content)
{
super(content);
}
@Override
protected String parse(Content.Chunk chunk) throws Throwable
{
// Accumulate the chunk bytes.
if (chunk.hasRemaining())
builder.append(chunk.getByteBuffer());
// Not the last chunk, the result is not ready yet.
if (!chunk.isLast())
return null;
// The result is ready.
return builder.takeCompleteString(IllegalStateException::new);
}
}
CompletableUTF8String cs = new CompletableUTF8String(source);
cs.parse();
String s = cs.get();
-
Nested Class Summary
Nested classes/interfaces inherited from class CompletableFuture
CompletableFuture.AsynchronousCompletionTaskNested classes/interfaces inherited from interface Future
Future.StateNested classes/interfaces inherited from interface Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.TaskNested classes/interfaces inherited from interface Invocable.Task
Invocable.Task.Abstract -
Field Summary
Fields inherited from interface Invocable
__nonBlocking, NOOP -
Constructor Summary
ConstructorsConstructorDescriptionContentSourceCompletableFuture(Content.Source content, Invocable.InvocationType invocationType) -
Method Summary
Modifier and TypeMethodDescriptionacceptEither(CompletionStage<? extends X> other, Consumer<? super X> action) <U> CompletableFuture<U> applyToEither(CompletionStage<? extends X> other, Function<? super X, U> fn) get()<U> CompletableFuture<U> handle(BiFunction<? super X, Throwable, ? extends U> fn) join()protected booleanonTransientFailure(Throwable cause) Callback method that informs the parsing about how to handle transient failures.voidparse()Initiates the parsing of theContent.Source.protected abstract Xparse(Content.Chunk chunk) Called byparse()to parse aContent.Chunk.voidrun()runAfterBoth(CompletionStage<?> other, Runnable action) runAfterEither(CompletionStage<?> other, Runnable action) thenAccept(Consumer<? super X> action) <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super X, ? super U> action) <U> CompletableFuture<U> <U,V1> CompletableFuture <V1> thenCombine(CompletionStage<? extends U> other, BiFunction<? super X, ? super U, ? extends V1> fn) <U> CompletableFuture<U> thenCompose(Function<? super X, ? extends CompletionStage<U>> fn) whenComplete(BiConsumer<? super X, ? super Throwable> action) Methods inherited from class CompletableFuture
acceptEitherAsync, acceptEitherAsync, allOf, anyOf, applyToEitherAsync, applyToEitherAsync, cancel, complete, completeAsync, completeAsync, completedFuture, completedStage, completeExceptionally, completeOnTimeout, copy, defaultExecutor, delayedExecutor, delayedExecutor, exceptionally, exceptionallyAsync, exceptionallyAsync, exceptionallyCompose, exceptionallyComposeAsync, exceptionallyComposeAsync, exceptionNow, failedFuture, failedStage, getNow, getNumberOfDependents, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, minimalCompletionStage, newIncompleteFuture, obtrudeException, obtrudeValue, orTimeout, resultNow, runAfterBothAsync, runAfterBothAsync, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, state, supplyAsync, supplyAsync, thenAcceptAsync, thenAcceptAsync, thenAcceptBothAsync, thenAcceptBothAsync, thenApplyAsync, thenApplyAsync, thenCombineAsync, thenCombineAsync, thenComposeAsync, thenComposeAsync, thenRunAsync, thenRunAsync, toCompletableFuture, toString, whenCompleteAsync, whenCompleteAsync
-
Constructor Details
-
ContentSourceCompletableFuture
-
ContentSourceCompletableFuture
public ContentSourceCompletableFuture(Content.Source content, Invocable.InvocationType invocationType)
-
-
Method Details
-
parse
public void parse()Initiates the parsing of the
Content.Source.For every valid chunk that is read,
parse(Content.Chunk)is called, until a result is produced that is used to complete thisCompletableFuture.Internally, this method is called multiple times to progress the parsing in response to
Content.Source.demand(Runnable)calls.Exceptions thrown during parsing result in this
CompletableFutureto be completed exceptionally. -
parse
Called by
parse()to parse aContent.Chunk.- Parameters:
chunk- The chunk containing content to parse. The chunk will never benullnor afailure chunk. If the chunk is stored away to be used later beyond the scope of this call, then implementations must callRetainable.retain()andRetainable.release()as appropriate.- Returns:
- The parsed
Xresult instance ornullif parsing is not yet complete - Throws:
Throwable- If there is an error parsing
-
onTransientFailure
Callback method that informs the parsing about how to handle transient failures.
- Parameters:
cause- A transient failure obtained by reading anon-lastfailure chunk- Returns:
trueif the transient failure can be ignored,falseotherwise
-
run
-
acceptEither
public CompletableFuture<Void> acceptEither(CompletionStage<? extends X> other, Consumer<? super X> action) - Specified by:
acceptEitherin interfaceCompletionStage<X>- Overrides:
acceptEitherin classCompletableFuture<X>
-
applyToEither
public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends X> other, Function<? super X, U> fn) - Specified by:
applyToEitherin interfaceCompletionStage<X>- Overrides:
applyToEitherin classCompletableFuture<X>
-
get
- Specified by:
getin interfaceFuture<X>- Overrides:
getin classCompletableFuture<X>- Throws:
InterruptedExceptionExecutionException
-
get
public X get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
getin interfaceFuture<X>- Overrides:
getin classCompletableFuture<X>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
getInvocationType
- Specified by:
getInvocationTypein interfaceInvocable- Returns:
- The InvocationType of this object
-
handle
- Specified by:
handlein interfaceCompletionStage<X>- Overrides:
handlein classCompletableFuture<X>
-
join
- Overrides:
joinin classCompletableFuture<X>
-
runAfterBoth
- Specified by:
runAfterBothin interfaceCompletionStage<X>- Overrides:
runAfterBothin classCompletableFuture<X>
-
runAfterEither
- Specified by:
runAfterEitherin interfaceCompletionStage<X>- Overrides:
runAfterEitherin classCompletableFuture<X>
-
thenAccept
- Specified by:
thenAcceptin interfaceCompletionStage<X>- Overrides:
thenAcceptin classCompletableFuture<X>
-
thenAcceptBoth
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super X, ? super U> action) - Specified by:
thenAcceptBothin interfaceCompletionStage<X>- Overrides:
thenAcceptBothin classCompletableFuture<X>
-
thenApply
- Specified by:
thenApplyin interfaceCompletionStage<X>- Overrides:
thenApplyin classCompletableFuture<X>
-
thenCombine
public <U,V1> CompletableFuture<V1> thenCombine(CompletionStage<? extends U> other, BiFunction<? super X, ? super U, ? extends V1> fn) - Specified by:
thenCombinein interfaceCompletionStage<X>- Overrides:
thenCombinein classCompletableFuture<X>
-
thenCompose
- Specified by:
thenComposein interfaceCompletionStage<X>- Overrides:
thenComposein classCompletableFuture<X>
-
thenRun
- Specified by:
thenRunin interfaceCompletionStage<X>- Overrides:
thenRunin classCompletableFuture<X>
-
whenComplete
- Specified by:
whenCompletein interfaceCompletionStage<X>- Overrides:
whenCompletein classCompletableFuture<X>
-