Skip to content

Commit ab4a6a1

Browse files
committed
JAVA-2641: Make com.mongodb a NonNullApi and add Nullable annotations to methods and parameters that require it
1 parent 4379ce1 commit ab4a6a1

File tree

68 files changed

+708
-366
lines changed

Some content is hidden

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

68 files changed

+708
-366
lines changed

driver-async/src/main/com/mongodb/async/client/MongoClientSettings.java

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -93,45 +93,27 @@ private Builder(final MongoClientSettings settings) {
9393
credentialList = new ArrayList<MongoCredential>(settings.credentialList);
9494

9595
wrappedBuilder.commandListenerList(new ArrayList<CommandListener>(settings.getCommandListeners()));
96-
if (settings.getCodecRegistry() != null) {
97-
wrappedBuilder.codecRegistry(settings.getCodecRegistry());
98-
}
99-
if (settings.getReadPreference() != null) {
100-
wrappedBuilder.readPreference(settings.getReadPreference());
101-
}
102-
if (settings.getWriteConcern() != null) {
103-
wrappedBuilder.writeConcern(settings.getWriteConcern());
104-
}
96+
wrappedBuilder.codecRegistry(settings.getCodecRegistry());
97+
wrappedBuilder.readPreference(settings.getReadPreference());
98+
wrappedBuilder.writeConcern(settings.getWriteConcern());
10599
wrappedBuilder.retryWrites(settings.getRetryWrites());
106-
if (settings.getReadConcern() != null) {
107-
wrappedBuilder.readConcern(settings.getReadConcern());
108-
}
109-
if (settings.getCredential() != null) {
110-
wrappedBuilder.credential(settings.getCredential());
111-
}
112-
if (settings.getStreamFactoryFactory() != null) {
113-
wrappedBuilder.streamFactoryFactory(settings.getStreamFactoryFactory());
114-
}
115-
if (settings.getClusterSettings() != null) {
116-
clusterSettings(settings.getClusterSettings());
117-
}
118-
if (settings.getServerSettings() != null) {
119-
serverSettings(settings.getServerSettings());
120-
}
121-
if (settings.getSocketSettings() != null) {
122-
socketSettings(settings.getSocketSettings());
100+
wrappedBuilder.readConcern(settings.getReadConcern());
101+
wrappedBuilder.applicationName(settings.getApplicationName());
102+
wrappedBuilder.compressorList(new ArrayList<MongoCompressor>(settings.getCompressorList()));
103+
104+
MongoCredential credential = settings.getCredential();
105+
if (credential != null) {
106+
wrappedBuilder.credential(credential);
123107
}
108+
109+
clusterSettings(settings.getClusterSettings());
110+
serverSettings(settings.getServerSettings());
111+
socketSettings(settings.getSocketSettings());
124112
if (settings.heartbeatSocketSettings != null) {
125113
heartbeatSocketSettings(settings.heartbeatSocketSettings);
126114
}
127-
if (settings.getConnectionPoolSettings() != null) {
128-
connectionPoolSettings(settings.getConnectionPoolSettings());
129-
}
130-
if (settings.getSslSettings() != null) {
131-
sslSettings(settings.getSslSettings());
132-
}
133-
wrappedBuilder.applicationName(settings.getApplicationName());
134-
wrappedBuilder.compressorList(new ArrayList<MongoCompressor>(settings.getCompressorList()));
115+
connectionPoolSettings(settings.getConnectionPoolSettings());
116+
sslSettings(settings.getSslSettings());
135117
}
136118

137119
/**
@@ -510,11 +492,12 @@ public List<MongoCredential> getCredentialList() {
510492
}
511493

512494
/**
513-
* Gets the credential list.
495+
* Gets the credential.
514496
*
515-
* @return the credential list
497+
* @return the credentia, which may be null
516498
* @since 3.6
517499
*/
500+
@Nullable
518501
public MongoCredential getCredential() {
519502
isTrue("Single or no credential", credentialList.size() <= 1);
520503
return wrapped.getCredential();
@@ -598,6 +581,7 @@ public List<CommandListener> getCommandListeners() {
598581
* @since 3.4
599582
* @mongodb.server.release 3.4
600583
*/
584+
@Nullable
601585
public String getApplicationName() {
602586
return wrapped.getApplicationName();
603587
}

driver-core/src/main/com/mongodb/ClientSessionOptions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.annotations.Immutable;
2020
import com.mongodb.annotations.NotThreadSafe;
21+
import com.mongodb.lang.Nullable;
2122
import com.mongodb.session.ClientSession;
2223

2324
/**
@@ -40,6 +41,7 @@ public final class ClientSessionOptions {
4041
* which is currently true.
4142
* @mongodb.driver.dochub core/causal-consistency Causal Consistency
4243
*/
44+
@Nullable
4345
public Boolean isCausallyConsistent() {
4446
return causallyConsistent;
4547
}

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.diagnostics.logging.Logger;
2020
import com.mongodb.diagnostics.logging.Loggers;
21+
import com.mongodb.lang.Nullable;
2122

2223
import java.io.UnsupportedEncodingException;
2324
import java.net.URLDecoder;
@@ -510,7 +511,7 @@ private List<MongoCompressor> createCompressors(final Map<String, List<String>>
510511
return buildCompressors(compressors, zlibCompressionLevel);
511512
}
512513

513-
private List<MongoCompressor> buildCompressors(final String compressors, final Integer zlibCompressionLevel) {
514+
private List<MongoCompressor> buildCompressors(final String compressors, @Nullable final Integer zlibCompressionLevel) {
514515
List<MongoCompressor> compressorsList = new ArrayList<MongoCompressor>();
515516

516517
for (String cur : compressors.split(",")) {
@@ -530,6 +531,7 @@ private List<MongoCompressor> buildCompressors(final String compressors, final I
530531
return unmodifiableList(compressorsList);
531532
}
532533

534+
@Nullable
533535
private WriteConcern createWriteConcern(final Map<String, List<String>> optionsMap) {
534536
Boolean safe = null;
535537
String w = null;
@@ -558,6 +560,7 @@ private WriteConcern createWriteConcern(final Map<String, List<String>> optionsM
558560
return buildWriteConcern(safe, w, wTimeout, fsync, journal);
559561
}
560562

563+
@Nullable
561564
private ReadPreference createReadPreference(final Map<String, List<String>> optionsMap) {
562565
String readPreferenceType = null;
563566
List<TagSet> tagSetList = new ArrayList<TagSet>();
@@ -583,8 +586,9 @@ private ReadPreference createReadPreference(final Map<String, List<String>> opti
583586
return buildReadPreference(readPreferenceType, tagSetList, maxStalenessSeconds);
584587
}
585588

586-
private MongoCredential createCredentials(final Map<String, List<String>> optionsMap, final String userName,
587-
final char[] password) {
589+
@Nullable
590+
private MongoCredential createCredentials(final Map<String, List<String>> optionsMap, @Nullable final String userName,
591+
@Nullable final char[] password) {
588592
AuthenticationMechanism mechanism = null;
589593
String authSource = (database == null) ? "admin" : database;
590594
String gssapiServiceName = null;
@@ -637,8 +641,8 @@ private MongoCredential createCredentials(final Map<String, List<String>> option
637641

638642
@SuppressWarnings("deprecation")
639643
private MongoCredential createMongoCredentialWithMechanism(final AuthenticationMechanism mechanism, final String userName,
640-
final char[] password, final String authSource,
641-
final String gssapiServiceName) {
644+
@Nullable final char[] password, final String authSource,
645+
@Nullable final String gssapiServiceName) {
642646
MongoCredential credential;
643647
switch (mechanism) {
644648
case GSSAPI:
@@ -667,6 +671,7 @@ private MongoCredential createMongoCredentialWithMechanism(final AuthenticationM
667671
return credential;
668672
}
669673

674+
@Nullable
670675
private String getLastValue(final Map<String, List<String>> optionsMap, final String key) {
671676
List<String> valueList = optionsMap.get(key);
672677
if (valueList == null) {
@@ -709,8 +714,9 @@ private Map<String, List<String>> parseOptions(final String optionsPart) {
709714
}
710715
}
711716
// handle legacy slaveok settings
712-
if (optionsMap.containsKey("slaveok") && !optionsMap.containsKey("readpreference")) {
713-
String readPreference = parseBoolean(getLastValue(optionsMap, "slaveok"), "slaveok")
717+
String slaveok = getLastValue(optionsMap, "slaveok");
718+
if (slaveok != null && !optionsMap.containsKey("readpreference")) {
719+
String readPreference = parseBoolean(slaveok, "slaveok")
714720
? "secondaryPreferred" : "primary";
715721
optionsMap.put("readpreference", singletonList(readPreference));
716722
if (LOGGER.isWarnEnabled()) {
@@ -728,7 +734,8 @@ private Map<String, List<String>> parseOptions(final String optionsPart) {
728734
return optionsMap;
729735
}
730736

731-
private ReadPreference buildReadPreference(final String readPreferenceType,
737+
@Nullable
738+
private ReadPreference buildReadPreference(@Nullable final String readPreferenceType,
732739
final List<TagSet> tagSetList, final long maxStalenessSeconds) {
733740
if (readPreferenceType != null) {
734741
if (tagSetList.isEmpty() && maxStalenessSeconds == -1) {
@@ -746,8 +753,10 @@ private ReadPreference buildReadPreference(final String readPreferenceType,
746753
}
747754

748755
@SuppressWarnings("deprecation")
749-
private WriteConcern buildWriteConcern(final Boolean safe, final String w,
750-
final Integer wTimeout, final Boolean fsync, final Boolean journal) {
756+
@Nullable
757+
private WriteConcern buildWriteConcern(@Nullable final Boolean safe, @Nullable final String w,
758+
@Nullable final Integer wTimeout, @Nullable final Boolean fsync,
759+
@Nullable final Boolean journal) {
751760
WriteConcern retVal = null;
752761
if (w != null || wTimeout != null || fsync != null || journal != null) {
753762
if (w == null) {
@@ -904,6 +913,7 @@ private String urldecode(final String input, final boolean password) {
904913
*
905914
* @return the username
906915
*/
916+
@Nullable
907917
public String getUsername() {
908918
return credential != null ? credential.getUserName() : null;
909919
}
@@ -913,6 +923,7 @@ public String getUsername() {
913923
*
914924
* @return the password
915925
*/
926+
@Nullable
916927
public char[] getPassword() {
917928
return credential != null ? credential.getPassword() : null;
918929
}
@@ -931,6 +942,7 @@ public List<String> getHosts() {
931942
*
932943
* @return the database name
933944
*/
945+
@Nullable
934946
public String getDatabase() {
935947
return database;
936948
}
@@ -941,6 +953,7 @@ public String getDatabase() {
941953
*
942954
* @return the collection name
943955
*/
956+
@Nullable
944957
public String getCollection() {
945958
return collection;
946959
}
@@ -984,6 +997,7 @@ public List<MongoCredential> getCredentialList() {
984997
* @return the credentials in an immutable list
985998
* @since 3.6
986999
*/
1000+
@Nullable
9871001
public MongoCredential getCredential() {
9881002
return credential;
9891003
}
@@ -992,6 +1006,7 @@ public MongoCredential getCredential() {
9921006
* Gets the read preference specified in the connection string.
9931007
* @return the read preference
9941008
*/
1009+
@Nullable
9951010
public ReadPreference getReadPreference() {
9961011
return readPreference;
9971012
}
@@ -1000,6 +1015,7 @@ public ReadPreference getReadPreference() {
10001015
* Gets the read concern specified in the connection string.
10011016
* @return the read concern
10021017
*/
1018+
@Nullable
10031019
public ReadConcern getReadConcern() {
10041020
return readConcern;
10051021
}
@@ -1008,6 +1024,7 @@ public ReadConcern getReadConcern() {
10081024
* Gets the write concern specified in the connection string.
10091025
* @return the write concern
10101026
*/
1027+
@Nullable
10111028
public WriteConcern getWriteConcern() {
10121029
return writeConcern;
10131030
}
@@ -1027,6 +1044,7 @@ public boolean getRetryWrites() {
10271044
* Gets the minimum connection pool size specified in the connection string.
10281045
* @return the minimum connection pool size
10291046
*/
1047+
@Nullable
10301048
public Integer getMinConnectionPoolSize() {
10311049
return minConnectionPoolSize;
10321050
}
@@ -1035,6 +1053,7 @@ public Integer getMinConnectionPoolSize() {
10351053
* Gets the maximum connection pool size specified in the connection string.
10361054
* @return the maximum connection pool size
10371055
*/
1056+
@Nullable
10381057
public Integer getMaxConnectionPoolSize() {
10391058
return maxConnectionPoolSize;
10401059
}
@@ -1043,6 +1062,7 @@ public Integer getMaxConnectionPoolSize() {
10431062
* Gets the multiplier for the number of threads allowed to block waiting for a connection specified in the connection string.
10441063
* @return the multiplier for the number of threads allowed to block waiting for a connection
10451064
*/
1065+
@Nullable
10461066
public Integer getThreadsAllowedToBlockForConnectionMultiplier() {
10471067
return threadsAllowedToBlockForConnectionMultiplier;
10481068
}
@@ -1051,6 +1071,7 @@ public Integer getThreadsAllowedToBlockForConnectionMultiplier() {
10511071
* Gets the maximum wait time of a thread waiting for a connection specified in the connection string.
10521072
* @return the maximum wait time of a thread waiting for a connection
10531073
*/
1074+
@Nullable
10541075
public Integer getMaxWaitTime() {
10551076
return maxWaitTime;
10561077
}
@@ -1059,6 +1080,7 @@ public Integer getMaxWaitTime() {
10591080
* Gets the maximum connection idle time specified in the connection string.
10601081
* @return the maximum connection idle time
10611082
*/
1083+
@Nullable
10621084
public Integer getMaxConnectionIdleTime() {
10631085
return maxConnectionIdleTime;
10641086
}
@@ -1067,6 +1089,7 @@ public Integer getMaxConnectionIdleTime() {
10671089
* Gets the maximum connection life time specified in the connection string.
10681090
* @return the maximum connection life time
10691091
*/
1092+
@Nullable
10701093
public Integer getMaxConnectionLifeTime() {
10711094
return maxConnectionLifeTime;
10721095
}
@@ -1075,6 +1098,7 @@ public Integer getMaxConnectionLifeTime() {
10751098
* Gets the socket connect timeout specified in the connection string.
10761099
* @return the socket connect timeout
10771100
*/
1101+
@Nullable
10781102
public Integer getConnectTimeout() {
10791103
return connectTimeout;
10801104
}
@@ -1083,6 +1107,7 @@ public Integer getConnectTimeout() {
10831107
* Gets the socket timeout specified in the connection string.
10841108
* @return the socket timeout
10851109
*/
1110+
@Nullable
10861111
public Integer getSocketTimeout() {
10871112
return socketTimeout;
10881113
}
@@ -1091,6 +1116,7 @@ public Integer getSocketTimeout() {
10911116
* Gets the SSL enabled value specified in the connection string.
10921117
* @return the SSL enabled value
10931118
*/
1119+
@Nullable
10941120
public Boolean getSslEnabled() {
10951121
return sslEnabled;
10961122
}
@@ -1100,6 +1126,7 @@ public Boolean getSslEnabled() {
11001126
* @return the stream type value
11011127
* @since 3.3
11021128
*/
1129+
@Nullable
11031130
public String getStreamType() {
11041131
return streamType;
11051132
}
@@ -1110,6 +1137,7 @@ public String getStreamType() {
11101137
* @return the SSL invalidHostnameAllowed value
11111138
* @since 3.3
11121139
*/
1140+
@Nullable
11131141
public Boolean getSslInvalidHostnameAllowed() {
11141142
return sslInvalidHostnameAllowed;
11151143
}
@@ -1118,6 +1146,7 @@ public Boolean getSslInvalidHostnameAllowed() {
11181146
* Gets the required replica set name specified in the connection string.
11191147
* @return the required replica set name
11201148
*/
1149+
@Nullable
11211150
public String getRequiredReplicaSetName() {
11221151
return requiredReplicaSetName;
11231152
}
@@ -1127,6 +1156,7 @@ public String getRequiredReplicaSetName() {
11271156
* @return the server selection timeout (in milliseconds), or null if unset
11281157
* @since 3.3
11291158
*/
1159+
@Nullable
11301160
public Integer getServerSelectionTimeout() {
11311161
return serverSelectionTimeout;
11321162
}
@@ -1136,6 +1166,7 @@ public Integer getServerSelectionTimeout() {
11361166
* @return the local threshold (in milliseconds), or null if unset
11371167
* since 3.3
11381168
*/
1169+
@Nullable
11391170
public Integer getLocalThreshold() {
11401171
return localThreshold;
11411172
}
@@ -1145,6 +1176,7 @@ public Integer getLocalThreshold() {
11451176
* @return the heartbeat frequency (in milliseconds), or null if unset
11461177
* since 3.3
11471178
*/
1179+
@Nullable
11481180
public Integer getHeartbeatFrequency() {
11491181
return heartbeatFrequency;
11501182
}
@@ -1159,6 +1191,7 @@ public Integer getHeartbeatFrequency() {
11591191
* @since 3.4
11601192
* @mongodb.server.release 3.4
11611193
*/
1194+
@Nullable
11621195
public String getApplicationName() {
11631196
return applicationName;
11641197
}

0 commit comments

Comments
 (0)