Skip to content

Commit 261c065

Browse files
authored
Make more packages NotNullApi by default (#1056)
JAVA-4359
1 parent 3387038 commit 261c065

File tree

180 files changed

+1302
-757
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

180 files changed

+1302
-757
lines changed

driver-core/src/main/com/mongodb/bulk/BulkWriteResult.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.List;
2222

23+
import static com.mongodb.assertions.Assertions.assertNotNull;
2324
import static java.util.Collections.emptyList;
2425
import static java.util.Collections.unmodifiableList;
2526

@@ -200,7 +201,7 @@ public int getDeletedCount() {
200201

201202
@Override
202203
public int getModifiedCount() {
203-
return modifiedCount;
204+
return assertNotNull(modifiedCount);
204205
}
205206

206207
@Override
@@ -230,7 +231,7 @@ public boolean equals(final Object o) {
230231
if (insertedCount != that.getInsertedCount()) {
231232
return false;
232233
}
233-
if (modifiedCount != null && !modifiedCount.equals(that.getModifiedCount())) {
234+
if (!modifiedCount.equals(that.getModifiedCount())) {
234235
return false;
235236
}
236237
if (removedCount != that.getDeletedCount()) {
@@ -256,7 +257,7 @@ public int hashCode() {
256257
result = 31 * result + insertedCount;
257258
result = 31 * result + matchedCount;
258259
result = 31 * result + removedCount;
259-
result = 31 * result + (modifiedCount != null ? modifiedCount.hashCode() : 0);
260+
result = 31 * result + modifiedCount.hashCode();
260261
return result;
261262
}
262263

driver-core/src/main/com/mongodb/bulk/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Contains classes for representing the result of a bulk write operation.
1919
*/
20+
@NonNullApi
2021
package com.mongodb.bulk;
22+
23+
import com.mongodb.lang.NonNullApi;

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

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

1717
package com.mongodb.connection;
1818

19+
import com.mongodb.lang.Nullable;
20+
1921
/**
2022
* Completion handler for asynchronous I/O.
2123
*
@@ -28,7 +30,7 @@ public interface AsyncCompletionHandler<T> {
2830
*
2931
* @param t the result of the completed operation
3032
*/
31-
void completed(T t);
33+
void completed(@Nullable T t);
3234

3335
/**
3436
* Invoked when an operation fails.

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public ClusterDescription(final ClusterConnectionMode connectionMode, final Clus
7171
*/
7272
public ClusterDescription(final ClusterConnectionMode connectionMode, final ClusterType type,
7373
final List<ServerDescription> serverDescriptions,
74-
final ClusterSettings clusterSettings,
75-
final ServerSettings serverSettings) {
74+
@Nullable final ClusterSettings clusterSettings,
75+
@Nullable final ServerSettings serverSettings) {
7676
this(connectionMode, type, null, serverDescriptions, clusterSettings, serverSettings);
7777
}
7878

@@ -88,10 +88,10 @@ public ClusterDescription(final ClusterConnectionMode connectionMode, final Clus
8888
* @since 3.10
8989
*/
9090
public ClusterDescription(final ClusterConnectionMode connectionMode, final ClusterType type,
91-
final MongoException srvResolutionException,
91+
@Nullable final MongoException srvResolutionException,
9292
final List<ServerDescription> serverDescriptions,
93-
final ClusterSettings clusterSettings,
94-
final ServerSettings serverSettings) {
93+
@Nullable final ClusterSettings clusterSettings,
94+
@Nullable final ServerSettings serverSettings) {
9595
notNull("all", serverDescriptions);
9696
this.connectionMode = notNull("connectionMode", connectionMode);
9797
this.type = notNull("type", type);
@@ -322,6 +322,7 @@ public String getShortDescription() {
322322
}
323323
}
324324

325+
@Nullable
325326
private Integer calculateLogicalSessionTimeoutMinutes() {
326327
Integer retVal = null;
327328

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.connection;
1818

1919
import com.mongodb.internal.VisibleForTesting;
20+
import com.mongodb.lang.Nullable;
2021
import org.bson.types.ObjectId;
2122

2223
import java.util.Objects;
@@ -47,7 +48,7 @@ public ClusterId() {
4748
*
4849
* @param description the user defined description of the MongoClient
4950
*/
50-
public ClusterId(final String description) {
51+
public ClusterId(@Nullable final String description) {
5152
this.value = new ObjectId().toHexString();
5253
this.description = description;
5354
}
@@ -72,6 +73,7 @@ public String getValue() {
7273
*
7374
* @return the user defined description of the MongoClient
7475
*/
76+
@Nullable
7577
public String getDescription() {
7678
return description;
7779
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public Builder mode(final ClusterConnectionMode mode) {
223223
* @param requiredReplicaSetName the required replica set name.
224224
* @return this
225225
*/
226-
public Builder requiredReplicaSetName(final String requiredReplicaSetName) {
226+
public Builder requiredReplicaSetName(@Nullable final String requiredReplicaSetName) {
227227
this.requiredReplicaSetName = requiredReplicaSetName;
228228
return this;
229229
}
@@ -328,9 +328,13 @@ public Builder applyConnectionString(final ConnectionString connectionString) {
328328
} else if (connectionString.isSrvProtocol()) {
329329
mode(ClusterConnectionMode.MULTIPLE);
330330
srvHost(connectionString.getHosts().get(0));
331-
srvMaxHosts(connectionString.getSrvMaxHosts());
332-
if (connectionString.getSrvServiceName() != null) {
333-
srvServiceName(connectionString.getSrvServiceName());
331+
Integer srvMaxHosts = connectionString.getSrvMaxHosts();
332+
if (srvMaxHosts != null) {
333+
srvMaxHosts(srvMaxHosts);
334+
}
335+
String srvServiceName = connectionString.getSrvServiceName();
336+
if (srvServiceName != null) {
337+
srvServiceName(srvServiceName);
334338
}
335339
} else if ((directConnection != null && directConnection)
336340
|| (directConnection == null && connectionString.getHosts().size() == 1

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public ConnectionDescription(final ConnectionId connectionId, final int maxWireV
9393
*/
9494
public ConnectionDescription(final ConnectionId connectionId, final int maxWireVersion,
9595
final ServerType serverType, final int maxBatchCount, final int maxDocumentSize,
96-
final int maxMessageSize, final List<String> compressors, final BsonArray saslSupportedMechanisms) {
96+
final int maxMessageSize, final List<String> compressors,
97+
@Nullable final BsonArray saslSupportedMechanisms) {
9798
this(null, connectionId, maxWireVersion, serverType, maxBatchCount, maxDocumentSize, maxMessageSize, compressors,
9899
saslSupportedMechanisms);
99100
}
@@ -114,7 +115,8 @@ public ConnectionDescription(final ConnectionId connectionId, final int maxWireV
114115
*/
115116
public ConnectionDescription(@Nullable final ObjectId serviceId, final ConnectionId connectionId, final int maxWireVersion,
116117
final ServerType serverType, final int maxBatchCount, final int maxDocumentSize,
117-
final int maxMessageSize, final List<String> compressors, final BsonArray saslSupportedMechanisms) {
118+
final int maxMessageSize, final List<String> compressors,
119+
@Nullable final BsonArray saslSupportedMechanisms) {
118120
this.serviceId = serviceId;
119121
this.connectionId = connectionId;
120122
this.serverType = serverType;
@@ -241,6 +243,7 @@ public List<String> getCompressors() {
241243
* @return the supported SASL mechanisms.
242244
* @since 4.1
243245
*/
246+
@Nullable
244247
public BsonArray getSaslSupportedMechanisms() {
245248
return saslSupportedMechanisms;
246249
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public static Builder builder(final ServerDescription serverDescription) {
123123
*
124124
* @return the host name and port that this replica set member is configured with.
125125
*/
126+
@Nullable
126127
public String getCanonicalAddress() {
127128
return canonicalAddress;
128129
}
@@ -228,7 +229,7 @@ public Builder address(final ServerAddress address) {
228229
*
229230
* @return this
230231
*/
231-
public Builder canonicalAddress(final String canonicalAddress) {
232+
public Builder canonicalAddress(@Nullable final String canonicalAddress) {
232233
this.canonicalAddress = canonicalAddress;
233234
return this;
234235
}
@@ -251,7 +252,7 @@ public Builder type(final ServerType type) {
251252
* hidden, passive, nor arbiters.
252253
* @return this
253254
*/
254-
public Builder hosts(final Set<String> hosts) {
255+
public Builder hosts(@Nullable final Set<String> hosts) {
255256
this.hosts = hosts == null ? Collections.emptySet() : Collections.unmodifiableSet(new HashSet<>(hosts));
256257
return this;
257258
}
@@ -263,7 +264,7 @@ public Builder hosts(final Set<String> hosts) {
263264
* priority of 0.
264265
* @return this
265266
*/
266-
public Builder passives(final Set<String> passives) {
267+
public Builder passives(@Nullable final Set<String> passives) {
267268
this.passives = passives == null ? Collections.emptySet() : Collections.unmodifiableSet(new HashSet<>(passives));
268269
return this;
269270
}
@@ -275,7 +276,7 @@ public Builder passives(final Set<String> passives) {
275276
* arbiters.
276277
* @return this
277278
*/
278-
public Builder arbiters(final Set<String> arbiters) {
279+
public Builder arbiters(@Nullable final Set<String> arbiters) {
279280
this.arbiters = arbiters == null ? Collections.emptySet() : Collections.unmodifiableSet(new HashSet<>(arbiters));
280281
return this;
281282
}
@@ -286,7 +287,7 @@ public Builder arbiters(final Set<String> arbiters) {
286287
* @param primary A string in the format of "[hostname]:[port]" listing the current primary member of the replica set.
287288
* @return this
288289
*/
289-
public Builder primary(final String primary) {
290+
public Builder primary(@Nullable final String primary) {
290291
this.primary = primary;
291292
return this;
292293
}
@@ -308,7 +309,7 @@ public Builder maxDocumentSize(final int maxDocumentSize) {
308309
* @param tagSet a TagSet with all the tags for this server.
309310
* @return this
310311
*/
311-
public Builder tagSet(final TagSet tagSet) {
312+
public Builder tagSet(@Nullable final TagSet tagSet) {
312313
this.tagSet = tagSet == null ? new TagSet() : tagSet;
313314
return this;
314315
}
@@ -331,7 +332,7 @@ public Builder roundTripTime(final long roundTripTime, final TimeUnit timeUnit)
331332
* @param setName the name of the replica set
332333
* @return this
333334
*/
334-
public Builder setName(final String setName) {
335+
public Builder setName(@Nullable final String setName) {
335336
this.setName = setName;
336337
return this;
337338
}
@@ -386,7 +387,7 @@ public Builder maxWireVersion(final int maxWireVersion) {
386387
* @param electionId the electionId
387388
* @return this
388389
*/
389-
public Builder electionId(final ObjectId electionId) {
390+
public Builder electionId(@Nullable final ObjectId electionId) {
390391
this.electionId = electionId;
391392
return this;
392393
}
@@ -397,7 +398,7 @@ public Builder electionId(final ObjectId electionId) {
397398
* @param setVersion the set version
398399
* @return this
399400
*/
400-
public Builder setVersion(final Integer setVersion) {
401+
public Builder setVersion(@Nullable final Integer setVersion) {
401402
this.setVersion = setVersion;
402403
return this;
403404
}
@@ -410,7 +411,7 @@ public Builder setVersion(final Integer setVersion) {
410411
* @since 4.1
411412
* @mongodb.server.release 4.4
412413
*/
413-
public Builder topologyVersion(final TopologyVersion topologyVersion) {
414+
public Builder topologyVersion(@Nullable final TopologyVersion topologyVersion) {
414415
this.topologyVersion = topologyVersion;
415416
return this;
416417
}
@@ -660,6 +661,7 @@ public Set<String> getArbiters() {
660661
*
661662
* @return A string in the format of "[hostname]:[port]" listing the current primary member of the replica set.
662663
*/
664+
@Nullable
663665
public String getPrimary() {
664666
return primary;
665667
}
@@ -727,6 +729,7 @@ public Integer getSetVersion() {
727729
* @since 4.1
728730
* @mongodb.server.release 4.4
729731
*/
732+
@Nullable
730733
public TopologyVersion getTopologyVersion() {
731734
return topologyVersion;
732735
}
@@ -779,6 +782,7 @@ public boolean hasTags(final TagSet desiredTags) {
779782
*
780783
* @return the name of the replica set
781784
*/
785+
@Nullable
782786
public String getSetName() {
783787
return setName;
784788
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import com.mongodb.internal.connection.PowerOfTwoBufferPool;
2323
import com.mongodb.internal.connection.SocketStream;
2424
import com.mongodb.internal.connection.UnixSocketChannelStream;
25+
import com.mongodb.lang.Nullable;
2526

2627
import javax.net.SocketFactory;
2728
import javax.net.ssl.SSLContext;
2829
import java.security.NoSuchAlgorithmException;
2930

3031
import static com.mongodb.assertions.Assertions.notNull;
32+
import static java.util.Optional.ofNullable;
3133

3234
/**
3335
* Factory for creating instances of {@code SocketStream}.
@@ -57,7 +59,7 @@ public SocketStreamFactory(final SocketSettings settings, final SslSettings sslS
5759
* @param sslSettings the SSL for connecting to a MongoDB server
5860
* @param socketFactory a SocketFactory for creating connections to servers.
5961
*/
60-
public SocketStreamFactory(final SocketSettings settings, final SslSettings sslSettings, final SocketFactory socketFactory) {
62+
public SocketStreamFactory(final SocketSettings settings, final SslSettings sslSettings, @Nullable final SocketFactory socketFactory) {
6163
this.settings = notNull("settings", settings);
6264
this.sslSettings = notNull("sslSettings", sslSettings);
6365
this.socketFactory = socketFactory;
@@ -85,7 +87,7 @@ public Stream create(final ServerAddress serverAddress) {
8587

8688
private SSLContext getSslContext() {
8789
try {
88-
return (sslSettings.getContext() == null) ? SSLContext.getDefault() : sslSettings.getContext();
90+
return ofNullable(sslSettings.getContext()).orElse(SSLContext.getDefault());
8991
} catch (NoSuchAlgorithmException e) {
9092
throw new MongoClientException("Unable to create default SSLContext", e);
9193
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.ConnectionString;
2020
import com.mongodb.annotations.Immutable;
2121
import com.mongodb.annotations.NotThreadSafe;
22+
import com.mongodb.lang.Nullable;
2223

2324
import javax.net.ssl.SSLContext;
2425
import java.util.Objects;
@@ -180,6 +181,7 @@ public boolean isInvalidHostNameAllowed() {
180181
* @since 3.5
181182
* @see SSLContext#getDefault()
182183
*/
184+
@Nullable
183185
public SSLContext getContext() {
184186
return context;
185187
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.mongodb.internal.connection.tlschannel.async.AsynchronousTlsChannelGroup;
3030
import com.mongodb.internal.diagnostics.logging.Logger;
3131
import com.mongodb.internal.diagnostics.logging.Loggers;
32+
import com.mongodb.lang.Nullable;
3233

3334
import javax.net.ssl.SSLContext;
3435
import javax.net.ssl.SSLEngine;
@@ -50,6 +51,7 @@
5051
import static com.mongodb.assertions.Assertions.isTrue;
5152
import static com.mongodb.internal.connection.SslHelper.enableHostNameVerification;
5253
import static com.mongodb.internal.connection.SslHelper.enableSni;
54+
import static java.util.Optional.ofNullable;
5355

5456
/**
5557
* A {@code StreamFactoryFactory} that supports TLS/SSL. The implementation supports asynchronous usage.
@@ -257,7 +259,7 @@ public void openAsync(final AsyncCompletionHandler<Void> handler) {
257259

258260
private SSLContext getSslContext() {
259261
try {
260-
return (sslSettings.getContext() == null) ? SSLContext.getDefault() : sslSettings.getContext();
262+
return ofNullable(sslSettings.getContext()).orElse(SSLContext.getDefault());
261263
} catch (NoSuchAlgorithmException e) {
262264
throw new MongoClientException("Unable to create default SSLContext", e);
263265
}
@@ -288,14 +290,14 @@ public <A> void read(final ByteBuffer dst, final A attach, final CompletionHandl
288290
}
289291

290292
@Override
291-
public <A> void read(final ByteBuffer dst, final long timeout, final TimeUnit unit, final A attach,
293+
public <A> void read(final ByteBuffer dst, final long timeout, final TimeUnit unit, @Nullable final A attach,
292294
final CompletionHandler<Integer, ? super A> handler) {
293295
wrapped.read(dst, timeout, unit, attach, handler);
294296
}
295297

296298
@Override
297299
public <A> void read(final ByteBuffer[] dsts, final int offset, final int length, final long timeout, final TimeUnit unit,
298-
final A attach, final CompletionHandler<Long, ? super A> handler) {
300+
@Nullable final A attach, final CompletionHandler<Long, ? super A> handler) {
299301
wrapped.read(dsts, offset, length, timeout, unit, attach, handler);
300302
}
301303

0 commit comments

Comments
 (0)