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> * @author <a href="http://tfox.org">Tim Fox</a>
*/ */
@VertxGen @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 * 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; 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 * @return an handler completing this future
*/ */
@CacheReturn @CacheReturn
default Handler<AsyncResult<T>> completer() { default Handler<AsyncResult<T>> completer() {
return ar -> { return this;
if (ar.succeeded()) {
complete(ar.result());
} else {
fail(ar.cause());
}
};
} }


/** /**
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 @Override
public void handle(AsyncResult<CompositeFuture> ar) { public void handle(AsyncResult<CompositeFuture> asyncResult) {
if (ar.succeeded()) { if (asyncResult.succeeded()) {
complete(this); complete(this);
} else { } 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 @Override
public void handle(AsyncResult<T> ar) { public void handle(AsyncResult<T> asyncResult) {
if (ar.succeeded()) { if (asyncResult.succeeded()) {
complete(ar.result()); complete(asyncResult.result());
} else { } 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 Throwable cause() { throw new UnsupportedOperationException(); }
public boolean succeeded() { throw new UnsupportedOperationException(); } public boolean succeeded() { throw new UnsupportedOperationException(); }
public boolean failed() { 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<>(); DefaultCompleterTestFuture<Object> successFuture = new DefaultCompleterTestFuture<>();
Expand Down

0 comments on commit 9050acc

Please sign in to comment.