Skip to content

Commit

Permalink
The Future#eventually should use a Supplier<Future<T>> instead of a f…
Browse files Browse the repository at this point in the history
…unction, the new method was added in Vert.x 4 and the function version has been deprecated.

Replace Future#eventually(Function<Void, Future<T>>) by Future#eventually(Supplier<Future<T>>).
  • Loading branch information
vietj committed Aug 31, 2023
1 parent 2f6220a commit c9f555f
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/examples/FileSystemExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void asyncFilePipe(Vertx vertx) {
.open("target/classes/les_miserables.txt", new OpenOptions())
.compose(file -> file
.pipeTo(output)
.eventually(v -> file.close()))
.eventually(() -> file.close()))
.onComplete(result -> {
if (result.succeeded()) {
System.out.println("Copy done");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/vertx/core/Future.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Represents the result of an action that may, or may not, have occurred yet.
Expand Down Expand Up @@ -417,7 +418,7 @@ default Future<T> recover(Function<Throwable, Future<T>> mapper) {
* @param mapper the function returning the future.
* @return the composed future
*/
<U> Future<T> eventually(Function<Void, Future<U>> mapper);
<U> Future<T> eventually(Supplier<Future<U>> mapper);

/**
* Apply a {@code mapper} function on this future.<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ public Future<Void> sendFile(String filename, long offset, long length) {
checkSendHeaders(false);
return file
.pipeTo(this)
.eventually(v -> file.close());
.eventually(() -> file.close());
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/core/http/impl/HttpNetSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public Future<Void> sendFile(String filename, long offset, long length) {
.pipe()
.endOnComplete(false)
.to(this)
.eventually(v -> file.close())
.eventually(file::close)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/vertx/core/impl/DeploymentManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public synchronized Future<Void> doUndeploy(ContextInternal undeployingContext)
Promise<Void> stopPromise = undeployingContext.promise();
Future<Void> stopFuture = stopPromise.future();
stopFuture
.eventually(v2 -> {
.eventually(() -> {
deployments.remove(deploymentID);
return verticleHolder
.close()
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/io/vertx/core/impl/future/Eventually.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.vertx.core.impl.ContextInternal;

import java.util.function.Function;
import java.util.function.Supplier;

/**
* Eventually operation.
Expand All @@ -22,18 +23,18 @@
*/
class Eventually<T, U> extends Operation<T> implements Listener<T> {

private final Function<Void, Future<U>> mapper;
private final Supplier<Future<U>> supplier;

Eventually(ContextInternal context, Function<Void, Future<U>> mapper) {
Eventually(ContextInternal context, Supplier<Future<U>> supplier) {
super(context);
this.mapper = mapper;
this.supplier = supplier;
}

@Override
public void onSuccess(T value) {
FutureInternal<U> future;
try {
future = (FutureInternal<U>) mapper.apply(null);
future = (FutureInternal<U>) supplier.get();
} catch (Throwable e) {
tryFail(e);
return;
Expand All @@ -54,7 +55,7 @@ public void onFailure(Throwable ignore) {
public void onFailure(Throwable failure) {
FutureInternal<U> future;
try {
future = (FutureInternal<U>) mapper.apply(null);
future = (FutureInternal<U>) supplier.get();
} catch (Throwable e) {
tryFail(e);
return;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/io/vertx/core/impl/future/FutureBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* Future base implementation.
Expand Down Expand Up @@ -94,9 +95,9 @@ public <U> Future<U> transform(Function<AsyncResult<T>, Future<U>> mapper) {
}

@Override
public <U> Future<T> eventually(Function<Void, Future<U>> mapper) {
Objects.requireNonNull(mapper, "No null mapper accepted");
Eventually<T, U> operation = new Eventually<>(context, mapper);
public <U> Future<T> eventually(Supplier<Future<U>> supplier) {
Objects.requireNonNull(supplier, "No null supplier accepted");
Eventually<T, U> operation = new Eventually<>(context, supplier);
addListener(operation);
return operation;
}
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/io/vertx/core/FutureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
Expand Down Expand Up @@ -396,7 +397,7 @@ private void testEventuallySuccessTo(Consumer<Promise<Integer>> op) {
Future<Integer> c = p.future();
Promise<String> p3 = Promise.promise();
Future<String> f3 = p3.future();
Future<String> f4 = f3.eventually(v -> {
Future<String> f4 = f3.eventually(() -> {
cnt.incrementAndGet();
return c;
});
Expand Down Expand Up @@ -425,7 +426,7 @@ private void testEventuallyFailureTo(Consumer<Promise<Integer>> op) {
Future<Integer> c = p.future();
Promise<String> p3 = Promise.promise();
Future<String> f3 = p3.future();
Future<String> f4 = f3.eventually(v -> {
Future<String> f4 = f3.eventually(() -> {
cnt.incrementAndGet();
return c;
});
Expand Down Expand Up @@ -823,7 +824,7 @@ public boolean tryFail(Throwable cause) {
public boolean failed() { throw new UnsupportedOperationException(); }
public <U> Future<U> compose(Function<T, Future<U>> successMapper, Function<Throwable, Future<U>> failureMapper) { throw new UnsupportedOperationException(); }
public <U> Future<U> transform(Function<AsyncResult<T>, Future<U>> mapper) { throw new UnsupportedOperationException(); }
public <U> Future<T> eventually(Function<Void, Future<U>> mapper) { throw new UnsupportedOperationException(); }
public <U> Future<T> eventually(Supplier<Future<U>> mapper) { throw new UnsupportedOperationException(); }
public <U> Future<U> map(Function<T, U> mapper) { throw new UnsupportedOperationException(); }
public <V> Future<V> map(V value) { throw new UnsupportedOperationException(); }
public Future<T> otherwise(Function<Throwable, T> mapper) { throw new UnsupportedOperationException(); }
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/vertx/core/http/WebSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3609,7 +3609,7 @@ public void testWriteHandlerIdNullByDefault() throws Exception {
assertNull(ws.binaryHandlerID());
ws.binaryMessageHandler(data -> {
assertEquals(hello, data);
ws.writeBinaryMessage(bye).eventually(v -> ws.close());
ws.writeBinaryMessage(bye).eventually(ws::close);
});
});

Expand Down

0 comments on commit c9f555f

Please sign in to comment.