DATAMONGO-2320 - Polishing.

Use for-loops instead of Stream API and Collectors. Reformat code. Invert condition for smoother readability.

Original pull request: #776.
This commit is contained in:
Mark Paluch
2019-08-02 10:52:23 +02:00
parent 79f8e06fc1
commit 049159374d
2 changed files with 16 additions and 13 deletions

View File

@@ -16,13 +16,11 @@
package org.springframework.data.mongodb.core.aggregation; package org.springframework.data.mongodb.core.aggregation;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.bson.Document; import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExpressionFieldReference; import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExpressionFieldReference;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@@ -31,23 +29,25 @@ import org.springframework.util.Assert;
* variable. * variable.
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch
* @since 1.10 * @since 1.10
*/ */
class NestedDelegatingExpressionAggregationOperationContext implements AggregationOperationContext { class NestedDelegatingExpressionAggregationOperationContext implements AggregationOperationContext {
private final AggregationOperationContext delegate; private final AggregationOperationContext delegate;
private final Set<String> inners; private final Collection<Field> inners;
/** /**
* Creates new {@link NestedDelegatingExpressionAggregationOperationContext}. * Creates new {@link NestedDelegatingExpressionAggregationOperationContext}.
* *
* @param referenceContext must not be {@literal null}. * @param referenceContext must not be {@literal null}.
*/ */
NestedDelegatingExpressionAggregationOperationContext(AggregationOperationContext referenceContext, Collection<Field> inners) { NestedDelegatingExpressionAggregationOperationContext(AggregationOperationContext referenceContext,
Collection<Field> inners) {
Assert.notNull(referenceContext, "Reference context must not be null!"); Assert.notNull(referenceContext, "Reference context must not be null!");
this.delegate = referenceContext; this.delegate = referenceContext;
this.inners = inners.stream().map(Field::getName).collect(Collectors.toSet()); this.inners = inners;
} }
/* /*
@@ -67,7 +67,7 @@ class NestedDelegatingExpressionAggregationOperationContext implements Aggregati
public FieldReference getReference(Field field) { public FieldReference getReference(Field field) {
FieldReference reference = delegate.getReference(field); FieldReference reference = delegate.getReference(field);
return !isInnerVariableReference(field) ? reference : new ExpressionFieldReference(delegate.getReference(field)) ; return isInnerVariableReference(field) ? new ExpressionFieldReference(delegate.getReference(field)) : reference;
} }
private boolean isInnerVariableReference(Field field) { private boolean isInnerVariableReference(Field field) {
@@ -76,11 +76,14 @@ class NestedDelegatingExpressionAggregationOperationContext implements Aggregati
return false; return false;
} }
if(inners.contains(field.getName())) { for (Field inner : inners) {
if (inner.getName().equals(field.getName())
|| (field.getTarget().contains(".") && field.getTarget().startsWith(inner.getName()))) {
return true; return true;
} }
}
return inners.stream().anyMatch(it -> field.getTarget().contains(".") && field.getTarget().startsWith(it)); return false;
} }
/* /*

View File

@@ -119,7 +119,7 @@ public class FilterExpressionUnitTests {
"cond: { $gt: [ \"$$item.price\", \"$field-1\" ] }" + // "cond: { $gt: [ \"$$item.price\", \"$field-1\" ] }" + //
"}"); "}");
assertThat($filter).isEqualTo(new Document(expected)); assertThat($filter, is(expected));
} }
private Document extractFilterOperatorFromDocument(Document source) { private Document extractFilterOperatorFromDocument(Document source) {