Skip to content

Make more packages NotNullApi by default #1056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions driver-core/src/main/com/mongodb/bulk/BulkWriteResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.List;

import static com.mongodb.assertions.Assertions.assertNotNull;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;

Expand Down Expand Up @@ -200,7 +201,7 @@ public int getDeletedCount() {

@Override
public int getModifiedCount() {
return modifiedCount;
return assertNotNull(modifiedCount);
}

@Override
Expand Down Expand Up @@ -230,7 +231,7 @@ public boolean equals(final Object o) {
if (insertedCount != that.getInsertedCount()) {
return false;
}
if (modifiedCount != null && !modifiedCount.equals(that.getModifiedCount())) {
if (!modifiedCount.equals(that.getModifiedCount())) {
return false;
}
if (removedCount != that.getDeletedCount()) {
Expand All @@ -256,7 +257,7 @@ public int hashCode() {
result = 31 * result + insertedCount;
result = 31 * result + matchedCount;
result = 31 * result + removedCount;
result = 31 * result + (modifiedCount != null ? modifiedCount.hashCode() : 0);
result = 31 * result + modifiedCount.hashCode();
return result;
}

Expand Down
3 changes: 3 additions & 0 deletions driver-core/src/main/com/mongodb/bulk/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
/**
* Contains classes for representing the result of a bulk write operation.
*/
@NonNullApi
package com.mongodb.bulk;

import com.mongodb.lang.NonNullApi;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.mongodb.connection;

import com.mongodb.lang.Nullable;

/**
* Completion handler for asynchronous I/O.
*
Expand All @@ -28,7 +30,7 @@ public interface AsyncCompletionHandler<T> {
*
* @param t the result of the completed operation
*/
void completed(T t);
void completed(@Nullable T t);

/**
* Invoked when an operation fails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public ClusterDescription(final ClusterConnectionMode connectionMode, final Clus
*/
public ClusterDescription(final ClusterConnectionMode connectionMode, final ClusterType type,
final List<ServerDescription> serverDescriptions,
final ClusterSettings clusterSettings,
final ServerSettings serverSettings) {
@Nullable final ClusterSettings clusterSettings,
@Nullable final ServerSettings serverSettings) {
this(connectionMode, type, null, serverDescriptions, clusterSettings, serverSettings);
}

Expand All @@ -88,10 +88,10 @@ public ClusterDescription(final ClusterConnectionMode connectionMode, final Clus
* @since 3.10
*/
public ClusterDescription(final ClusterConnectionMode connectionMode, final ClusterType type,
final MongoException srvResolutionException,
@Nullable final MongoException srvResolutionException,
final List<ServerDescription> serverDescriptions,
final ClusterSettings clusterSettings,
final ServerSettings serverSettings) {
@Nullable final ClusterSettings clusterSettings,
@Nullable final ServerSettings serverSettings) {
notNull("all", serverDescriptions);
this.connectionMode = notNull("connectionMode", connectionMode);
this.type = notNull("type", type);
Expand Down Expand Up @@ -322,6 +322,7 @@ public String getShortDescription() {
}
}

@Nullable
private Integer calculateLogicalSessionTimeoutMinutes() {
Integer retVal = null;

Expand Down
4 changes: 3 additions & 1 deletion driver-core/src/main/com/mongodb/connection/ClusterId.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.mongodb.connection;

import com.mongodb.internal.VisibleForTesting;
import com.mongodb.lang.Nullable;
import org.bson.types.ObjectId;

import java.util.Objects;
Expand Down Expand Up @@ -47,7 +48,7 @@ public ClusterId() {
*
* @param description the user defined description of the MongoClient
*/
public ClusterId(final String description) {
public ClusterId(@Nullable final String description) {
this.value = new ObjectId().toHexString();
this.description = description;
}
Expand All @@ -72,6 +73,7 @@ public String getValue() {
*
* @return the user defined description of the MongoClient
*/
@Nullable
public String getDescription() {
return description;
}
Expand Down
12 changes: 8 additions & 4 deletions driver-core/src/main/com/mongodb/connection/ClusterSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public Builder mode(final ClusterConnectionMode mode) {
* @param requiredReplicaSetName the required replica set name.
* @return this
*/
public Builder requiredReplicaSetName(final String requiredReplicaSetName) {
public Builder requiredReplicaSetName(@Nullable final String requiredReplicaSetName) {
this.requiredReplicaSetName = requiredReplicaSetName;
return this;
}
Expand Down Expand Up @@ -328,9 +328,13 @@ public Builder applyConnectionString(final ConnectionString connectionString) {
} else if (connectionString.isSrvProtocol()) {
mode(ClusterConnectionMode.MULTIPLE);
srvHost(connectionString.getHosts().get(0));
srvMaxHosts(connectionString.getSrvMaxHosts());
if (connectionString.getSrvServiceName() != null) {
srvServiceName(connectionString.getSrvServiceName());
Integer srvMaxHosts = connectionString.getSrvMaxHosts();
if (srvMaxHosts != null) {
srvMaxHosts(srvMaxHosts);
}
String srvServiceName = connectionString.getSrvServiceName();
if (srvServiceName != null) {
srvServiceName(srvServiceName);
}
} else if ((directConnection != null && directConnection)
|| (directConnection == null && connectionString.getHosts().size() == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public ConnectionDescription(final ConnectionId connectionId, final int maxWireV
*/
public ConnectionDescription(final ConnectionId connectionId, final int maxWireVersion,
final ServerType serverType, final int maxBatchCount, final int maxDocumentSize,
final int maxMessageSize, final List<String> compressors, final BsonArray saslSupportedMechanisms) {
final int maxMessageSize, final List<String> compressors,
@Nullable final BsonArray saslSupportedMechanisms) {
this(null, connectionId, maxWireVersion, serverType, maxBatchCount, maxDocumentSize, maxMessageSize, compressors,
saslSupportedMechanisms);
}
Expand All @@ -114,7 +115,8 @@ public ConnectionDescription(final ConnectionId connectionId, final int maxWireV
*/
public ConnectionDescription(@Nullable final ObjectId serviceId, final ConnectionId connectionId, final int maxWireVersion,
final ServerType serverType, final int maxBatchCount, final int maxDocumentSize,
final int maxMessageSize, final List<String> compressors, final BsonArray saslSupportedMechanisms) {
final int maxMessageSize, final List<String> compressors,
@Nullable final BsonArray saslSupportedMechanisms) {
this.serviceId = serviceId;
this.connectionId = connectionId;
this.serverType = serverType;
Expand Down Expand Up @@ -241,6 +243,7 @@ public List<String> getCompressors() {
* @return the supported SASL mechanisms.
* @since 4.1
*/
@Nullable
public BsonArray getSaslSupportedMechanisms() {
return saslSupportedMechanisms;
}
Expand Down
24 changes: 14 additions & 10 deletions driver-core/src/main/com/mongodb/connection/ServerDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static Builder builder(final ServerDescription serverDescription) {
*
* @return the host name and port that this replica set member is configured with.
*/
@Nullable
public String getCanonicalAddress() {
return canonicalAddress;
}
Expand Down Expand Up @@ -228,7 +229,7 @@ public Builder address(final ServerAddress address) {
*
* @return this
*/
public Builder canonicalAddress(final String canonicalAddress) {
public Builder canonicalAddress(@Nullable final String canonicalAddress) {
this.canonicalAddress = canonicalAddress;
return this;
}
Expand All @@ -251,7 +252,7 @@ public Builder type(final ServerType type) {
* hidden, passive, nor arbiters.
* @return this
*/
public Builder hosts(final Set<String> hosts) {
public Builder hosts(@Nullable final Set<String> hosts) {
this.hosts = hosts == null ? Collections.emptySet() : Collections.unmodifiableSet(new HashSet<>(hosts));
return this;
}
Expand All @@ -263,7 +264,7 @@ public Builder hosts(final Set<String> hosts) {
* priority of 0.
* @return this
*/
public Builder passives(final Set<String> passives) {
public Builder passives(@Nullable final Set<String> passives) {
this.passives = passives == null ? Collections.emptySet() : Collections.unmodifiableSet(new HashSet<>(passives));
return this;
}
Expand All @@ -275,7 +276,7 @@ public Builder passives(final Set<String> passives) {
* arbiters.
* @return this
*/
public Builder arbiters(final Set<String> arbiters) {
public Builder arbiters(@Nullable final Set<String> arbiters) {
this.arbiters = arbiters == null ? Collections.emptySet() : Collections.unmodifiableSet(new HashSet<>(arbiters));
return this;
}
Expand All @@ -286,7 +287,7 @@ public Builder arbiters(final Set<String> arbiters) {
* @param primary A string in the format of "[hostname]:[port]" listing the current primary member of the replica set.
* @return this
*/
public Builder primary(final String primary) {
public Builder primary(@Nullable final String primary) {
this.primary = primary;
return this;
}
Expand All @@ -308,7 +309,7 @@ public Builder maxDocumentSize(final int maxDocumentSize) {
* @param tagSet a TagSet with all the tags for this server.
* @return this
*/
public Builder tagSet(final TagSet tagSet) {
public Builder tagSet(@Nullable final TagSet tagSet) {
this.tagSet = tagSet == null ? new TagSet() : tagSet;
return this;
}
Expand All @@ -331,7 +332,7 @@ public Builder roundTripTime(final long roundTripTime, final TimeUnit timeUnit)
* @param setName the name of the replica set
* @return this
*/
public Builder setName(final String setName) {
public Builder setName(@Nullable final String setName) {
this.setName = setName;
return this;
}
Expand Down Expand Up @@ -386,7 +387,7 @@ public Builder maxWireVersion(final int maxWireVersion) {
* @param electionId the electionId
* @return this
*/
public Builder electionId(final ObjectId electionId) {
public Builder electionId(@Nullable final ObjectId electionId) {
this.electionId = electionId;
return this;
}
Expand All @@ -397,7 +398,7 @@ public Builder electionId(final ObjectId electionId) {
* @param setVersion the set version
* @return this
*/
public Builder setVersion(final Integer setVersion) {
public Builder setVersion(@Nullable final Integer setVersion) {
this.setVersion = setVersion;
return this;
}
Expand All @@ -410,7 +411,7 @@ public Builder setVersion(final Integer setVersion) {
* @since 4.1
* @mongodb.server.release 4.4
*/
public Builder topologyVersion(final TopologyVersion topologyVersion) {
public Builder topologyVersion(@Nullable final TopologyVersion topologyVersion) {
this.topologyVersion = topologyVersion;
return this;
}
Expand Down Expand Up @@ -660,6 +661,7 @@ public Set<String> getArbiters() {
*
* @return A string in the format of "[hostname]:[port]" listing the current primary member of the replica set.
*/
@Nullable
public String getPrimary() {
return primary;
}
Expand Down Expand Up @@ -727,6 +729,7 @@ public Integer getSetVersion() {
* @since 4.1
* @mongodb.server.release 4.4
*/
@Nullable
public TopologyVersion getTopologyVersion() {
return topologyVersion;
}
Expand Down Expand Up @@ -779,6 +782,7 @@ public boolean hasTags(final TagSet desiredTags) {
*
* @return the name of the replica set
*/
@Nullable
public String getSetName() {
return setName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import com.mongodb.internal.connection.PowerOfTwoBufferPool;
import com.mongodb.internal.connection.SocketStream;
import com.mongodb.internal.connection.UnixSocketChannelStream;
import com.mongodb.lang.Nullable;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;

import static com.mongodb.assertions.Assertions.notNull;
import static java.util.Optional.ofNullable;

/**
* Factory for creating instances of {@code SocketStream}.
Expand Down Expand Up @@ -57,7 +59,7 @@ public SocketStreamFactory(final SocketSettings settings, final SslSettings sslS
* @param sslSettings the SSL for connecting to a MongoDB server
* @param socketFactory a SocketFactory for creating connections to servers.
*/
public SocketStreamFactory(final SocketSettings settings, final SslSettings sslSettings, final SocketFactory socketFactory) {
public SocketStreamFactory(final SocketSettings settings, final SslSettings sslSettings, @Nullable final SocketFactory socketFactory) {
this.settings = notNull("settings", settings);
this.sslSettings = notNull("sslSettings", sslSettings);
this.socketFactory = socketFactory;
Expand Down Expand Up @@ -85,7 +87,7 @@ public Stream create(final ServerAddress serverAddress) {

private SSLContext getSslContext() {
try {
return (sslSettings.getContext() == null) ? SSLContext.getDefault() : sslSettings.getContext();
return ofNullable(sslSettings.getContext()).orElse(SSLContext.getDefault());
} catch (NoSuchAlgorithmException e) {
throw new MongoClientException("Unable to create default SSLContext", e);
}
Expand Down
2 changes: 2 additions & 0 deletions driver-core/src/main/com/mongodb/connection/SslSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.ConnectionString;
import com.mongodb.annotations.Immutable;
import com.mongodb.annotations.NotThreadSafe;
import com.mongodb.lang.Nullable;

import javax.net.ssl.SSLContext;
import java.util.Objects;
Expand Down Expand Up @@ -180,6 +181,7 @@ public boolean isInvalidHostNameAllowed() {
* @since 3.5
* @see SSLContext#getDefault()
*/
@Nullable
public SSLContext getContext() {
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.mongodb.internal.connection.tlschannel.async.AsynchronousTlsChannelGroup;
import com.mongodb.internal.diagnostics.logging.Logger;
import com.mongodb.internal.diagnostics.logging.Loggers;
import com.mongodb.lang.Nullable;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
Expand All @@ -50,6 +51,7 @@
import static com.mongodb.assertions.Assertions.isTrue;
import static com.mongodb.internal.connection.SslHelper.enableHostNameVerification;
import static com.mongodb.internal.connection.SslHelper.enableSni;
import static java.util.Optional.ofNullable;

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

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

@Override
public <A> void read(final ByteBuffer dst, final long timeout, final TimeUnit unit, final A attach,
public <A> void read(final ByteBuffer dst, final long timeout, final TimeUnit unit, @Nullable final A attach,
final CompletionHandler<Integer, ? super A> handler) {
wrapped.read(dst, timeout, unit, attach, handler);
}

@Override
public <A> void read(final ByteBuffer[] dsts, final int offset, final int length, final long timeout, final TimeUnit unit,
final A attach, final CompletionHandler<Long, ? super A> handler) {
@Nullable final A attach, final CompletionHandler<Long, ? super A> handler) {
wrapped.read(dsts, offset, length, timeout, unit, attach, handler);
}

Expand Down
Loading