From 8ad4f4b71b1b4fea1fafd1153d153147994416d7 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 3 Sep 2019 11:22:54 +0200 Subject: [PATCH] DATAMONGO-2344 - Polishing. Remove generics from FindPublisherPreparer. Rename ReadPreferenceAware.hasReadPreferences to hasReadPreference. Original pull request: #779. --- .../data/mongodb/core/CursorPreparer.java | 4 ++-- .../mongodb/core/FindPublisherPreparer.java | 12 +++--------- .../data/mongodb/core/MongoTemplate.java | 2 +- .../core/ReactiveFindOperationSupport.java | 17 +++-------------- .../mongodb/core/ReactiveMongoTemplate.java | 16 ++++++---------- .../data/mongodb/core/ReadPreferenceAware.java | 7 ++++++- 6 files changed, 21 insertions(+), 37 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CursorPreparer.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CursorPreparer.java index 9f4fa09d6..9942104be 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CursorPreparer.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CursorPreparer.java @@ -50,7 +50,7 @@ interface CursorPreparer extends ReadPreferenceAware { /** * Apply query specific settings to {@link MongoCollection} and initate a find operation returning a * {@link FindIterable} via the given {@link Function find} function. - * + * * @param collection must not be {@literal null}. * @param find must not be {@literal null}. * @return @@ -63,7 +63,7 @@ interface CursorPreparer extends ReadPreferenceAware { Assert.notNull(collection, "Collection must not be null!"); Assert.notNull(find, "Find function must not be null!"); - if (hasReadPreferences()) { + if (hasReadPreference()) { collection = collection.withReadPreference(getReadPreference()); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindPublisherPreparer.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindPublisherPreparer.java index 1252e7286..ef5a497fc 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindPublisherPreparer.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/FindPublisherPreparer.java @@ -38,20 +38,14 @@ interface FindPublisherPreparer extends ReadPreferenceAware { * * @since 2.2 */ - FindPublisherPreparer NO_OP_PREPARER = new FindPublisherPreparer() { - - @Override - public FindPublisher prepare(FindPublisher findPublisher) { - return findPublisher; - } - }; + FindPublisherPreparer NO_OP_PREPARER = (findPublisher -> findPublisher); /** * Prepare the given cursor (apply limits, skips and so on). Returns the prepared cursor. * * @param findPublisher must not be {@literal null}. */ - FindPublisher prepare(FindPublisher findPublisher); + FindPublisher prepare(FindPublisher findPublisher); /** * Apply query specific settings to {@link MongoCollection} and initate a find operation returning a @@ -69,7 +63,7 @@ interface FindPublisherPreparer extends ReadPreferenceAware { Assert.notNull(collection, "Collection must not be null!"); Assert.notNull(find, "Find function must not be null!"); - if (hasReadPreferences()) { + if (hasReadPreference()) { collection = collection.withReadPreference(getReadPreference()); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java index 587284e7b..8aa9c46fd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java @@ -935,7 +935,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware, } QueryCursorPreparer preparer = new QueryCursorPreparer(query, entityClass); - if (preparer.hasReadPreferences()) { + if (preparer.hasReadPreference()) { collection = collection.withReadPreference(preparer.getReadPreference()); } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java index ecc59bc9f..8957d9347 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupport.java @@ -23,6 +23,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.bson.Document; + import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.data.mongodb.core.query.NearQuery; import org.springframework.data.mongodb.core.query.Query; @@ -31,8 +32,6 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import com.mongodb.reactivestreams.client.FindPublisher; - /** * Implementation of {@link ReactiveFindOperation}. * @@ -120,12 +119,7 @@ class ReactiveFindOperationSupport implements ReactiveFindOperation { public Mono first() { FindPublisherPreparer preparer = getCursorPreparer(query); - Flux result = doFind(new FindPublisherPreparer() { - @Override - public FindPublisher prepare(FindPublisher publisher) { - return preparer.prepare(publisher).limit(1); - } - }); + Flux result = doFind(publisher -> preparer.prepare(publisher).limit(1)); return result.next(); } @@ -138,12 +132,7 @@ class ReactiveFindOperationSupport implements ReactiveFindOperation { public Mono one() { FindPublisherPreparer preparer = getCursorPreparer(query); - Flux result = doFind(new FindPublisherPreparer() { - @Override - public FindPublisher prepare(FindPublisher publisher) { - return preparer.prepare(publisher).limit(2); - } - }); + Flux result = doFind(publisher -> preparer.prepare(publisher).limit(2)); return result.collectList().flatMap(it -> { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java index 458d8ee4b..a2763d032 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java @@ -934,7 +934,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati } FindPublisherPreparer preparer = new QueryFindPublisherPreparer(query, entityClass); - if (preparer.hasReadPreferences()) { + if (preparer.hasReadPreference()) { collection = collection.withReadPreference(preparer.getReadPreference()); } @@ -2334,12 +2334,8 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati protected Mono doFindOne(String collectionName, Document query, @Nullable Document fields, Class entityClass, @Nullable Collation collation) { - return doFindOne(collectionName, query, fields, entityClass, new FindPublisherPreparer() { - @Override - public FindPublisher prepare(FindPublisher findPublisher) { - return collation != null ? findPublisher.collation(collation.toMongoCollation()) : findPublisher; - } - }); + return doFindOne(collectionName, query, fields, entityClass, + findPublisher -> collation != null ? findPublisher.collation(collation.toMongoCollation()) : findPublisher); } /** @@ -3183,9 +3179,9 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati } @SuppressWarnings("deprecation") - public FindPublisher prepare(FindPublisher findPublisher) { + public FindPublisher prepare(FindPublisher findPublisher) { - FindPublisher findPublisherToUse = operations.forType(type) // + FindPublisher findPublisherToUse = operations.forType(type) // .getCollation(query) // .map(Collation::toMongoCollation) // .map(findPublisher::collation) // @@ -3259,7 +3255,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati } @Override - public FindPublisher prepare(FindPublisher findPublisher) { + public FindPublisher prepare(FindPublisher findPublisher) { return super.prepare(findPublisher.cursorType(CursorType.TailableAwait)); } } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java index 8811d55a5..0eb079392 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReadPreferenceAware.java @@ -20,7 +20,12 @@ import org.springframework.lang.Nullable; import com.mongodb.ReadPreference; /** + * Interface to be implemented by any object that wishes to expose the {@link ReadPreference}. + *

+ * Typically implemented by cursor or query preparer objects. + * * @author Christoph Strobl + * @author Mark Paluch * @since 2.2 */ interface ReadPreferenceAware { @@ -28,7 +33,7 @@ interface ReadPreferenceAware { /** * @return {@literal true} if a {@link ReadPreference} is set. */ - default boolean hasReadPreferences() { + default boolean hasReadPreference() { return getReadPreference() != null; }