Skip to content

Commit 28c1c46

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

File tree

64 files changed

+478
-223
lines changed

Some content is hidden

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

64 files changed

+478
-223
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,14 @@ private void executeCreateCollection(@Nullable final ClientSession clientSession
277277
.collation(options.getCollation());
278278

279279
IndexOptionDefaults indexOptionDefaults = options.getIndexOptionDefaults();
280-
if (indexOptionDefaults.getStorageEngine() != null) {
281-
operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
280+
Bson storageEngine = indexOptionDefaults.getStorageEngine();
281+
if (storageEngine != null) {
282+
operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(storageEngine)));
282283
}
283284
ValidationOptions validationOptions = options.getValidationOptions();
284-
if (validationOptions.getValidator() != null) {
285-
operation.validator(toBsonDocument(validationOptions.getValidator()));
285+
Bson validator = validationOptions.getValidator();
286+
if (validator != null) {
287+
operation.validator(toBsonDocument(validator));
286288
}
287289
if (validationOptions.getValidationLevel() != null) {
288290
operation.validationLevel(validationOptions.getValidationLevel());

driver-async/src/main/com/mongodb/async/client/gridfs/GridFSBucketImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public GridFSUploadStream openUploadStream(final ClientSession clientSession, fi
176176
private GridFSUploadStream createGridFSUploadStream(@Nullable final ClientSession clientSession, final BsonValue id,
177177
final String filename, final GridFSUploadOptions options) {
178178
notNull("options", options);
179-
int chunkSize = options.getChunkSizeBytes() == null ? chunkSizeBytes : options.getChunkSizeBytes();
179+
Integer chunkSizeBytes = options.getChunkSizeBytes();
180+
int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes;
180181
return new GridFSUploadStreamImpl(clientSession, filesCollection, chunksCollection, id, filename, chunkSize, options.getMetadata(),
181182
new GridFSIndexCheckImpl(clientSession, filesCollection, chunksCollection));
182183
}
@@ -257,7 +258,8 @@ private void executeUploadFromStream(@Nullable final ClientSession clientSession
257258
notNull("source", source);
258259
notNull("options", options);
259260
notNull("callback", callback);
260-
int chunkSize = options.getChunkSizeBytes() == null ? chunkSizeBytes : options.getChunkSizeBytes();
261+
Integer chunkSizeBytes = options.getChunkSizeBytes();
262+
int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes;
261263
GridFSUploadStream uploadStream;
262264
if (clientSession != null){
263265
uploadStream = openUploadStream(clientSession, id, filename, options);

driver-async/src/main/com/mongodb/async/client/gridfs/GridFSUploadStreamImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ final class GridFSUploadStreamImpl implements GridFSUploadStream {
6868

6969
GridFSUploadStreamImpl(@Nullable final ClientSession clientSession, final MongoCollection<GridFSFile> filesCollection,
7070
final MongoCollection<Document> chunksCollection, final BsonValue fileId, final String filename,
71-
final int chunkSizeBytes, final Document metadata, final GridFSIndexCheck indexCheck) {
71+
final int chunkSizeBytes, @Nullable final Document metadata, final GridFSIndexCheck indexCheck) {
7272
this.clientSession = clientSession;
7373
this.filesCollection = notNull("files collection", filesCollection);
7474
this.chunksCollection = notNull("chunks collection", chunksCollection);

driver-core/src/main/com/mongodb/client/gridfs/codecs/GridFSFileCodec.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb.client.gridfs.codecs;
1818

1919
import com.mongodb.client.gridfs.model.GridFSFile;
20+
import com.mongodb.lang.Nullable;
2021
import org.bson.BsonDateTime;
2122
import org.bson.BsonDocument;
2223
import org.bson.BsonDocumentReader;
@@ -111,6 +112,7 @@ public Class<GridFSFile> getEncoderClass() {
111112
return GridFSFile.class;
112113
}
113114

115+
@Nullable
114116
private Document asDocumentOrNull(final BsonDocument bsonDocument) {
115117
if (bsonDocument.isEmpty()) {
116118
return null;

driver-core/src/main/com/mongodb/client/gridfs/codecs/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
*
2020
* @since 3.3
2121
*/
22+
@NonNullApi
2223
package com.mongodb.client.gridfs.codecs;
24+
25+
import com.mongodb.lang.NonNullApi;

driver-core/src/main/com/mongodb/client/gridfs/model/GridFSFile.java

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

1919
import com.mongodb.MongoGridFSException;
20+
import com.mongodb.lang.Nullable;
2021
import org.bson.BsonValue;
2122
import org.bson.Document;
2223
import org.bson.types.ObjectId;
@@ -76,7 +77,7 @@ public GridFSFile(final BsonValue id, final String filename, final long length,
7677
* @param extraElements any extra data stored in the document
7778
*/
7879
public GridFSFile(final BsonValue id, final String filename, final long length, final int chunkSize, final Date uploadDate,
79-
final String md5, final Document metadata, final Document extraElements) {
80+
final String md5, @Nullable final Document metadata, @Nullable final Document extraElements) {
8081
this.id = notNull("id", id);
8182
this.filename = notNull("filename", filename);
8283
this.length = notNull("length", length);
@@ -160,6 +161,7 @@ public String getMD5() {
160161
*
161162
* @return the metadata document or null
162163
*/
164+
@Nullable
163165
public Document getMetadata() {
164166
return metadata;
165167
}
@@ -171,6 +173,7 @@ public Document getMetadata() {
171173
* @deprecated any extra information should be stored in the metadata document instead.
172174
*/
173175
@Deprecated
176+
@Nullable
174177
public Document getExtraElements() {
175178
return extraElements;
176179
}

driver-core/src/main/com/mongodb/client/gridfs/model/GridFSUploadOptions.java

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

1717
package com.mongodb.client.gridfs.model;
1818

19+
import com.mongodb.lang.Nullable;
1920
import org.bson.Document;
2021

2122
/**
@@ -42,6 +43,7 @@ public GridFSUploadOptions() {
4243
*
4344
* @return number of bytes per chunk if set or null
4445
*/
46+
@Nullable
4547
public Integer getChunkSizeBytes() {
4648
return chunkSizeBytes;
4749
}
@@ -52,7 +54,7 @@ public Integer getChunkSizeBytes() {
5254
* @param chunkSizeBytes the number of bytes per chunk for the uploaded file
5355
* @return this
5456
*/
55-
public GridFSUploadOptions chunkSizeBytes(final Integer chunkSizeBytes) {
57+
public GridFSUploadOptions chunkSizeBytes(@Nullable final Integer chunkSizeBytes) {
5658
this.chunkSizeBytes = chunkSizeBytes;
5759
return this;
5860
}
@@ -62,6 +64,7 @@ public GridFSUploadOptions chunkSizeBytes(final Integer chunkSizeBytes) {
6264
*
6365
* @return the user provided metadata for the file if set or null
6466
*/
67+
@Nullable
6568
public Document getMetadata() {
6669
return metadata;
6770
}
@@ -72,7 +75,7 @@ public Document getMetadata() {
7275
* @param metadata the metadata to be stored
7376
* @return this
7477
*/
75-
public GridFSUploadOptions metadata(final Document metadata) {
78+
public GridFSUploadOptions metadata(@Nullable final Document metadata) {
7679
this.metadata = metadata;
7780
return this;
7881
}

driver-core/src/main/com/mongodb/client/gridfs/model/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
*
2020
* @since 3.1
2121
*/
22+
@NonNullApi
2223
package com.mongodb.client.gridfs.model;
24+
25+
import com.mongodb.lang.NonNullApi;

driver-core/src/main/com/mongodb/client/model/Aggregates.java

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

1717
package com.mongodb.client.model;
1818

19+
import com.mongodb.lang.Nullable;
1920
import org.bson.BsonBoolean;
2021
import org.bson.BsonDocument;
2122
import org.bson.BsonDocumentWriter;
@@ -274,7 +275,8 @@ public static Bson lookup(final String from, final List<? extends Bson> pipeline
274275
* @mongodb.server.release 3.6
275276
* @since 3.7
276277
*/
277-
public static Bson lookup(final String from, final List<Variable<?>> let, final List<? extends Bson> pipeline, final String as) {
278+
public static Bson lookup(final String from, @Nullable final List<Variable<?>> let, final List<? extends Bson> pipeline,
279+
final String as) {
278280
return new LookupStage(from, let, pipeline, as);
279281
}
280282

@@ -396,11 +398,13 @@ public static Bson unwind(final String fieldName) {
396398
public static Bson unwind(final String fieldName, final UnwindOptions unwindOptions) {
397399
notNull("unwindOptions", unwindOptions);
398400
BsonDocument options = new BsonDocument("path", new BsonString(fieldName));
399-
if (unwindOptions.isPreserveNullAndEmptyArrays() != null) {
400-
options.append("preserveNullAndEmptyArrays", BsonBoolean.valueOf(unwindOptions.isPreserveNullAndEmptyArrays()));
401+
Boolean preserveNullAndEmptyArrays = unwindOptions.isPreserveNullAndEmptyArrays();
402+
if (preserveNullAndEmptyArrays != null) {
403+
options.append("preserveNullAndEmptyArrays", BsonBoolean.valueOf(preserveNullAndEmptyArrays));
401404
}
402-
if (unwindOptions.getIncludeArrayIndex() != null) {
403-
options.append("includeArrayIndex", new BsonString(unwindOptions.getIncludeArrayIndex()));
405+
String includeArrayIndex = unwindOptions.getIncludeArrayIndex();
406+
if (includeArrayIndex != null) {
407+
options.append("includeArrayIndex", new BsonString(includeArrayIndex));
404408
}
405409
return new BsonDocument("$unwind", options);
406410
}
@@ -444,7 +448,8 @@ public static Bson sample(final int size) {
444448
return new BsonDocument("$sample", new BsonDocument("size", new BsonInt32(size)));
445449
}
446450

447-
static void writeBucketOutput(final CodecRegistry codecRegistry, final BsonDocumentWriter writer, final List<BsonField> output) {
451+
static void writeBucketOutput(final CodecRegistry codecRegistry, final BsonDocumentWriter writer,
452+
@Nullable final List<BsonField> output) {
448453
if (output != null) {
449454
writer.writeName("output");
450455
writer.writeStartDocument();
@@ -508,9 +513,10 @@ public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentC
508513
BuildersHelper.encodeValue(writer, boundary, codecRegistry);
509514
}
510515
writer.writeEndArray();
511-
if (options.getDefaultBucket() != null) {
516+
Object defaultBucket = options.getDefaultBucket();
517+
if (defaultBucket != null) {
512518
writer.writeName("default");
513-
BuildersHelper.encodeValue(writer, options.getDefaultBucket(), codecRegistry);
519+
BuildersHelper.encodeValue(writer, defaultBucket, codecRegistry);
514520
}
515521
writeBucketOutput(codecRegistry, writer, options.getOutput());
516522
writer.writeEndDocument();
@@ -557,8 +563,9 @@ public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentC
557563

558564
writeBucketOutput(codecRegistry, writer, options.getOutput());
559565

560-
if (options.getGranularity() != null) {
561-
writer.writeString("granularity", options.getGranularity().getValue());
566+
BucketGranularity granularity = options.getGranularity();
567+
if (granularity != null) {
568+
writer.writeString("granularity", granularity.getValue());
562569
}
563570
writer.writeEndDocument();
564571

@@ -582,7 +589,8 @@ private static final class LookupStage implements Bson {
582589
private final List<? extends Bson> pipeline;
583590
private final String as;
584591

585-
private LookupStage(final String from, final List<Variable<?>> let, final List<? extends Bson> pipeline, final String as) {
592+
private LookupStage(final String from, @Nullable final List<Variable<?>> let, final List<? extends Bson> pipeline,
593+
final String as) {
586594
this.from = from;
587595
this.let = let;
588596
this.pipeline = pipeline;
@@ -672,15 +680,18 @@ public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> tDocumentC
672680
writer.writeString("connectFromField", connectFromField);
673681
writer.writeString("connectToField", connectToField);
674682
writer.writeString("as", as);
675-
if (options.getMaxDepth() != null) {
676-
writer.writeInt32("maxDepth", options.getMaxDepth());
683+
Integer maxDepth = options.getMaxDepth();
684+
if (maxDepth != null) {
685+
writer.writeInt32("maxDepth", maxDepth);
677686
}
678-
if (options.getDepthField() != null) {
679-
writer.writeString("depthField", options.getDepthField());
687+
String depthField = options.getDepthField();
688+
if (depthField != null) {
689+
writer.writeString("depthField", depthField);
680690
}
681-
if (options.getRestrictSearchWithMatch() != null) {
691+
Bson restrictSearchWithMatch = options.getRestrictSearchWithMatch();
692+
if (restrictSearchWithMatch != null) {
682693
writer.writeName("restrictSearchWithMatch");
683-
BuildersHelper.encodeValue(writer, options.getRestrictSearchWithMatch(), codecRegistry);
694+
BuildersHelper.encodeValue(writer, restrictSearchWithMatch, codecRegistry);
684695
}
685696

686697
writer.writeEndDocument();

driver-core/src/main/com/mongodb/client/model/BucketAutoOptions.java

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

1717
package com.mongodb.client.model;
1818

19+
import com.mongodb.lang.Nullable;
20+
1921
import java.util.ArrayList;
2022
import java.util.List;
2123

@@ -35,13 +37,15 @@ public class BucketAutoOptions {
3537
/**
3638
* @return the granularity of the bucket definitions
3739
*/
40+
@Nullable
3841
public BucketGranularity getGranularity() {
3942
return granularity;
4043
}
4144

4245
/**
4346
* @return the output document definition
4447
*/
48+
@Nullable
4549
public List<BsonField> getOutput() {
4650
return output == null ? null : new ArrayList<BsonField>(output);
4751
}
@@ -54,7 +58,7 @@ public List<BsonField> getOutput() {
5458
* @see <a href="https://en.wikipedia.org/wiki/Preferred_number">Preferred numbers</a>
5559
* @see BucketGranularity
5660
*/
57-
public BucketAutoOptions granularity(final BucketGranularity granularity) {
61+
public BucketAutoOptions granularity(@Nullable final BucketGranularity granularity) {
5862
this.granularity = granularity;
5963
return this;
6064
}
@@ -76,7 +80,7 @@ public BucketAutoOptions output(final BsonField... output) {
7680
* @param output the output document definition
7781
* @return this
7882
*/
79-
public BucketAutoOptions output(final List<BsonField> output) {
83+
public BucketAutoOptions output(@Nullable final List<BsonField> output) {
8084
this.output = output;
8185
return this;
8286
}

0 commit comments

Comments
 (0)