Skip to content

Commit

Permalink
Closes #2 - renamed complete to success as its more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Randgalt committed Jun 15, 2017
1 parent e3232c3 commit 7abce6c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ StagedFuture.async(executor)
.then(() -> queryDatabaseFor("something"))
.withTimeout(Duration.ofSeconds(25))
.thenIf(record -> applyRecord(record)) // chain aborts if no record found
.whenComplete(result -> handleResult(result))
.whenSucceeded(result -> handleResult(result))
.whenAborted(() -> handleAbort())
.whenFailed(e -> handleFailure(e));
```
Expand Down Expand Up @@ -82,8 +82,8 @@ _Completers_

At any point in the chain, you can add handlers for successful completions, failures or aborts:

- `whenComplete(Consumer<T> handler)` - if the chain completes successfully the handler is called.
- `whenCompleteYield(Function<T, U> handler)` - same as `whenComplete()` but allows mapping the return type.
- `whenSucceeded(Consumer<T> handler)` - if the chain completes successfully the handler is called.
- `whenSucceededYield(Function<T, U> handler)` - same as `whenSucceeded()` but allows mapping the return type.
- `whenAborted(Runnable handler)` - if the chain is aborted (i.e. one of the `thenIf()` tasks returns empty) the handler is called.
- `whenFailed(Consumer<Throwable> handler)` - if there is an exception or failure in the chain the handler is called.
- `whenFinal(Runnable handler)` - calls the handler when the chain completes in any way (success, abort, exception, etc.).
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/soabase/stages/Aborted.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AbortException() {
* @param handler success handler - called only when the given stage returns a non-empty Optional
* @return new CompletionStage
*/
public static <T, U> CompletionStage<Optional<U>> whenComplete(CompletionStage<Optional<T>> stage, Function<T, Optional<U>> handler)
public static <T, U> CompletionStage<Optional<U>> whenSucceeded(CompletionStage<Optional<T>> stage, Function<T, Optional<U>> handler)
{
return stage.thenApply(optional -> optional.flatMap(handler));
}
Expand All @@ -53,7 +53,7 @@ public static <T, U> CompletionStage<Optional<U>> whenComplete(CompletionStage<O
* @param handler success handler - called only when the given stage returns a non-empty Optional
* @return new CompletionStage
*/
public static <T, U> CompletionStage<Optional<U>> whenCompleteAsync(CompletionStage<Optional<T>> stage, Function<T, Optional<U>> handler)
public static <T, U> CompletionStage<Optional<U>> whenSucceededAsync(CompletionStage<Optional<T>> stage, Function<T, Optional<U>> handler)
{
return stage.thenApplyAsync(optional -> optional.flatMap(handler));
}
Expand All @@ -68,7 +68,7 @@ public static <T, U> CompletionStage<Optional<U>> whenCompleteAsync(CompletionSt
* @param executor the executor to use for asynchronous execution
* @return new CompletionStage
*/
public static <T, U> CompletionStage<Optional<U>> whenCompleteAsync(CompletionStage<Optional<T>> stage, Function<T, Optional<U>> handler, Executor executor)
public static <T, U> CompletionStage<Optional<U>> whenSucceededAsync(CompletionStage<Optional<T>> stage, Function<T, Optional<U>> handler, Executor executor)
{
return stage.thenApplyAsync(optional -> optional.flatMap(handler), executor);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/soabase/stages/StagedFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static StagedFutureBuilder asyncPool(Tracing tracing) {
* @param handler consumer for the value
* @return next stage in the chain
*/
StagedFuture<T> whenComplete(Consumer<T> handler);
StagedFuture<T> whenSucceeded(Consumer<T> handler);

/**
* If the stage and any previous stages in the chain complete successfully, the handler is called with the resulting value.
Expand All @@ -180,7 +180,7 @@ static StagedFutureBuilder asyncPool(Tracing tracing) {
* @param handler mapper for the value
* @return next stage in the chain
*/
<U> StagedFuture<U> whenCompleteYield(Function<T, U> handler);
<U> StagedFuture<U> whenSucceededYield(Function<T, U> handler);

/**
* If this stage or any previous stages in the chain return {@link Optional#empty()}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/soabase/stages/StagedFutureImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@ public StagedFuture<T> withTimeout(Duration max, Supplier<T> defaultValue) {
}

@Override
public StagedFuture<T> whenComplete(Consumer<T> handler) {
public StagedFuture<T> whenSucceeded(Consumer<T> handler) {
Objects.requireNonNull(handler, "handler cannot be null");
return whenCompleteYield(value -> {
return whenSucceededYield(value -> {
handler.accept(value);
return value;
});
}

@Override
public <U> StagedFuture<U> whenCompleteYield(Function<T, U> handler) {
public <U> StagedFuture<U> whenSucceededYield(Function<T, U> handler) {
Objects.requireNonNull(handler, "handler cannot be null");
CompletionStage<Optional<U>> next = Aborted.whenCompleteAsync(future, value -> Optional.of(handler.apply(value)));
CompletionStage<Optional<U>> next = Aborted.whenSucceededAsync(future, value -> Optional.of(handler.apply(value)));
return new StagedFutureImpl<>(executor, next, tracing);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/io/soabase/stages/TestStaged.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void testBasic() throws Exception {
.then(() -> worker("1"))
.then(s -> worker("2"))
.then(s -> worker("3"))
.whenCompleteYield(s -> tracing.getTracing());
.whenSucceededYield(s -> tracing.getTracing());

Optional<List<TestTracing.Trace>> optional = complete(future);
assertThat(optional).isPresent();
Expand All @@ -82,7 +82,7 @@ public void testWrappedOptional() throws Exception {
complete(StagedFuture.sync()
.then(() -> Optional.of("hey"))
.then(heyOpt -> (heyOpt.isPresent() && heyOpt.get().equals("hey")) ? Optional.empty() : Optional.of("fail"))
.whenComplete(shouldBeEmpty -> weGotHere.set(!shouldBeEmpty.isPresent())));
.whenSucceeded(shouldBeEmpty -> weGotHere.set(!shouldBeEmpty.isPresent())));
assertThat(weGotHere.get()).isTrue();
}

Expand Down

0 comments on commit 7abce6c

Please sign in to comment.