Polishing.

Accept window units in addition to plain strings. Document operator.

See: #3716
Original pull request: #3742.
This commit is contained in:
Mark Paluch
2021-08-24 14:29:46 +02:00
parent 82b33331fc
commit 10c0203605
3 changed files with 26 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.data.mongodb.core.aggregation;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Avg;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.CovariancePop;
@@ -26,6 +27,8 @@ import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Mi
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.StdDevPop;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.StdDevSamp;
import org.springframework.data.mongodb.core.aggregation.AccumulatorOperators.Sum;
import org.springframework.data.mongodb.core.aggregation.SetWindowFieldsOperation.WindowUnit;
import org.springframework.data.mongodb.core.aggregation.SetWindowFieldsOperation.WindowUnits;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -34,6 +37,7 @@ import org.springframework.util.StringUtils;
* Gateway to {@literal Arithmetic} aggregation operations that perform math operations on numbers.
*
* @author Christoph Strobl
* @author Mark Paluch
* @since 1.10
*/
public class ArithmeticOperators {
@@ -600,7 +604,22 @@ public class ArithmeticOperators {
* @since 3.3
*/
public Derivative derivative() {
return derivative(null);
return derivative((String) null);
}
/**
* Creates new {@link AggregationExpression} that calculates the mathematical derivative value.
*
* @param unit The time unit ({@link WindowUnits#WEEK}, {@link WindowUnits#DAY}, {@link WindowUnits#HOUR},
* {@link WindowUnits#MINUTE}, {@link WindowUnits#SECOND}, {@link WindowUnits#MILLISECOND}) to apply.
* @return new instance of {@link Derivative}.
* @since 3.3
*/
public Derivative derivative(WindowUnit unit) {
Assert.notNull(unit, "Window unit must not be null");
return derivative(unit.name().toLowerCase(Locale.ROOT));
}
/**

View File

@@ -28,8 +28,9 @@ import org.junit.jupiter.api.Test;
* Unit tests for {@link Round}.
*
* @author Christoph Strobl
* @author Mark Paluch
*/
public class ArithmeticOperatorsUnitTests {
class ArithmeticOperatorsUnitTests {
@Test // DATAMONGO-2370
void roundShouldWithoutPlace() {
@@ -63,7 +64,8 @@ public class ArithmeticOperatorsUnitTests {
@Test // GH-3716
void rendersDerivativeCorrectly() {
assertThat(valueOf("miles").derivative("hour").toDocument(Aggregation.DEFAULT_CONTEXT))
assertThat(
valueOf("miles").derivative(SetWindowFieldsOperation.WindowUnits.HOUR).toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo(Document.parse("{ $derivative: { input: \"$miles\", unit: \"hour\" } }"));
}
}

View File

@@ -85,7 +85,7 @@ At the time of this writing, we provide support for the following Aggregation Op
| `addToSet`, `covariancePop`, `covarianceSamp`, `expMovingAvg`, `first`, `last`, `max`, `min`, `avg`, `push`, `sum`, `count` (+++*+++), `stdDevPop`, `stdDevSamp`
| Arithmetic Aggregation Operators
| `abs`, `add` (+++*+++ via `plus`), `ceil`, `divide`, `exp`, `floor`, `ln`, `log`, `log10`, `mod`, `multiply`, `pow`, `round`, `sqrt`, `subtract` (+++*+++ via `minus`), `trunc`
| `abs`, `add` (+++*+++ via `plus`), `ceil`, `derivative`, `divide`, `exp`, `floor`, `ln`, `log`, `log10`, `mod`, `multiply`, `pow`, `round`, `sqrt`, `subtract` (+++*+++ via `minus`), `trunc`
| String Aggregation Operators
| `concat`, `substr`, `toLower`, `toUpper`, `stcasecmp`, `indexOfBytes`, `indexOfCP`, `split`, `strLenBytes`, `strLenCP`, `substrCP`, `trim`, `ltrim`, `rtim`
@@ -119,6 +119,7 @@ At the time of this writing, we provide support for the following Aggregation Op
| Script Aggregation Operators
| `function`, `accumulator`
|===
+++*+++ The operation is mapped or added by Spring Data MongoDB.