Skip to content

Commit 3b7b1ac

Browse files
committed
Polishing.
Introduce isEmpty method for HintFunction for easier invocation avoiding negations on the call site. See #3218 Original pull request: #4311
1 parent cd63501 commit 3b7b1ac

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/HintFunction.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public boolean isPresent() {
6868
return (hint instanceof String hintString && StringUtils.hasText(hintString)) || hint instanceof Bson;
6969
}
7070

71+
/**
72+
* If a hint is not present, returns {@code true}, otherwise {@code false}.
73+
*
74+
* @return {@code true} if a hint is not present, otherwise {@code false}.
75+
*/
76+
public boolean isEmpty() {
77+
return !isPresent();
78+
}
79+
7180
/**
7281
* Apply the hint to consumers depending on the hint format if {@link #isPresent() present}.
7382
*
@@ -79,7 +88,7 @@ public boolean isPresent() {
7988
public <R> void ifPresent(@Nullable CodecRegistryProvider registryProvider, Function<String, R> stringConsumer,
8089
Function<Bson, R> bsonConsumer) {
8190

82-
if (!isPresent()) {
91+
if (isEmpty()) {
8392
return;
8493
}
8594
apply(registryProvider, stringConsumer, bsonConsumer);
@@ -97,7 +106,7 @@ public <R> void ifPresent(@Nullable CodecRegistryProvider registryProvider, Func
97106
public <R> R apply(@Nullable CodecRegistryProvider registryProvider, Function<String, R> stringConsumer,
98107
Function<Bson, R> bsonConsumer) {
99108

100-
if (!isPresent()) {
109+
if (isEmpty()) {
101110
throw new IllegalStateException("No hint present");
102111
}
103112

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3226,7 +3226,7 @@ public FindIterable<Document> prepare(FindIterable<Document> iterable) {
32263226
Meta meta = query.getMeta();
32273227
HintFunction hintFunction = HintFunction.from(query.getHint());
32283228
if (query.getSkip() <= 0 && query.getLimit() <= 0 && ObjectUtils.isEmpty(query.getSortObject())
3229-
&& !hintFunction.isPresent() && !meta.hasValues() && !query.getCollation().isPresent()) {
3229+
&& hintFunction.isEmpty() && !meta.hasValues() && query.getCollation().isEmpty()) {
32303230
return cursorToUse;
32313231
}
32323232

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ public FindPublisher<Document> prepare(FindPublisher<Document> findPublisher) {
31083108
HintFunction hintFunction = HintFunction.from(query.getHint());
31093109
Meta meta = query.getMeta();
31103110
if (query.getSkip() <= 0 && query.getLimit() <= 0 && ObjectUtils.isEmpty(query.getSortObject())
3111-
&& !hintFunction.isPresent() && !meta.hasValues()) {
3111+
&& hintFunction.isEmpty() && !meta.hasValues()) {
31123112
return findPublisherToUse;
31133113
}
31143114

0 commit comments

Comments
 (0)