Skip to content

Commit 26c66e9

Browse files
committed
JAVA-2886: Add missing Nullable annotations
Added to: * id parameter of Aggregates#group * value parameter of some Filters methods * value parameter for some Updates methods * return value of MongoCollection#findOneAnd* methods
1 parent ce74702 commit 26c66e9

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,27 +350,27 @@ public static <TExpression> Bson graphLookup(final String from, final TExpressio
350350
* Creates a $group pipeline stage for the specified filter
351351
*
352352
* @param <TExpression> the expression type
353-
* @param id the id expression for the group
353+
* @param id the id expression for the group, which may be null
354354
* @param fieldAccumulators zero or more field accumulator pairs
355355
* @return the $group pipeline stage
356356
* @mongodb.driver.manual reference/operator/aggregation/group/ $group
357357
* @mongodb.driver.manual meta/aggregation-quick-reference/#aggregation-expressions Expressions
358358
*/
359-
public static <TExpression> Bson group(final TExpression id, final BsonField... fieldAccumulators) {
359+
public static <TExpression> Bson group(@Nullable final TExpression id, final BsonField... fieldAccumulators) {
360360
return group(id, asList(fieldAccumulators));
361361
}
362362

363363
/**
364364
* Creates a $group pipeline stage for the specified filter
365365
*
366366
* @param <TExpression> the expression type
367-
* @param id the id expression for the group
367+
* @param id the id expression for the group, which may be null
368368
* @param fieldAccumulators zero or more field accumulator pairs
369369
* @return the $group pipeline stage
370370
* @mongodb.driver.manual reference/operator/aggregation/group/ $group
371371
* @mongodb.driver.manual meta/aggregation-quick-reference/#aggregation-expressions Expressions
372372
*/
373-
public static <TExpression> Bson group(final TExpression id, final List<BsonField> fieldAccumulators) {
373+
public static <TExpression> Bson group(@Nullable final TExpression id, final List<BsonField> fieldAccumulators) {
374374
return new GroupStage<TExpression>(id, fieldAccumulators);
375375
}
376376

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ private Filters() {
5858
* Creates a filter that matches all documents where the value of _id field equals the specified value. Note that this doesn't
5959
* actually generate a $eq operator, as the query language doesn't require it.
6060
*
61-
* @param value the value
61+
* @param value the value, which may be null
6262
* @param <TItem> the value type
6363
* @return the filter
6464
* @mongodb.driver.manual reference/operator/query/eq $eq
6565
*
6666
* @since 3.4
6767
*/
68-
public static <TItem> Bson eq(final TItem value) {
68+
public static <TItem> Bson eq(@Nullable final TItem value) {
6969
return eq("_id", value);
7070
}
7171

@@ -74,25 +74,25 @@ public static <TItem> Bson eq(final TItem value) {
7474
* actually generate a $eq operator, as the query language doesn't require it.
7575
*
7676
* @param fieldName the field name
77-
* @param value the value
77+
* @param value the value, which may be null
7878
* @param <TItem> the value type
7979
* @return the filter
8080
* @mongodb.driver.manual reference/operator/query/eq $eq
8181
*/
82-
public static <TItem> Bson eq(final String fieldName, final TItem value) {
82+
public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value) {
8383
return new SimpleEncodingFilter<TItem>(fieldName, value);
8484
}
8585

8686
/**
8787
* Creates a filter that matches all documents where the value of the field name does not equal the specified value.
8888
*
8989
* @param fieldName the field name
90-
* @param value the value
90+
* @param value the value, which may be null
9191
* @param <TItem> the value type
9292
* @return the filter
9393
* @mongodb.driver.manual reference/operator/query/ne $ne
9494
*/
95-
public static <TItem> Bson ne(final String fieldName, final TItem value) {
95+
public static <TItem> Bson ne(final String fieldName, @Nullable final TItem value) {
9696
return new OperatorFilter<TItem>("$ne", fieldName, value);
9797
}
9898

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

Lines changed: 11 additions & 10 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.BsonDocument;
2021
import org.bson.BsonDocumentWriter;
2122
import org.bson.BsonInt32;
@@ -68,12 +69,12 @@ public static Bson combine(final List<? extends Bson> updates) {
6869
* Creates an update that sets the value of the field with the given name to the given value.
6970
*
7071
* @param fieldName the non-null field name
71-
* @param value the value
72+
* @param value the value, which may be null
7273
* @param <TItem> the value type
7374
* @return the update
7475
* @mongodb.driver.manual reference/operator/update/set/ $set
7576
*/
76-
public static <TItem> Bson set(final String fieldName, final TItem value) {
77+
public static <TItem> Bson set(final String fieldName, @Nullable final TItem value) {
7778
return new SimpleUpdate<TItem>(fieldName, value, "$set");
7879
}
7980

@@ -93,13 +94,13 @@ public static Bson unset(final String fieldName) {
9394
* results in an insert of a document.
9495
*
9596
* @param fieldName the non-null field name
96-
* @param value the value
97+
* @param value the value, which may be null
9798
* @param <TItem> the value type
9899
* @return the update
99100
* @mongodb.driver.manual reference/operator/update/setOnInsert/ $setOnInsert
100101
* @see UpdateOptions#upsert(boolean)
101102
*/
102-
public static <TItem> Bson setOnInsert(final String fieldName, final TItem value) {
103+
public static <TItem> Bson setOnInsert(final String fieldName, @Nullable final TItem value) {
103104
return new SimpleUpdate<TItem>(fieldName, value, "$setOnInsert");
104105
}
105106

@@ -200,12 +201,12 @@ public static Bson currentTimestamp(final String fieldName) {
200201
* already present, in which case it does nothing
201202
*
202203
* @param fieldName the non-null field name
203-
* @param value the value
204+
* @param value the value, which may be null
204205
* @param <TItem> the value type
205206
* @return the update
206207
* @mongodb.driver.manual reference/operator/update/addToSet/ $addToSet
207208
*/
208-
public static <TItem> Bson addToSet(final String fieldName, final TItem value) {
209+
public static <TItem> Bson addToSet(final String fieldName, @Nullable final TItem value) {
209210
return new SimpleUpdate<TItem>(fieldName, value, "$addToSet");
210211
}
211212

@@ -227,12 +228,12 @@ public static <TItem> Bson addEachToSet(final String fieldName, final List<TItem
227228
* Creates an update that adds the given value to the array value of the field with the given name.
228229
*
229230
* @param fieldName the non-null field name
230-
* @param value the value
231+
* @param value the value, which may be null
231232
* @param <TItem> the value type
232233
* @return the update
233234
* @mongodb.driver.manual reference/operator/update/push/ $push
234235
*/
235-
public static <TItem> Bson push(final String fieldName, final TItem value) {
236+
public static <TItem> Bson push(final String fieldName, @Nullable final TItem value) {
236237
return new SimpleUpdate<TItem>(fieldName, value, "$push");
237238
}
238239

@@ -268,12 +269,12 @@ public static <TItem> Bson pushEach(final String fieldName, final List<TItem> va
268269
* Creates an update that removes all instances of the given value from the array value of the field with the given name.
269270
*
270271
* @param fieldName the non-null field name
271-
* @param value the value
272+
* @param value the value, which may be null
272273
* @param <TItem> the value type
273274
* @return the update
274275
* @mongodb.driver.manual reference/operator/update/pull/ $pull
275276
*/
276-
public static <TItem> Bson pull(final String fieldName, final TItem value) {
277+
public static <TItem> Bson pull(final String fieldName, @Nullable final TItem value) {
277278
return new SimpleUpdate<TItem>(fieldName, value, "$pull");
278279
}
279280

driver-sync/src/main/com/mongodb/client/MongoCollection.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.mongodb.client.model.WriteModel;
4242
import com.mongodb.client.result.DeleteResult;
4343
import com.mongodb.client.result.UpdateResult;
44+
import com.mongodb.lang.Nullable;
4445
import org.bson.Document;
4546
import org.bson.codecs.configuration.CodecRegistry;
4647
import org.bson.conversions.Bson;
@@ -1151,6 +1152,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
11511152
* @param filter the query filter to find the document with
11521153
* @return the document that was removed. If no documents matched the query filter, then null will be returned
11531154
*/
1155+
@Nullable
11541156
TDocument findOneAndDelete(Bson filter);
11551157

11561158
/**
@@ -1161,6 +1163,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
11611163
* @param options the options to apply to the operation
11621164
* @return the document that was removed. If no documents matched the query filter, then null will be returned
11631165
*/
1166+
@Nullable
11641167
TDocument findOneAndDelete(Bson filter, FindOneAndDeleteOptions options);
11651168

11661169
/**
@@ -1173,6 +1176,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
11731176
* @since 3.6
11741177
* @mongodb.server.release 3.6
11751178
*/
1179+
@Nullable
11761180
TDocument findOneAndDelete(ClientSession clientSession, Bson filter);
11771181

11781182
/**
@@ -1186,6 +1190,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
11861190
* @since 3.6
11871191
* @mongodb.server.release 3.6
11881192
*/
1193+
@Nullable
11891194
TDocument findOneAndDelete(ClientSession clientSession, Bson filter, FindOneAndDeleteOptions options);
11901195

11911196
/**
@@ -1198,6 +1203,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
11981203
* document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be
11991204
* returned
12001205
*/
1206+
@Nullable
12011207
TDocument findOneAndReplace(Bson filter, TDocument replacement);
12021208

12031209
/**
@@ -1211,6 +1217,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
12111217
* document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be
12121218
* returned
12131219
*/
1220+
@Nullable
12141221
TDocument findOneAndReplace(Bson filter, TDocument replacement, FindOneAndReplaceOptions options);
12151222

12161223
/**
@@ -1226,6 +1233,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
12261233
* @since 3.6
12271234
* @mongodb.server.release 3.6
12281235
*/
1236+
@Nullable
12291237
TDocument findOneAndReplace(ClientSession clientSession, Bson filter, TDocument replacement);
12301238

12311239
/**
@@ -1242,6 +1250,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
12421250
* @since 3.6
12431251
* @mongodb.server.release 3.6
12441252
*/
1253+
@Nullable
12451254
TDocument findOneAndReplace(ClientSession clientSession, Bson filter, TDocument replacement, FindOneAndReplaceOptions options);
12461255

12471256
/**
@@ -1253,6 +1262,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
12531262
* @return the document that was updated before the update was applied. If no documents matched the query filter, then null will be
12541263
* returned
12551264
*/
1265+
@Nullable
12561266
TDocument findOneAndUpdate(Bson filter, Bson update);
12571267

12581268
/**
@@ -1266,6 +1276,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
12661276
* document as it was before the update or as it is after the update. If no documents matched the query filter, then null will be
12671277
* returned
12681278
*/
1279+
@Nullable
12691280
TDocument findOneAndUpdate(Bson filter, Bson update, FindOneAndUpdateOptions options);
12701281

12711282
/**
@@ -1280,6 +1291,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
12801291
* @since 3.6
12811292
* @mongodb.server.release 3.6
12821293
*/
1294+
@Nullable
12831295
TDocument findOneAndUpdate(ClientSession clientSession, Bson filter, Bson update);
12841296

12851297
/**
@@ -1296,6 +1308,7 @@ BulkWriteResult bulkWrite(ClientSession clientSession, List<? extends WriteModel
12961308
* @since 3.6
12971309
* @mongodb.server.release 3.6
12981310
*/
1311+
@Nullable
12991312
TDocument findOneAndUpdate(ClientSession clientSession, Bson filter, Bson update, FindOneAndUpdateOptions options);
13001313

13011314
/**

driver-sync/src/main/com/mongodb/client/internal/MongoCollectionImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,80 +649,95 @@ public UpdateResult updateMany(final ClientSession clientSession, final Bson fil
649649
}
650650

651651
@Override
652+
@Nullable
652653
public TDocument findOneAndDelete(final Bson filter) {
653654
return findOneAndDelete(filter, new FindOneAndDeleteOptions());
654655
}
655656

656657
@Override
658+
@Nullable
657659
public TDocument findOneAndDelete(final Bson filter, final FindOneAndDeleteOptions options) {
658660
return executeFindOneAndDelete(null, filter, options);
659661
}
660662

661663
@Override
664+
@Nullable
662665
public TDocument findOneAndDelete(final ClientSession clientSession, final Bson filter) {
663666
return findOneAndDelete(clientSession, filter, new FindOneAndDeleteOptions());
664667
}
665668

666669
@Override
670+
@Nullable
667671
public TDocument findOneAndDelete(final ClientSession clientSession, final Bson filter, final FindOneAndDeleteOptions options) {
668672
notNull("clientSession", clientSession);
669673
return executeFindOneAndDelete(clientSession, filter, options);
670674
}
671675

676+
@Nullable
672677
private TDocument executeFindOneAndDelete(@Nullable final ClientSession clientSession, final Bson filter,
673678
final FindOneAndDeleteOptions options) {
674679
return executor.execute(operations.findOneAndDelete(filter, options), readConcern, clientSession);
675680
}
676681

677682
@Override
683+
@Nullable
678684
public TDocument findOneAndReplace(final Bson filter, final TDocument replacement) {
679685
return findOneAndReplace(filter, replacement, new FindOneAndReplaceOptions());
680686
}
681687

682688
@Override
689+
@Nullable
683690
public TDocument findOneAndReplace(final Bson filter, final TDocument replacement, final FindOneAndReplaceOptions options) {
684691
return executeFindOneAndReplace(null, filter, replacement, options);
685692
}
686693

687694
@Override
695+
@Nullable
688696
public TDocument findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement) {
689697
return findOneAndReplace(clientSession, filter, replacement, new FindOneAndReplaceOptions());
690698
}
691699

692700
@Override
701+
@Nullable
693702
public TDocument findOneAndReplace(final ClientSession clientSession, final Bson filter, final TDocument replacement,
694703
final FindOneAndReplaceOptions options) {
695704
notNull("clientSession", clientSession);
696705
return executeFindOneAndReplace(clientSession, filter, replacement, options);
697706
}
698707

708+
@Nullable
699709
private TDocument executeFindOneAndReplace(@Nullable final ClientSession clientSession, final Bson filter, final TDocument replacement,
700710
final FindOneAndReplaceOptions options) {
701711
return executor.execute(operations.findOneAndReplace(filter, replacement, options), readConcern, clientSession);
702712
}
703713

704714
@Override
715+
@Nullable
705716
public TDocument findOneAndUpdate(final Bson filter, final Bson update) {
706717
return findOneAndUpdate(filter, update, new FindOneAndUpdateOptions());
707718
}
708719

709720
@Override
721+
@Nullable
710722
public TDocument findOneAndUpdate(final Bson filter, final Bson update, final FindOneAndUpdateOptions options) {
711723
return executeFindOneAndUpdate(null, filter, update, options);
712724
}
713725

714726
@Override
727+
@Nullable
715728
public TDocument findOneAndUpdate(final ClientSession clientSession, final Bson filter, final Bson update) {
716729
return findOneAndUpdate(clientSession, filter, update, new FindOneAndUpdateOptions());
717730
}
718731

719732
@Override
733+
@Nullable
720734
public TDocument findOneAndUpdate(final ClientSession clientSession, final Bson filter, final Bson update,
721735
final FindOneAndUpdateOptions options) {
722736
notNull("clientSession", clientSession);
723737
return executeFindOneAndUpdate(clientSession, filter, update, options);
724738
}
725739

740+
@Nullable
726741
private TDocument executeFindOneAndUpdate(@Nullable final ClientSession clientSession, final Bson filter, final Bson update,
727742
final FindOneAndUpdateOptions options) {
728743
return executor.execute(operations.findOneAndUpdate(filter, update, options), readConcern, clientSession);

0 commit comments

Comments
 (0)