Polishing.

See #4139
Original pull request: #4182.
This commit is contained in:
Christoph Strobl
2022-09-23 14:33:41 +02:00
committed by Mark Paluch
parent cdfdeafdac
commit ff28789507
6 changed files with 134 additions and 41 deletions

View File

@@ -23,9 +23,9 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
@@ -78,7 +78,14 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
}
if (value instanceof Fields fields) {
return fields.asList().stream().map(it -> unpack(it, context)).collect(Collectors.toList());
List<Object> mapped = new ArrayList<>(fields.size());
for (Field field : fields) {
mapped.add(unpack(field, context));
}
return mapped;
}
if (value instanceof Sort sort) {
@@ -98,7 +105,9 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
List<Object> sourceList = (List<Object>) value;
List<Object> mappedList = new ArrayList<>(sourceList.size());
sourceList.stream().map((item) -> unpack(item, context)).forEach(mappedList::add);
for (Object o : sourceList) {
mappedList.add(unpack(o, context));
}
return mappedList;
}
@@ -150,7 +159,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
return append(value, Expand.EXPAND_VALUES);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked" })
protected Map<String, Object> append(String key, Object value) {
Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map");
@@ -165,6 +174,7 @@ abstract class AbstractAggregationExpression implements AggregationExpression {
return clone;
}
@SuppressWarnings("rawtypes")
protected Map<String, Object> appendTo(String key, Object value) {
Assert.isInstanceOf(Map.class, this.value, "Value must be a type of Map");

View File

@@ -826,7 +826,7 @@ public class DateOperators {
*/
public TsIncrement tsIncrement() {
if(timezone != null && !Timezone.none().equals(timezone)) {
if (timezone != null && !Timezone.none().equals(timezone)) {
throw new IllegalArgumentException("$tsIncrement does not support timezones");
}
@@ -841,7 +841,7 @@ public class DateOperators {
*/
public TsSecond tsSecond() {
if(timezone != null && !Timezone.none().equals(timezone)) {
if (timezone != null && !Timezone.none().equals(timezone)) {
throw new IllegalArgumentException("$tsSecond does not support timezones");
}

View File

@@ -190,7 +190,7 @@ public class DensifyOperation implements AggregationOperation {
/**
* Set the {@link DensifyUnit unit} for the step field.
*
*
* @param unit
* @return this.
*/
@@ -354,6 +354,8 @@ public class DensifyOperation implements AggregationOperation {
*/
public DensifyOperationBuilder fullRange(Consumer<DensifyRange> consumer) {
Assert.notNull(consumer, "Consumer must not be null");
DensifyRange range = Range.full();
consumer.accept(range);
@@ -374,7 +376,7 @@ public class DensifyOperation implements AggregationOperation {
return range(range);
}
DensifyOperation build() {
public DensifyOperation build() {
return new DensifyOperation(target.field, target.partitionBy, target.range);
}
}

View File

@@ -18,10 +18,8 @@ package org.springframework.data.mongodb.core.aggregation;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable;
import org.springframework.util.Assert;
/**
@@ -135,7 +133,7 @@ public class ObjectOperators {
* @since 4.0
*/
public GetField getField(String fieldName) {
return GetField.getField(fieldName).from(value);
return GetField.getField(fieldName).of(value);
}
/**
@@ -145,7 +143,7 @@ public class ObjectOperators {
* @since 4.0
*/
public SetField setField(String fieldName) {
return SetField.setField(fieldName).of(value);
return SetField.field(fieldName).input(value);
}
/**
@@ -155,7 +153,7 @@ public class ObjectOperators {
* @since 4.0
*/
public AggregationExpression removeField(String fieldName) {
return SetField.setField(fieldName).of(value).toValue(SystemVariable.REMOVE);
return SetField.field(fieldName).input(value).toValue(SystemVariable.REMOVE);
}
}
@@ -329,23 +327,49 @@ public class ObjectOperators {
super(value);
}
/**
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given {@code fieldName}.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public static GetField getField(String fieldName) {
return new GetField(Collections.singletonMap("field", fieldName));
}
/**
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given {@link Field}.
*
* @param field must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public static GetField getField(Field field) {
return getField(field.getTarget());
}
public GetField from(String fieldRef) {
return from(Fields.field(fieldRef));
/**
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given
* {@code field reference}.
*
* @param fieldRef must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public GetField of(String fieldRef) {
return of(Fields.field(fieldRef));
}
public GetField from(AggregationExpression expression) {
return from((Object) expression);
/**
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given
* {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public GetField of(AggregationExpression expression) {
return of((Object) expression);
}
private GetField from(Object fieldRef) {
private GetField of(Object fieldRef) {
return new GetField(append("input", fieldRef));
}
@@ -367,34 +391,87 @@ public class ObjectOperators {
super(value);
}
public static SetField setField(String fieldName) {
/**
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
* {@code fieldName}.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public static SetField field(String fieldName) {
return new SetField(Collections.singletonMap("field", fieldName));
}
public static SetField setField(Field field) {
return setField(field.getTarget());
/**
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input {@link Field}.
*
* @param field must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public static SetField field(Field field) {
return field(field.getTarget());
}
public SetField of(String fieldRef) {
return of(Fields.field(fieldRef));
/**
* Creates new {@link GetField aggregation expression} that takes the value pointed to by given input
* {@code field reference}.
*
* @param fieldRef must not be {@literal null}.
* @return new instance of {@link GetField}.
*/
public SetField input(String fieldRef) {
return input(Fields.field(fieldRef));
}
public SetField of(AggregationExpression expression) {
return of((Object) expression);
/**
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
* {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public SetField input(AggregationExpression expression) {
return input((Object) expression);
}
private SetField of(Object fieldRef) {
/**
* Creates new {@link SetField aggregation expression} that takes the value pointed to by given input
* {@code field reference}.
*
* @param fieldRef must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
private SetField input(Object fieldRef) {
return new SetField(append("input", fieldRef));
}
/**
* Creates new {@link SetField aggregation expression} providing the {@code value} using {@literal fieldReference}.
*
* @param fieldReference must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public SetField toValueOf(String fieldReference) {
return toValue(Fields.field(fieldReference));
}
/**
* Creates new {@link SetField aggregation expression} providing the {@code value} using
* {@link AggregationExpression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link SetField}.
*/
public SetField toValueOf(AggregationExpression expression) {
return toValue(expression);
}
/**
* Creates new {@link SetField aggregation expression} providing the {@code value}.
*
* @param value
* @return new instance of {@link SetField}.
*/
public SetField toValue(Object value) {
return new SetField(append("value", value));
}

View File

@@ -261,7 +261,7 @@ public class SelectionOperators {
* Limits the number of returned elements to the given value.
*
* @param numberOfResults
* @return new instance of {@link Bottom}.
* @return new instance of {@link First}.
*/
public First limit(int numberOfResults) {
return limit((Object) numberOfResults);
@@ -272,7 +272,7 @@ public class SelectionOperators {
* expression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link First}.
*/
public First limit(AggregationExpression expression) {
return limit((Object) expression);
@@ -286,7 +286,7 @@ public class SelectionOperators {
* Define the field to serve as source.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link First}.
*/
public First of(String fieldName) {
return input(fieldName);
@@ -296,7 +296,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link First}.
*/
public First of(AggregationExpression expression) {
return input(expression);
@@ -306,7 +306,7 @@ public class SelectionOperators {
* Define the field to serve as source.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link First}.
*/
public First input(String fieldName) {
return new First(append("input", Fields.field(fieldName)));
@@ -316,7 +316,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link First}.
*/
public First input(AggregationExpression expression) {
return new First(append("input", expression));
@@ -355,7 +355,7 @@ public class SelectionOperators {
* Limits the number of returned elements to the given value.
*
* @param numberOfResults
* @return new instance of {@link Bottom}.
* @return new instance of {@link Last}.
*/
public Last limit(int numberOfResults) {
return limit((Object) numberOfResults);
@@ -366,7 +366,7 @@ public class SelectionOperators {
* expression}.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link Last}.
*/
public Last limit(AggregationExpression expression) {
return limit((Object) expression);
@@ -380,7 +380,7 @@ public class SelectionOperators {
* Define the field to serve as source.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link Last}.
*/
public Last of(String fieldName) {
return input(fieldName);
@@ -390,7 +390,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link Last}.
*/
public Last of(AggregationExpression expression) {
return input(expression);
@@ -400,7 +400,7 @@ public class SelectionOperators {
* Define the field to serve as source.
*
* @param fieldName must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link Last}.
*/
public Last input(String fieldName) {
return new Last(append("input", Fields.field(fieldName)));
@@ -410,7 +410,7 @@ public class SelectionOperators {
* Define the expression building the value to serve as source.
*
* @param expression must not be {@literal null}.
* @return new instance of {@link Bottom}.
* @return new instance of {@link Last}.
*/
public Last input(AggregationExpression expression) {
return new Last(append("input", expression));

View File

@@ -30,6 +30,8 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.lang.Nullable;
/**
* Unit tests for {@link DensifyOperation}.
*
* @author Christoph Strobl
*/
class DensifyOperationUnitTests {
@@ -38,7 +40,8 @@ class DensifyOperationUnitTests {
void rendersFieldNamesAsIsForUntypedContext() {
DensifyOperation densify = DensifyOperation.builder().densify("ts")
.range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR)).build();
.range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR))
.build();
assertThat(densify.toDocument(contextFor(null))).isEqualTo("""
{
@@ -58,7 +61,8 @@ class DensifyOperationUnitTests {
void rendersFieldNamesCorrectly() {
DensifyOperation densify = DensifyOperation.builder().densify("ts")
.range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR)).build();
.range(Range.bounded("2021-05-18T00:00:00", "2021-05-18T08:00:00").incrementBy(1).unit(DensifyUnits.HOUR))
.build();
assertThat(densify.toDocument(contextFor(Weather.class))).isEqualTo("""
{
@@ -95,7 +99,7 @@ class DensifyOperationUnitTests {
}
@Test // GH-4139
void rendersPartitonRangeCorrectly() {
void rendersPartitionRangeCorrectly() {
DensifyOperation densify = DensifyOperation.builder().densify("alt").partitionBy("var")
.partitionRange(range -> range.incrementBy(200)).build();