Skip to content

Commit

Permalink
Reverts #1824 and instead ddd tryComplete/tryFail methods that allow …
Browse files Browse the repository at this point in the history
…to complete/fail Future if not yet completed - fixes #1835
  • Loading branch information
vietj committed Feb 23, 2017
1 parent d89c93b commit c29268f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 48 deletions.
11 changes: 6 additions & 5 deletions src/main/java/io/vertx/core/CompositeFuture.java
Expand Up @@ -186,17 +186,18 @@ static CompositeFuture join(List<Future> futures) {
CompositeFuture setHandler(Handler<AsyncResult<CompositeFuture>> handler);

/**
* @return false
* Set this instance as result. Any handler will be called, if there is one, and the future will be marked as completed.
*/
@Override
boolean complete();
void complete();

/**
* @return false
* Try to set this instance as result. When it happens, any handler will be called, if there is one, and the future will be marked as completed.
*
* @return false when the future is already completed
*/
@Override
boolean complete(CompositeFuture result);

boolean tryComplete();

/**
* Returns a cause of a wrapped future
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/io/vertx/core/Future.java
Expand Up @@ -122,35 +122,61 @@ static <T> Future<T> failedFuture(String failureMessage) {
Future<T> setHandler(Handler<AsyncResult<T>> handler);

/**
* Try to set the result. When it happens, any handler will be called, if there is one, and the future will be marked as completed.
* Set the result. Any handler will be called, if there is one, and the future will be marked as completed.
*
* @param result the result
*/
void complete(T result);

/**
* Set a null result. Any handler will be called, if there is one, and the future will be marked as completed.
*/
void complete();

/**
* Set the failure. Any handler will be called, if there is one, and the future will be marked as completed.
*
* @param cause the failure cause
*/
void fail(Throwable cause);

/**
* Try to set the failure. When it happens, any handler will be called, if there is one, and the future will be marked as completed.
*
* @param failureMessage the failure message
*/
void fail(String failureMessage);

/**
* Set the failure. Any handler will be called, if there is one, and the future will be marked as completed.
*
* @param result the result
* @return false when the future is already completed
*/
boolean complete(T result);
boolean tryComplete(T result);

/**
* Try to set the result. When it happens, any handler will be called, if there is one, and the future will be marked as completed.
*
* @return false when the future is already completed
*/
boolean complete();
boolean tryComplete();

/**
* Try to set the failure. When it happens, any handler will be called, if there is one, and the future will be marked as completed.
*
* @param cause the failure cause
* @return false when the future is already completed
*/
boolean fail(Throwable cause);
boolean tryFail(Throwable cause);

/**
* Try to set the failure. When it happens, any handler will be called, if there is one, and the future will be marked as completed.
*
* @param failureMessage the failure message
* @return false when the future is already completed
*/
boolean fail(String failureMessage);
boolean tryFail(String failureMessage);

/**
* The result of the operation. This will be null if the operation failed.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/core/impl/CloseHooks.java
Expand Up @@ -90,7 +90,7 @@ void run(Handler<AsyncResult<Void>> completionHandler) {
hook.close(a.completer());
} catch (Throwable t) {
log.warn("Failed to run close hooks", t);
a.fail(t);
a.tryFail(t);
}
}
} else {
Expand Down
48 changes: 41 additions & 7 deletions src/main/java/io/vertx/core/impl/CompositeFutureImpl.java
Expand Up @@ -217,17 +217,51 @@ public synchronized CompositeFuture result() {
}

@Override
public boolean complete(CompositeFuture result) {
return false;
public void complete() {
if (!tryComplete()) {
throw new IllegalStateException("Result is already complete: " + (this.cause == null ? "succeeded" : "failed"));
}
}

@Override
public void complete(CompositeFuture result) {
if (!tryComplete(result)) {
throw new IllegalStateException("Result is already complete: " + (this.cause == null ? "succeeded" : "failed"));
}
}

@Override
public void fail(Throwable cause) {
if (!tryFail(cause)) {
throw new IllegalStateException("Result is already complete: " + (this.cause == null ? "succeeded" : "failed"));
}
}

@Override
public void fail(String failureMessage) {
if (!tryFail(failureMessage)) {
throw new IllegalStateException("Result is already complete: " + (this.cause == null ? "succeeded" : "failed"));
}
}

@Override
public boolean tryComplete(CompositeFuture result) {
Handler<AsyncResult<CompositeFuture>> handler = setCompleted(null);
if (handler != null) {
handler.handle(this);
return true;
} else {
return false;
}
}

@Override
public boolean complete() {
return false;
public boolean tryComplete() {
return tryComplete(this);
}

@Override
public boolean fail(Throwable cause) {
public boolean tryFail(Throwable cause) {
Handler<AsyncResult<CompositeFuture>> handler = setCompleted(cause);
if (handler != null) {
handler.handle(this);
Expand All @@ -238,8 +272,8 @@ public boolean fail(Throwable cause) {
}

@Override
public boolean fail(String failureMessage) {
return fail(new NoStackTraceThrowable(failureMessage));
public boolean tryFail(String failureMessage) {
return tryFail(new NoStackTraceThrowable(failureMessage));
}

private Handler<AsyncResult<CompositeFuture>> setCompleted(Throwable cause) {
Expand Down
40 changes: 34 additions & 6 deletions src/main/java/io/vertx/core/impl/FutureImpl.java
Expand Up @@ -102,7 +102,35 @@ public Future<T> setHandler(Handler<AsyncResult<T>> handler) {
}

@Override
public boolean complete(T result) {
public void complete(T result) {
if (!tryComplete(result)) {
throw new IllegalStateException("Result is already complete: " + (succeeded ? "succeeded" : "failed"));
}
}

@Override
public void complete() {
if (!tryComplete()) {
throw new IllegalStateException("Result is already complete: " + (succeeded ? "succeeded" : "failed"));
}
}

@Override
public void fail(Throwable cause) {
if (!tryFail(cause)) {
throw new IllegalStateException("Result is already complete: " + (succeeded ? "succeeded" : "failed"));
}
}

@Override
public void fail(String failureMessage) {
if (!tryFail(failureMessage)) {
throw new IllegalStateException("Result is already complete: " + (succeeded ? "succeeded" : "failed"));
}
}

@Override
public boolean tryComplete(T result) {
if (succeeded || failed) {
return false;
}
Expand All @@ -113,8 +141,8 @@ public boolean complete(T result) {
}

@Override
public boolean complete() {
return complete(null);
public boolean tryComplete() {
return tryComplete(null);
}

public void handle(Future<T> ar) {
Expand All @@ -140,7 +168,7 @@ public void handle(AsyncResult<T> asyncResult) {
}

@Override
public boolean fail(Throwable cause) {
public boolean tryFail(Throwable cause) {
if (succeeded || failed) {
return false;
}
Expand All @@ -151,8 +179,8 @@ public boolean fail(Throwable cause) {
}

@Override
public boolean fail(String failureMessage) {
return fail(new NoStackTraceThrowable(failureMessage));
public boolean tryFail(String failureMessage) {
return tryFail(new NoStackTraceThrowable(failureMessage));
}

private void checkCallHandler() {
Expand Down

0 comments on commit c29268f

Please sign in to comment.