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:
@@ -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,20 +67,23 @@ 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) {
|
||||||
|
|
||||||
if(inners.isEmpty()) {
|
if (inners.isEmpty()) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user