Skip to content

Commit

Permalink
Future<T> extends Handler<AsyncResult<T>> - fixes #1826
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Feb 20, 2017
1 parent cfef3de commit 9050acc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
19 changes: 11 additions & 8 deletions src/main/java/io/vertx/core/Future.java
Expand Up @@ -31,7 +31,7 @@
* @author <a href="http://tfox.org">Tim Fox</a>
*/
@VertxGen
public interface Future<T> extends AsyncResult<T> {
public interface Future<T> extends AsyncResult<T>, Handler<AsyncResult<T>> {

/**
* Create a future that hasn't completed yet
Expand Down Expand Up @@ -291,18 +291,21 @@ default <V> Future<V> map(V value) {
return ret;
}

/**
* Succeed or fail this future with the {@link AsyncResult} event.
*
* @param asyncResult the async result to handle
*/
@GenIgnore
@Override
void handle(AsyncResult<T> asyncResult);

/**
* @return an handler completing this future
*/
@CacheReturn
default Handler<AsyncResult<T>> completer() {
return ar -> {
if (ar.succeeded()) {
complete(ar.result());
} else {
fail(ar.cause());
}
};
return this;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/vertx/core/impl/CompositeFutureImpl.java
Expand Up @@ -272,11 +272,11 @@ public Handler<AsyncResult<CompositeFuture>> completer() {
}

@Override
public void handle(AsyncResult<CompositeFuture> ar) {
if (ar.succeeded()) {
public void handle(AsyncResult<CompositeFuture> asyncResult) {
if (asyncResult.succeeded()) {
complete(this);
} else {
fail(ar.cause());
fail(asyncResult.cause());
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/io/vertx/core/impl/FutureImpl.java
Expand Up @@ -130,11 +130,11 @@ public Handler<AsyncResult<T>> completer() {
}

@Override
public void handle(AsyncResult<T> ar) {
if (ar.succeeded()) {
complete(ar.result());
public void handle(AsyncResult<T> asyncResult) {
if (asyncResult.succeeded()) {
complete(asyncResult.result());
} else {
fail(ar.cause());
fail(asyncResult.cause());
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/io/vertx/test/core/FutureTest.java
Expand Up @@ -731,6 +731,13 @@ class DefaultCompleterTestFuture<T> implements Future<T> {
public Throwable cause() { throw new UnsupportedOperationException(); }
public boolean succeeded() { throw new UnsupportedOperationException(); }
public boolean failed() { throw new UnsupportedOperationException(); }
public void handle(AsyncResult<T> asyncResult) {
if (asyncResult.succeeded()) {
complete(asyncResult.result());
} else {
fail(asyncResult.cause());
}
}
}

DefaultCompleterTestFuture<Object> successFuture = new DefaultCompleterTestFuture<>();
Expand Down

0 comments on commit 9050acc

Please sign in to comment.