Skip to content

Commit 36a30f1

Browse files
committed
Update async functions
1 parent 4c1b6b0 commit 36a30f1

File tree

5 files changed

+662
-106
lines changed

5 files changed

+662
-106
lines changed

driver-core/src/main/com/mongodb/connection/AsyncCompletionHandler.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.connection;
1818

19+
import com.mongodb.internal.async.SingleResultCallback;
1920
import com.mongodb.lang.Nullable;
2021

2122
/**
@@ -38,4 +39,17 @@ public interface AsyncCompletionHandler<T> {
3839
* @param t the exception that describes the failure
3940
*/
4041
void failed(Throwable t);
42+
43+
/**
44+
* @return this handler as a callback
45+
*/
46+
default SingleResultCallback<T> asCallback() {
47+
return (r, t) -> {
48+
if (t != null) {
49+
failed(t);
50+
} else {
51+
completed(r);
52+
}
53+
};
54+
}
4155
}

driver-core/src/main/com/mongodb/internal/async/AsyncFunction.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.mongodb.internal.async;
1818

19-
import com.mongodb.lang.Nullable;
20-
2119
/**
2220
* See tests for usage (AsyncFunctionsTest).
2321
* <p>
@@ -29,5 +27,5 @@ public interface AsyncFunction<T, R> {
2927
* This should not be called externally, but should be implemented as a
3028
* lambda. To "finish" an async chain, use one of the "finish" methods.
3129
*/
32-
void unsafeFinish(@Nullable T value, SingleResultCallback<R> callback);
30+
void unsafeFinish(T value, SingleResultCallback<R> callback);
3331
}

driver-core/src/main/com/mongodb/internal/async/AsyncSupplier.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.mongodb.internal.async;
1818

19-
import com.mongodb.lang.Nullable;
20-
2119
import java.util.function.Predicate;
2220

2321

@@ -38,6 +36,7 @@ public interface AsyncSupplier<T> extends AsyncFunction<Void, T> {
3836
void unsafeFinish(SingleResultCallback<T> callback);
3937

4038
/**
39+
* This is the async variant of a supplier's get method.
4140
* This method must only be used when this AsyncSupplier corresponds
4241
* to a {@link java.util.function.Supplier} (and is therefore being
4342
* used within an async chain method lambda).
@@ -48,7 +47,7 @@ default void getAsync(final SingleResultCallback<T> callback) {
4847
}
4948

5049
@Override
51-
default void unsafeFinish(@Nullable final Void value, final SingleResultCallback<T> callback) {
50+
default void unsafeFinish(final Void value, final SingleResultCallback<T> callback) {
5251
unsafeFinish(callback);
5352
}
5453

@@ -108,13 +107,13 @@ default AsyncRunnable thenConsume(final AsyncConsumer<T> consumer) {
108107

109108
/**
110109
* @param errorCheck A check, comparable to a catch-if/otherwise-rethrow
111-
* @param supplier The branch to execute if the error matches
110+
* @param errorFunction The branch to execute if the error matches
112111
* @return The composition of this, and the conditional branch
113112
*/
114113
default AsyncSupplier<T> onErrorIf(
115114
final Predicate<Throwable> errorCheck,
116-
final AsyncSupplier<T> supplier) {
117-
return (callback) -> this.unsafeFinish((r, e) -> {
115+
final AsyncFunction<Throwable, T> errorFunction) {
116+
return (callback) -> this.finish((r, e) -> {
118117
if (e == null) {
119118
callback.onResult(r, null);
120119
return;
@@ -128,7 +127,7 @@ default AsyncSupplier<T> onErrorIf(
128127
return;
129128
}
130129
if (errorMatched) {
131-
supplier.unsafeFinish(callback);
130+
errorFunction.unsafeFinish(e, callback);
132131
} else {
133132
callback.onResult(null, e);
134133
}

driver-core/src/main/com/mongodb/internal/async/SingleResultCallback.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.mongodb.internal.async;
1818

19+
import com.mongodb.assertions.Assertions;
20+
import com.mongodb.connection.AsyncCompletionHandler;
1921
import com.mongodb.internal.async.function.AsyncCallbackFunction;
2022
import com.mongodb.lang.Nullable;
2123

@@ -34,4 +36,32 @@ public interface SingleResultCallback<T> {
3436
* @throws Error Never, on the best effort basis.
3537
*/
3638
void onResult(@Nullable T result, @Nullable Throwable t);
39+
40+
/**
41+
* @return this callback as a handler
42+
*/
43+
default AsyncCompletionHandler<T> asHandler() {
44+
return new AsyncCompletionHandler<T>() {
45+
@Override
46+
public void completed(@Nullable final T result) {
47+
onResult(result, null);
48+
}
49+
@Override
50+
public void failed(final Throwable t) {
51+
onResult(null, t);
52+
}
53+
};
54+
}
55+
56+
default void complete(final SingleResultCallback<Void> callback) {
57+
// takes a void callback (itself) to help ensure that this method
58+
// is not accidentally used when "complete(T)" should have been used
59+
// instead, since results are not marked nullable.
60+
Assertions.assertTrue(callback == this);
61+
this.onResult(null, null);
62+
}
63+
64+
default void complete(final T result) {
65+
this.onResult(result, null);
66+
}
3767
}

0 commit comments

Comments
 (0)