Package org.eclipse.jetty.io.content
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 java.util.concurrent.CompletableFuture
CompletableFuture.AsynchronousCompletionTask
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.Task
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable.Task
Invocable.Task.Abstract
-
Field Summary
Fields inherited from interface org.eclipse.jetty.util.thread.Invocable
__nonBlocking, NOOP
-
Constructor Summary
ConstructorDescriptionContentSourceCompletableFuture
(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 boolean
onTransientFailure
(Throwable cause) Callback method that informs the parsing about how to handle transient failures.void
parse()
Initiates the parsing of theContent.Source
.protected abstract X
parse
(Content.Chunk chunk) Called byparse()
to parse aContent.Chunk
.void
run()
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 java.util.concurrent.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, failedFuture, failedStage, getNow, getNumberOfDependents, handleAsync, handleAsync, isCancelled, isCompletedExceptionally, isDone, minimalCompletionStage, newIncompleteFuture, obtrudeException, obtrudeValue, orTimeout, runAfterBothAsync, runAfterBothAsync, runAfterEitherAsync, runAfterEitherAsync, runAsync, runAsync, 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
CompletableFuture
to be completed exceptionally. -
parse
Called by
parse()
to parse aContent.Chunk
.- Parameters:
chunk
- The chunk containing content to parse. The chunk will never benull
nor 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
X
result instance ornull
if 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-last
failure chunk
- Returns:
true
if the transient failure can be ignored,false
otherwise
-
run
public void run() -
acceptEither
public CompletableFuture<Void> acceptEither(CompletionStage<? extends X> other, Consumer<? super X> action) - Specified by:
acceptEither
in interfaceCompletionStage<X>
- Overrides:
acceptEither
in classCompletableFuture<X>
-
applyToEither
public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends X> other, Function<? super X, U> fn) - Specified by:
applyToEither
in interfaceCompletionStage<X>
- Overrides:
applyToEither
in classCompletableFuture<X>
-
get
- Specified by:
get
in interfaceFuture<X>
- Overrides:
get
in classCompletableFuture<X>
- Throws:
InterruptedException
ExecutionException
-
get
public X get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
get
in interfaceFuture<X>
- Overrides:
get
in classCompletableFuture<X>
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
getInvocationType
- Specified by:
getInvocationType
in interfaceInvocable
- Returns:
- The InvocationType of this object
-
handle
- Specified by:
handle
in interfaceCompletionStage<X>
- Overrides:
handle
in classCompletableFuture<X>
-
join
- Overrides:
join
in classCompletableFuture<X>
-
runAfterBoth
- Specified by:
runAfterBoth
in interfaceCompletionStage<X>
- Overrides:
runAfterBoth
in classCompletableFuture<X>
-
runAfterEither
- Specified by:
runAfterEither
in interfaceCompletionStage<X>
- Overrides:
runAfterEither
in classCompletableFuture<X>
-
thenAccept
- Specified by:
thenAccept
in interfaceCompletionStage<X>
- Overrides:
thenAccept
in classCompletableFuture<X>
-
thenAcceptBoth
public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, BiConsumer<? super X, ? super U> action) - Specified by:
thenAcceptBoth
in interfaceCompletionStage<X>
- Overrides:
thenAcceptBoth
in classCompletableFuture<X>
-
thenApply
- Specified by:
thenApply
in interfaceCompletionStage<X>
- Overrides:
thenApply
in classCompletableFuture<X>
-
thenCombine
public <U,V1> CompletableFuture<V1> thenCombine(CompletionStage<? extends U> other, BiFunction<? super X, ? super U, ? extends V1> fn) - Specified by:
thenCombine
in interfaceCompletionStage<X>
- Overrides:
thenCombine
in classCompletableFuture<X>
-
thenCompose
- Specified by:
thenCompose
in interfaceCompletionStage<X>
- Overrides:
thenCompose
in classCompletableFuture<X>
-
thenRun
- Specified by:
thenRun
in interfaceCompletionStage<X>
- Overrides:
thenRun
in classCompletableFuture<X>
-
whenComplete
- Specified by:
whenComplete
in interfaceCompletionStage<X>
- Overrides:
whenComplete
in classCompletableFuture<X>
-