Polishing.

Introduce isEmpty method for HintFunction for easier invocation avoiding negations on the call site.

See #3218
Original pull request: #4311
This commit is contained in:
Mark Paluch
2023-03-06 14:43:07 +01:00
parent cd63501680
commit 3b7b1ace8b
3 changed files with 13 additions and 4 deletions

View File

@@ -68,6 +68,15 @@ class HintFunction {
return (hint instanceof String hintString && StringUtils.hasText(hintString)) || hint instanceof Bson;
}
/**
* If a hint is not present, returns {@code true}, otherwise {@code false}.
*
* @return {@code true} if a hint is not present, otherwise {@code false}.
*/
public boolean isEmpty() {
return !isPresent();
}
/**
* Apply the hint to consumers depending on the hint format if {@link #isPresent() present}.
*
@@ -79,7 +88,7 @@ class HintFunction {
public <R> void ifPresent(@Nullable CodecRegistryProvider registryProvider, Function<String, R> stringConsumer,
Function<Bson, R> bsonConsumer) {
if (!isPresent()) {
if (isEmpty()) {
return;
}
apply(registryProvider, stringConsumer, bsonConsumer);
@@ -97,7 +106,7 @@ class HintFunction {
public <R> R apply(@Nullable CodecRegistryProvider registryProvider, Function<String, R> stringConsumer,
Function<Bson, R> bsonConsumer) {
if (!isPresent()) {
if (isEmpty()) {
throw new IllegalStateException("No hint present");
}

View File

@@ -3226,7 +3226,7 @@ public class MongoTemplate
Meta meta = query.getMeta();
HintFunction hintFunction = HintFunction.from(query.getHint());
if (query.getSkip() <= 0 && query.getLimit() <= 0 && ObjectUtils.isEmpty(query.getSortObject())
&& !hintFunction.isPresent() && !meta.hasValues() && !query.getCollation().isPresent()) {
&& hintFunction.isEmpty() && !meta.hasValues() && query.getCollation().isEmpty()) {
return cursorToUse;
}

View File

@@ -3108,7 +3108,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
HintFunction hintFunction = HintFunction.from(query.getHint());
Meta meta = query.getMeta();
if (query.getSkip() <= 0 && query.getLimit() <= 0 && ObjectUtils.isEmpty(query.getSortObject())
&& !hintFunction.isPresent() && !meta.hasValues()) {
&& hintFunction.isEmpty() && !meta.hasValues()) {
return findPublisherToUse;
}