Skip to content

Commit 4814e43

Browse files
committed
Remove Nullable from async code
1 parent 9b8ac4a commit 4814e43

File tree

7 files changed

+20
-21
lines changed

7 files changed

+20
-21
lines changed

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

+1-3
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

+1-3
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

@@ -49,7 +47,7 @@ default void getAsync(final SingleResultCallback<T> callback) {
4947
}
5048

5149
@Override
52-
default void unsafeFinish(@Nullable final Void value, final SingleResultCallback<T> callback) {
50+
default void unsafeFinish(final Void value, final SingleResultCallback<T> callback) {
5351
unsafeFinish(callback);
5452
}
5553

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.internal.async;
1818

19+
import com.mongodb.assertions.Assertions;
1920
import com.mongodb.connection.AsyncCompletionHandler;
2021
import com.mongodb.internal.async.function.AsyncCallbackFunction;
2122
import com.mongodb.lang.Nullable;
@@ -49,7 +50,11 @@ public void failed(final Throwable t) {
4950
};
5051
}
5152

52-
default void complete() {
53+
default void complete(final SingleResultCallback<Void> callback) {
54+
// takes a void callback (itself) to help ensure that this method
55+
// is not accidentally used when "complete(T)" should have been used
56+
// instead, since results are not marked nullable.
57+
Assertions.assertTrue(callback == this);
5358
this.onResult(null, null);
5459
}
5560

driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnection.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public void openAsync(final SingleResultCallback<Void> callback) {
245245
if (t instanceof MongoException) {
246246
throw (MongoException) t;
247247
} else {
248-
throw new MongoException(assertNotNull(t).toString(), t);
248+
throw new MongoException(t.toString(), t);
249249
}
250250
}).finish(callback);
251251
}

driver-core/src/main/com/mongodb/internal/connection/InternalStreamConnectionInitializer.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636

3737
import java.util.List;
3838

39-
import static com.mongodb.assertions.Assertions.assertNotNull;
4039
import static com.mongodb.assertions.Assertions.notNull;
4140
import static com.mongodb.internal.async.AsyncRunnable.beginAsync;
4241
import static com.mongodb.internal.connection.CommandHelper.HELLO;
@@ -109,9 +108,9 @@ public void startHandshakeAsync(final InternalConnection internalConnection,
109108
beginAsync().<BsonDocument>thenSupply(c2 -> {
110109
executeCommandAsync("admin", helloCommandDocument, clusterConnectionMode, serverApi, internalConnection, c2);
111110
}).onErrorIf(e -> e instanceof MongoException, (t, c2) -> {
112-
throw mapHelloException((MongoException) assertNotNull(t));
111+
throw mapHelloException((MongoException) t);
113112
}).thenApply((helloResult, c2) -> {
114-
setSpeculativeAuthenticateResponse(assertNotNull(helloResult));
113+
setSpeculativeAuthenticateResponse(helloResult);
115114
c2.complete(createInitializationDescription(helloResult, internalConnection, startTime));
116115
});
117116
}).finish(callback);

driver-core/src/test/unit/com/mongodb/internal/async/AsyncFunctionsTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void testBasicVariations2() {
159159
async(1, c);
160160
}).thenRun(c -> {
161161
plain(2);
162-
c.complete();
162+
c.complete(c);
163163
}).finish(callback);
164164
});
165165

@@ -419,7 +419,7 @@ void testPlain() {
419419
(callback) -> {
420420
beginAsync().thenRun(c -> {
421421
plain(1);
422-
c.complete();
422+
c.complete(c);
423423
}).finish(callback);
424424
});
425425
}
@@ -457,7 +457,7 @@ void testTryCatch() {
457457
async(1, c);
458458
}).onErrorIf(t -> true, (t, c) -> {
459459
plain(2);
460-
c.complete();
460+
c.complete(c);
461461
}).finish(callback);
462462
});
463463

@@ -602,7 +602,7 @@ void testTryCatchWithVariables() {
602602
final int[] i = new int[1];
603603
beginAsync().thenRun(c -> {
604604
i[0] = plainTest(0) ? 1 : 2;
605-
c.complete();
605+
c.complete(c);
606606
}).thenRun(c -> {
607607
beginAsync().<Integer>thenSupply(c2 -> {
608608
asyncReturns(i[0] + 10, c2);
@@ -818,7 +818,7 @@ void testDerivation() {
818818
};
819819
BiConsumer<Integer, SingleResultCallback<Void>> happyAsync = (i, c) -> {
820820
happySync.accept(i);
821-
c.complete();
821+
c.complete(c);
822822
};
823823

824824
// Standard nested async, no error handling:
@@ -955,12 +955,12 @@ private Integer syncReturns(final int i) {
955955
private void async(final int i, final SingleResultCallback<Void> callback) {
956956
if (throwExceptionsFromAsync) {
957957
sync(i);
958-
callback.complete();
958+
callback.complete(callback);
959959

960960
} else {
961961
try {
962962
sync(i);
963-
callback.complete();
963+
callback.complete(callback);
964964
} catch (Throwable t) {
965965
callback.onResult(null, t);
966966
}

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/ClientSessionBinding.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.mongodb.internal.binding.AsyncReadWriteBinding;
3131
import com.mongodb.internal.binding.TransactionContext;
3232
import com.mongodb.internal.connection.AsyncConnection;
33-
import com.mongodb.internal.connection.Connection;
3433
import com.mongodb.internal.connection.OperationContext;
3534
import com.mongodb.internal.session.ClientSessionContext;
3635
import com.mongodb.internal.session.SessionContext;
@@ -127,7 +126,7 @@ private void getConnectionSource(final AsyncSupplier<AsyncConnectionSource> conn
127126
c2.complete(source);
128127
}).finish(c);
129128
}).<AsyncConnectionSource>thenApply((source, c) -> {
130-
c.complete(new SessionBindingAsyncConnectionSource(assertNotNull(source)));
129+
c.complete(new SessionBindingAsyncConnectionSource(source));
131130
}).finish(callback);
132131
}
133132

@@ -204,7 +203,7 @@ public void getConnection(final SingleResultCallback<AsyncConnection> callback)
204203
beginAsync().<AsyncConnection>thenSupply(c2 -> {
205204
wrapped.getConnection(c2);
206205
}).<AsyncConnection>thenApply((connection, c2) -> {
207-
transactionContext.pinConnection(assertNotNull(connection), AsyncConnection::markAsPinned);
206+
transactionContext.pinConnection(connection, AsyncConnection::markAsPinned);
208207
c2.complete(connection);
209208
}).finish(c);
210209
}).finish(callback);

0 commit comments

Comments
 (0)