Skip to content

Commit

Permalink
feat: onComplete method new signature
Browse files Browse the repository at this point in the history
Add Future implementation
Add FutureImpl implementations
Refactor to re-use new code for onSuccess and onFailure

Signed-off-by: Samuel Hawker <sam.b.hawker@gmail.com>
  • Loading branch information
samuel-hawker authored and vietj committed Sep 9, 2023
1 parent c9bd1e2 commit 81dc710
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 12 deletions.
32 changes: 22 additions & 10 deletions src/main/java/io/vertx/core/Future.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,26 @@ static <T> Future<T> failedFuture(String failureMessage) {
@Fluent
Future<T> onComplete(Handler<AsyncResult<T>> handler);

/**
* Add handlers to be notified on succeeded result and failed result.
* <p>
* <em><strong>WARNING</strong></em>: this is a terminal operation.
* If several {@code handler}s are registered, there is no guarantee that they will be invoked in order of registration.
*
* @param successHandler the handler that will be called with the succeeded result
* @param failureHandler the handler that will be called with the failed result
* @return a reference to this, so it can be used fluently
*/
default Future<T> onComplete(Handler<T> successHandler, Handler<Throwable> failureHandler) {
return onComplete(ar -> {
if (successHandler != null && ar.succeeded()) {
successHandler.handle(ar.result());
} else if (failureHandler != null && ar.failed()) {
failureHandler.handle(ar.cause());
}
});
}

/**
* Add a handler to be notified of the succeeded result.
* <p>
Expand All @@ -278,11 +298,7 @@ static <T> Future<T> failedFuture(String failureMessage) {
*/
@Fluent
default Future<T> onSuccess(Handler<T> handler) {
return onComplete(ar -> {
if (ar.succeeded()) {
handler.handle(ar.result());
}
});
return onComplete(handler, null);
}

/**
Expand All @@ -296,11 +312,7 @@ default Future<T> onSuccess(Handler<T> handler) {
*/
@Fluent
default Future<T> onFailure(Handler<Throwable> handler) {
return onComplete(ar -> {
if (ar.failed()) {
handler.handle(ar.cause());
}
});
return onComplete(null, handler);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/vertx/core/impl/future/FutureImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,41 @@ public void onFailure(Throwable failure) {
return this;
}

@Override
public Future<T> onComplete(Handler<T> successHandler, Handler<Throwable> failureHandler) {
addListener(new Listener<T>() {
@Override
public void onSuccess(T value) {
try {
if (successHandler != null) {
successHandler.handle(value);
}
} catch (Throwable t) {
if (context != null) {
context.reportException(t);
} else {
throw t;
}
}
}
@Override
public void onFailure(Throwable failure) {
try {
if (failureHandler != null) {
failureHandler.handle(failure);
}
} catch (Throwable t) {
if (context != null) {
context.reportException(t);
} else {
throw t;
}
}
}
});
return this;
}

@Override
public Future<T> onComplete(Handler<AsyncResult<T>> handler) {
Objects.requireNonNull(handler, "No null handler accepted");
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/io/vertx/core/FutureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1304,13 +1304,20 @@ public void testSeveralHandlers3() {

@Test
public void testSuccessNotification() {
waitFor(2);
waitFor(3);
Promise<String> promise = Promise.promise();
Future<String> fut = promise.future();
fut.onComplete(onSuccess(res -> {
assertEquals("foo", res);
complete();
}));
fut.onComplete(
res -> {
assertEquals("foo", res);
complete();
},
err -> fail()
);
fut.onSuccess(res -> {
assertEquals("foo", res);
complete();
Expand All @@ -1324,14 +1331,21 @@ public void testSuccessNotification() {

@Test
public void testFailureNotification() {
waitFor(2);
waitFor(3);
Promise<String> promise = Promise.promise();
Future<String> fut = promise.future();
Throwable failure = new Throwable();
fut.onComplete(onFailure(err -> {
assertEquals(failure, err);
complete();
}));
fut.onComplete(
res -> fail(),
err -> {
assertEquals(failure, err);
complete();
}
);
fut.onSuccess(res -> {
fail();
});
Expand Down

0 comments on commit 81dc710

Please sign in to comment.