20
20
import static org .springframework .data .util .Optionals .*;
21
21
22
22
import java .io .IOException ;
23
- import java .util .ArrayList ;
24
- import java .util .Collection ;
25
- import java .util .Collections ;
26
- import java .util .HashMap ;
27
- import java .util .HashSet ;
28
- import java .util .Iterator ;
29
- import java .util .LinkedHashSet ;
30
- import java .util .List ;
31
- import java .util .Map ;
23
+ import java .util .*;
32
24
import java .util .Map .Entry ;
33
- import java .util .Optional ;
34
- import java .util .Scanner ;
35
- import java .util .Set ;
36
25
import java .util .concurrent .TimeUnit ;
37
26
38
27
import org .bson .Document ;
60
49
import org .springframework .data .geo .GeoResult ;
61
50
import org .springframework .data .geo .GeoResults ;
62
51
import org .springframework .data .geo .Metric ;
52
+ import org .springframework .data .mapping .PersistentEntity ;
63
53
import org .springframework .data .mapping .PersistentPropertyAccessor ;
64
54
import org .springframework .data .mapping .context .MappingContext ;
65
55
import org .springframework .data .mapping .model .ConvertingPropertyAccessor ;
@@ -722,7 +712,7 @@ public <T> T findAndModify(Query query, Update update, FindAndModifyOptions opti
722
712
723
713
Optionals .ifAllPresent (query .getCollation (), optionsToUse .getCollation (), (l , r ) -> {
724
714
throw new IllegalArgumentException (
725
- "Both Query and FindAndModifyOptions define the collation. Please provide the collation only via one of the two." );
715
+ "Both Query and FindAndModifyOptions define a collation. Please provide the collation only via one of the two." );
726
716
});
727
717
728
718
query .getCollation ().ifPresent (optionsToUse ::collation );
@@ -885,7 +875,7 @@ private void initializeVersionProperty(Object entity) {
885
875
886
876
Optional <? extends MongoPersistentEntity <?>> persistentEntity = getPersistentEntity (entity .getClass ());
887
877
888
- ifAllPresent (persistentEntity , persistentEntity .flatMap (it -> it . getVersionProperty () ), (l , r ) -> {
878
+ ifAllPresent (persistentEntity , persistentEntity .flatMap (PersistentEntity :: getVersionProperty ), (l , r ) -> {
889
879
ConvertingPropertyAccessor accessor = new ConvertingPropertyAccessor (l .getPropertyAccessor (entity ),
890
880
mongoConverter .getConversionService ());
891
881
accessor .setProperty (r , Optional .of (0 ));
@@ -972,7 +962,7 @@ public void save(Object objectToSave, String collectionName) {
972
962
Assert .hasText (collectionName , "Collection name must not be null or empty!" );
973
963
974
964
Optional <? extends MongoPersistentEntity <?>> entity = getPersistentEntity (objectToSave .getClass ());
975
- Optional <MongoPersistentProperty > versionProperty = entity .flatMap (it -> it . getVersionProperty () );
965
+ Optional <MongoPersistentProperty > versionProperty = entity .flatMap (PersistentEntity :: getVersionProperty );
976
966
977
967
mapIfAllPresent (entity , versionProperty , //
978
968
(l , r ) -> doSaveVersioned (objectToSave , l , collectionName ))//
@@ -1225,18 +1215,19 @@ public UpdateResult doInCollection(MongoCollection<Document> collection)
1225
1215
private void increaseVersionForUpdateIfNecessary (Optional <? extends MongoPersistentEntity <?>> persistentEntity ,
1226
1216
Update update ) {
1227
1217
1228
- ifAllPresent (persistentEntity , persistentEntity .flatMap (it -> it .getVersionProperty ()), (entity , property ) -> {
1229
- String versionFieldName = property .getFieldName ();
1230
- if (!update .modifies (versionFieldName )) {
1231
- update .inc (versionFieldName , 1L );
1232
- }
1233
- });
1218
+ ifAllPresent (persistentEntity , persistentEntity .flatMap (PersistentEntity ::getVersionProperty ),
1219
+ (entity , property ) -> {
1220
+ String versionFieldName = property .getFieldName ();
1221
+ if (!update .modifies (versionFieldName )) {
1222
+ update .inc (versionFieldName , 1L );
1223
+ }
1224
+ });
1234
1225
}
1235
1226
1236
1227
private boolean documentContainsVersionProperty (Document document ,
1237
1228
Optional <? extends MongoPersistentEntity <?>> persistentEntity ) {
1238
1229
1239
- return mapIfAllPresent (persistentEntity , persistentEntity .flatMap (it -> it . getVersionProperty () ), //
1230
+ return mapIfAllPresent (persistentEntity , persistentEntity .flatMap (PersistentEntity :: getVersionProperty ), //
1240
1231
(entity , property ) -> document .containsKey (property .getFieldName ()))//
1241
1232
.orElse (false );
1242
1233
}
@@ -1458,7 +1449,7 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
1458
1449
1459
1450
Optionals .ifAllPresent (collation , mapReduceOptions .getCollation (), (l , r ) -> {
1460
1451
throw new IllegalArgumentException (
1461
- "Both Query and MapReduceOptions define the collation. Please provide the collation only via one of the two." );
1452
+ "Both Query and MapReduceOptions define a collation. Please provide the collation only via one of the two." );
1462
1453
});
1463
1454
1464
1455
if (mapReduceOptions .getCollation ().isPresent ()) {
@@ -1482,9 +1473,7 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
1482
1473
}
1483
1474
}
1484
1475
1485
- if (collation .isPresent ()) {
1486
- result = result .collation (collation .map (Collation ::toMongoCollation ).get ());
1487
- }
1476
+ result = collation .map (Collation ::toMongoCollation ).map (result ::collation ).orElse (result );
1488
1477
1489
1478
List <T > mappedResults = new ArrayList <T >();
1490
1479
DocumentCallback <T > callback = new ReadDocumentCallback <T >(mongoConverter , entityClass , inputCollectionName );
@@ -2297,7 +2286,7 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
2297
2286
if (LOGGER .isDebugEnabled ()) {
2298
2287
2299
2288
LOGGER .debug ("findOne using query: {} fields: {} in db.collection: {}" , serializeToJsonSafely (query ),
2300
- serializeToJsonSafely (fields .orElseGet (() -> new Document () )), collection .getNamespace ().getFullName ());
2289
+ serializeToJsonSafely (fields .orElseGet (Document :: new )), collection .getNamespace ().getFullName ());
2301
2290
}
2302
2291
2303
2292
if (fields .isPresent ()) {
@@ -2336,11 +2325,7 @@ public FindIterable<Document> doInCollection(MongoCollection<Document> collectio
2336
2325
2337
2326
FindIterable <Document > iterable = collection .find (query );
2338
2327
2339
- if (fields .filter (val -> !val .isEmpty ()).isPresent ()) {
2340
- iterable = iterable .projection (fields .get ());
2341
- }
2342
-
2343
- return iterable ;
2328
+ return fields .filter (val -> !val .isEmpty ()).map (iterable ::projection ).orElse (iterable );
2344
2329
}
2345
2330
}
2346
2331
@@ -2399,7 +2384,7 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
2399
2384
opts .upsert (true );
2400
2385
}
2401
2386
opts .projection (fields );
2402
- if (options .returnNew ) {
2387
+ if (options .isReturnNew () ) {
2403
2388
opts .returnDocument (ReturnDocument .AFTER );
2404
2389
}
2405
2390
@@ -2506,16 +2491,16 @@ public FindIterable<Document> prepare(FindIterable<Document> cursor) {
2506
2491
return cursor ;
2507
2492
}
2508
2493
2509
- if (query .getSkip () <= 0 && query .getLimit () <= 0 && query .getSortObject () == null
2510
- && !StringUtils .hasText (query .getHint ()) && !query .getMeta ().hasValues ()) {
2494
+ if (query .getSkip () <= 0 && query .getLimit () <= 0
2495
+ && (query .getSortObject () == null || query .getSortObject ().isEmpty ()) && !StringUtils .hasText (query .getHint ())
2496
+ && !query .getMeta ().hasValues () && !query .getCollation ().isPresent ()) {
2511
2497
return cursor ;
2512
2498
}
2513
2499
2514
- FindIterable <Document > cursorToUse = cursor ;
2500
+ FindIterable <Document > cursorToUse ;
2501
+
2502
+ cursorToUse = query .getCollation ().map (Collation ::toMongoCollation ).map (cursor ::collation ).orElse (cursor );
2515
2503
2516
- if (query .getCollation ().isPresent ()) {
2517
- cursorToUse = cursorToUse .collation (query .getCollation ().map (val -> val .toMongoCollation ()).get ());
2518
- }
2519
2504
try {
2520
2505
if (query .getSkip () > 0 ) {
2521
2506
cursorToUse = cursorToUse .skip ((int ) query .getSkip ());
0 commit comments