Skip to content

Commit 6363cd2

Browse files
committed
Refactor to more functional.
1 parent 4391aad commit 6363cd2

File tree

3 files changed

+85
-73
lines changed

3 files changed

+85
-73
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionPreparer.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import org.bson.Document;
1918
import org.springframework.util.Assert;
2019

2120
import com.mongodb.client.MongoCollection;
@@ -26,14 +25,14 @@
2625
* @author Mark Paluch
2726
* @since 4.1
2827
*/
29-
public interface CollectionPreparer {
28+
public interface CollectionPreparer<T> {
3029

3130
/**
3231
* Returns a preparer that always returns its input collection.
3332
*
3433
* @return a preparer that always returns its input collection.
3534
*/
36-
static CollectionPreparer identity() {
35+
static <T> CollectionPreparer<T> identity() {
3736
return it -> it;
3837
}
3938

@@ -43,7 +42,7 @@ static CollectionPreparer identity() {
4342
* @param collection the collection to prepare.
4443
* @return the prepared collection.
4544
*/
46-
MongoCollection<Document> prepare(MongoCollection<Document> collection);
45+
T prepare(T collection);
4746

4847
/**
4948
* Returns a composed {@code CollectionPreparer} that first applies this preparer to the collection, and then applies
@@ -54,7 +53,7 @@ static CollectionPreparer identity() {
5453
* @return a composed {@code CollectionPreparer} that first applies this preparer and then applies the {@code after}
5554
* preparer.
5655
*/
57-
default CollectionPreparer andThen(CollectionPreparer after) {
56+
default CollectionPreparer<T> andThen(CollectionPreparer<T> after) {
5857
Assert.notNull(after, "After CollectionPreparer must not be null");
5958
return c -> after.prepare(prepare(c));
6059
}

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionPreparerDelegate.java renamed to spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionPreparerSupport.java

+41-24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.springframework.data.mongodb.core;
1717

1818
import java.util.List;
19+
import java.util.function.BiFunction;
20+
import java.util.function.Function;
1921

2022
import org.bson.Document;
2123

@@ -24,43 +26,31 @@
2426
import com.mongodb.client.MongoCollection;
2527

2628
/**
27-
* Delegate to apply {@link ReadConcern} and {@link ReadPreference} settings upon {@link CollectionPreparer preparing a
28-
* collection}.
29+
* Support class for delegate implementations to apply {@link ReadConcern} and {@link ReadPreference} settings upon
30+
* {@link CollectionPreparer preparing a collection}.
2931
*
3032
* @author Mark Paluch
3133
* @since 4.1
3234
*/
33-
class CollectionPreparerDelegate implements ReadConcernAware, ReadPreferenceAware, CollectionPreparer {
35+
class CollectionPreparerSupport implements ReadConcernAware, ReadPreferenceAware {
3436

35-
List<Object> sources;
37+
final List<Object> sources;
3638

37-
private CollectionPreparerDelegate(List<Object> sources) {
39+
private CollectionPreparerSupport(List<Object> sources) {
3840
this.sources = sources;
3941
}
4042

41-
public static CollectionPreparerDelegate of(ReadPreferenceAware... awares) {
42-
return of((Object[]) awares);
43-
}
44-
45-
public static CollectionPreparerDelegate of(Object... mixedAwares) {
46-
47-
if (mixedAwares.length == 1 && mixedAwares[0] instanceof CollectionPreparerDelegate) {
48-
return (CollectionPreparerDelegate) mixedAwares[0];
49-
}
50-
return new CollectionPreparerDelegate(List.of(mixedAwares));
51-
}
43+
<T> T doPrepare(T collection, Function<T, ReadConcern> concernAccessor, BiFunction<T, ReadConcern, T> concernFunction,
44+
Function<T, ReadPreference> preferenceAccessor, BiFunction<T, ReadPreference, T> preferenceFunction) {
5245

53-
@Override
54-
public MongoCollection<Document> prepare(MongoCollection<Document> collection) {
55-
56-
MongoCollection<Document> collectionToUse = collection;
46+
T collectionToUse = collection;
5747

5848
for (Object source : sources) {
5949
if (source instanceof ReadConcernAware rca && rca.hasReadConcern()) {
6050

6151
ReadConcern concern = rca.getReadConcern();
62-
if (collection.getReadConcern() != concern) {
63-
collectionToUse = collectionToUse.withReadConcern(concern);
52+
if (concernAccessor.apply(collectionToUse) != concern) {
53+
collectionToUse = concernFunction.apply(collectionToUse, concern);
6454
}
6555
break;
6656
}
@@ -70,8 +60,8 @@ public MongoCollection<Document> prepare(MongoCollection<Document> collection) {
7060
if (source instanceof ReadPreferenceAware rpa && rpa.hasReadPreference()) {
7161

7262
ReadPreference preference = rpa.getReadPreference();
73-
if (collection.getReadPreference() != preference) {
74-
collectionToUse = collectionToUse.withReadPreference(preference);
63+
if (preferenceAccessor.apply(collectionToUse) != preference) {
64+
collectionToUse = preferenceFunction.apply(collectionToUse, preference);
7565
}
7666
break;
7767
}
@@ -128,4 +118,31 @@ public ReadPreference getReadPreference() {
128118
return null;
129119
}
130120

121+
static class CollectionPreparerDelegate extends CollectionPreparerSupport
122+
implements CollectionPreparer<MongoCollection<Document>> {
123+
124+
private CollectionPreparerDelegate(List<Object> sources) {
125+
super(sources);
126+
}
127+
128+
public static CollectionPreparerDelegate of(ReadPreferenceAware... awares) {
129+
return of((Object[]) awares);
130+
}
131+
132+
public static CollectionPreparerDelegate of(Object... mixedAwares) {
133+
134+
if (mixedAwares.length == 1 && mixedAwares[0] instanceof CollectionPreparerDelegate) {
135+
return (CollectionPreparerDelegate) mixedAwares[0];
136+
}
137+
return new CollectionPreparerDelegate(List.of(mixedAwares));
138+
}
139+
140+
@Override
141+
public MongoCollection<Document> prepare(MongoCollection<Document> collection) {
142+
return doPrepare(collection, MongoCollection::getReadConcern, MongoCollection::withReadConcern,
143+
MongoCollection::getReadPreference, MongoCollection::withReadPreference);
144+
}
145+
146+
}
147+
131148
}

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

+40-44
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.springframework.data.mongodb.MongoDatabaseUtils;
5656
import org.springframework.data.mongodb.SessionSynchronization;
5757
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
58+
import org.springframework.data.mongodb.core.CollectionPreparerSupport.CollectionPreparerDelegate;
5859
import org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext;
5960
import org.springframework.data.mongodb.core.EntityOperations.AdaptibleEntity;
6061
import org.springframework.data.mongodb.core.QueryOperations.AggregationDefinition;
@@ -1120,7 +1121,8 @@ public long estimatedCount(String collectionName) {
11201121
return doEstimatedCount(CollectionPreparerDelegate.of(this), collectionName, new EstimatedDocumentCountOptions());
11211122
}
11221123

1123-
protected long doEstimatedCount(CollectionPreparer collectionPreparer, String collectionName,
1124+
protected long doEstimatedCount(CollectionPreparer<MongoCollection<Document>> collectionPreparer,
1125+
String collectionName,
11241126
EstimatedDocumentCountOptions options) {
11251127
return execute(collectionName,
11261128
collection -> collectionPreparer.prepare(collection).estimatedDocumentCount(options));
@@ -1137,11 +1139,10 @@ public long exactCount(Query query, @Nullable Class<?> entityClass, String colle
11371139
return doExactCount(createDelegate(query), collectionName, mappedQuery, options);
11381140
}
11391141

1140-
protected long doExactCount(CollectionPreparer collectionPreparer, String collectionName, Document filter,
1141-
CountOptions options) {
1142-
return execute(collectionName,
1143-
collection -> collectionPreparer.prepare(collection).countDocuments(CountQuery.of(filter).toQueryDocument(),
1144-
options));
1142+
protected long doExactCount(CollectionPreparer<MongoCollection<Document>> collectionPreparer, String collectionName,
1143+
Document filter, CountOptions options) {
1144+
return execute(collectionName, collection -> collectionPreparer.prepare(collection)
1145+
.countDocuments(CountQuery.of(filter).toQueryDocument(), options));
11451146
}
11461147

11471148
protected boolean countCanBeEstimated(Document filter, CountOptions options) {
@@ -1202,7 +1203,7 @@ protected void ensureNotCollectionLike(@Nullable Object source) {
12021203
protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
12031204

12041205
if (this.readPreference != null && this.readPreference != collection.getReadPreference()) {
1205-
collection = collection.withReadPreference(readPreference);
1206+
return collection.withReadPreference(readPreference);
12061207
}
12071208

12081209
return collection;
@@ -2401,8 +2402,7 @@ protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collecti
24012402
@Nullable
24022403
@SuppressWarnings("ConstantConditions")
24032404
protected <T> T doFindOne(CollectionPreparer collectionPreparer, String collectionName, Document query,
2404-
Document fields, CursorPreparer preparer,
2405-
Class<T> entityClass) {
2405+
Document fields, CursorPreparer preparer, Class<T> entityClass) {
24062406

24072407
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
24082408

@@ -2449,15 +2449,13 @@ protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String colle
24492449
* @return the {@link List} of converted objects.
24502450
*/
24512451
protected <T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query,
2452-
Document fields, Class<T> entityClass,
2453-
CursorPreparer preparer) {
2452+
Document fields, Class<T> entityClass, CursorPreparer preparer) {
24542453
return doFind(collectionPreparer, collectionName, query, fields, entityClass, preparer,
24552454
new ReadDocumentCallback<>(mongoConverter, entityClass, collectionName));
24562455
}
24572456

24582457
protected <S, T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query,
2459-
Document fields, Class<S> entityClass,
2460-
@Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) {
2458+
Document fields, Class<S> entityClass, @Nullable CursorPreparer preparer, DocumentCallback<T> objectCallback) {
24612459

24622460
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
24632461

@@ -2481,8 +2479,7 @@ protected <S, T> List<T> doFind(CollectionPreparer collectionPreparer, String co
24812479
* @since 2.0
24822480
*/
24832481
<S, T> List<T> doFind(CollectionPreparer collectionPreparer, String collectionName, Document query, Document fields,
2484-
Class<S> sourceClass,
2485-
Class<T> targetClass, CursorPreparer preparer) {
2482+
Class<S> sourceClass, Class<T> targetClass, CursorPreparer preparer) {
24862483

24872484
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(sourceClass);
24882485
EntityProjection<T, S> projection = operations.introspectProjection(targetClass, sourceClass);
@@ -2571,8 +2568,7 @@ Document getMappedValidator(Validator validator, Class<?> domainType) {
25712568
*/
25722569
@SuppressWarnings("ConstantConditions")
25732570
protected <T> T doFindAndRemove(CollectionPreparer collectionPreparer, String collectionName, Document query,
2574-
Document fields, Document sort,
2575-
@Nullable Collation collation, Class<T> entityClass) {
2571+
Document fields, Document sort, @Nullable Collation collation, Class<T> entityClass) {
25762572

25772573
EntityReader<? super T, Bson> readerToUse = this.mongoConverter;
25782574

@@ -2583,16 +2579,15 @@ protected <T> T doFindAndRemove(CollectionPreparer collectionPreparer, String co
25832579

25842580
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
25852581

2586-
return executeFindOneInternal(
2587-
new FindAndRemoveCallback(collectionPreparer, queryMapper.getMappedObject(query, entity), fields, sort,
2588-
collation),
2582+
return executeFindOneInternal(new FindAndRemoveCallback(collectionPreparer,
2583+
queryMapper.getMappedObject(query, entity), fields, sort, collation),
25892584
new ReadDocumentCallback<>(readerToUse, entityClass, collectionName), collectionName);
25902585
}
25912586

25922587
@SuppressWarnings("ConstantConditions")
25932588
protected <T> T doFindAndModify(CollectionPreparer collectionPreparer, String collectionName, Document query,
2594-
Document fields, Document sort,
2595-
Class<T> entityClass, UpdateDefinition update, @Nullable FindAndModifyOptions options) {
2589+
Document fields, Document sort, Class<T> entityClass, UpdateDefinition update,
2590+
@Nullable FindAndModifyOptions options) {
25962591

25972592
EntityReader<? super T, Bson> readerToUse = this.mongoConverter;
25982593

@@ -2639,15 +2634,13 @@ protected <T> T doFindAndModify(CollectionPreparer collectionPreparer, String co
26392634
*/
26402635
@Nullable
26412636
protected <T> T doFindAndReplace(CollectionPreparer collectionPreparer, String collectionName, Document mappedQuery,
2642-
Document mappedFields,
2643-
Document mappedSort, @Nullable com.mongodb.client.model.Collation collation, Class<?> entityType,
2644-
Document replacement, FindAndReplaceOptions options, Class<T> resultType) {
2637+
Document mappedFields, Document mappedSort, @Nullable com.mongodb.client.model.Collation collation,
2638+
Class<?> entityType, Document replacement, FindAndReplaceOptions options, Class<T> resultType) {
26452639

26462640
EntityProjection<T, ?> projection = operations.introspectProjection(resultType, entityType);
26472641

26482642
return doFindAndReplace(collectionPreparer, collectionName, mappedQuery, mappedFields, mappedSort, collation,
2649-
entityType, replacement,
2650-
options, projection);
2643+
entityType, replacement, options, projection);
26512644
}
26522645

26532646
CollectionPreparerDelegate createDelegate(Query query) {
@@ -2672,9 +2665,8 @@ CollectionPreparerDelegate createDelegate(Query query) {
26722665
*/
26732666
@Nullable
26742667
private <T> T doFindAndReplace(CollectionPreparer collectionPreparer, String collectionName, Document mappedQuery,
2675-
Document mappedFields,
2676-
Document mappedSort, @Nullable com.mongodb.client.model.Collation collation, Class<?> entityType,
2677-
Document replacement, FindAndReplaceOptions options, EntityProjection<T, ?> projection) {
2668+
Document mappedFields, Document mappedSort, @Nullable com.mongodb.client.model.Collation collation,
2669+
Class<?> entityType, Document replacement, FindAndReplaceOptions options, EntityProjection<T, ?> projection) {
26782670

26792671
if (LOGGER.isDebugEnabled()) {
26802672
LOGGER
@@ -2685,10 +2677,9 @@ private <T> T doFindAndReplace(CollectionPreparer collectionPreparer, String col
26852677
serializeToJsonSafely(mappedSort), entityType, serializeToJsonSafely(replacement), collectionName));
26862678
}
26872679

2688-
return executeFindOneInternal(
2689-
new FindAndReplaceCallback(collectionPreparer, mappedQuery, mappedFields, mappedSort, replacement, collation,
2690-
options),
2691-
new ProjectingReadCallback<>(mongoConverter, projection, collectionName), collectionName);
2680+
return executeFindOneInternal(new FindAndReplaceCallback(collectionPreparer, mappedQuery, mappedFields, mappedSort,
2681+
replacement, collation, options), new ProjectingReadCallback<>(mongoConverter, projection, collectionName),
2682+
collectionName);
26922683
}
26932684

26942685
/**
@@ -2858,12 +2849,13 @@ static RuntimeException potentiallyConvertRuntimeException(RuntimeException ex,
28582849
*/
28592850
private static class FindOneCallback implements CollectionCallback<Document> {
28602851

2861-
private final CollectionPreparer collectionPreparer;
2852+
private final CollectionPreparer<MongoCollection<Document>> collectionPreparer;
28622853
private final Document query;
28632854
private final Optional<Document> fields;
28642855
private final CursorPreparer cursorPreparer;
28652856

2866-
FindOneCallback(CollectionPreparer collectionPreparer, Document query, Document fields, CursorPreparer preparer) {
2857+
FindOneCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query, Document fields,
2858+
CursorPreparer preparer) {
28672859

28682860
this.collectionPreparer = collectionPreparer;
28692861
this.query = query;
@@ -2902,12 +2894,13 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
29022894
*/
29032895
private static class FindCallback implements CollectionCallback<FindIterable<Document>> {
29042896

2905-
private final CollectionPreparer collectionPreparer;
2897+
private final CollectionPreparer<MongoCollection<Document>> collectionPreparer;
29062898
private final Document query;
29072899
private final Document fields;
29082900
private final @Nullable com.mongodb.client.model.Collation collation;
29092901

2910-
public FindCallback(CollectionPreparer collectionPreparer, Document query, Document fields,
2902+
public FindCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
2903+
Document fields,
29112904
@Nullable com.mongodb.client.model.Collation collation) {
29122905

29132906
Assert.notNull(query, "Query must not be null");
@@ -2970,13 +2963,14 @@ public Boolean doInCollection(MongoCollection<Document> collection) throws Mongo
29702963
*/
29712964
private static class FindAndRemoveCallback implements CollectionCallback<Document> {
29722965

2973-
private final CollectionPreparer collectionPreparer;
2966+
private final CollectionPreparer<MongoCollection<Document>> collectionPreparer;
29742967
private final Document query;
29752968
private final Document fields;
29762969
private final Document sort;
29772970
private final Optional<Collation> collation;
29782971

2979-
FindAndRemoveCallback(CollectionPreparer collectionPreparer, Document query, Document fields, Document sort,
2972+
FindAndRemoveCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
2973+
Document fields, Document sort,
29802974
@Nullable Collation collation) {
29812975
this.collectionPreparer = collectionPreparer;
29822976

@@ -2998,15 +2992,16 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
29982992

29992993
private static class FindAndModifyCallback implements CollectionCallback<Document> {
30002994

3001-
private final CollectionPreparer collectionPreparer;
2995+
private final CollectionPreparer<MongoCollection<Document>> collectionPreparer;
30022996
private final Document query;
30032997
private final Document fields;
30042998
private final Document sort;
30052999
private final Object update;
30063000
private final List<Document> arrayFilters;
30073001
private final FindAndModifyOptions options;
30083002

3009-
FindAndModifyCallback(CollectionPreparer collectionPreparer, Document query, Document fields, Document sort,
3003+
FindAndModifyCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
3004+
Document fields, Document sort,
30103005
Object update, List<Document> arrayFilters, FindAndModifyOptions options) {
30113006

30123007
this.collectionPreparer = collectionPreparer;
@@ -3056,15 +3051,16 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
30563051
*/
30573052
private static class FindAndReplaceCallback implements CollectionCallback<Document> {
30583053

3059-
private final CollectionPreparer collectionPreparer;
3054+
private final CollectionPreparer<MongoCollection<Document>> collectionPreparer;
30603055
private final Document query;
30613056
private final Document fields;
30623057
private final Document sort;
30633058
private final Document update;
30643059
private final @Nullable com.mongodb.client.model.Collation collation;
30653060
private final FindAndReplaceOptions options;
30663061

3067-
FindAndReplaceCallback(CollectionPreparer collectionPreparer, Document query, Document fields, Document sort,
3062+
FindAndReplaceCallback(CollectionPreparer<MongoCollection<Document>> collectionPreparer, Document query,
3063+
Document fields, Document sort,
30683064
Document update, @Nullable com.mongodb.client.model.Collation collation, FindAndReplaceOptions options) {
30693065
this.collectionPreparer = collectionPreparer;
30703066
this.query = query;

0 commit comments

Comments
 (0)