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:
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user