Browse Source

Add support for `$atan`, `$atan2` and `$atanh` aggregation operators.

Closes #3709
Original pull request: #3794.
pull/3802/head
divya srivastava 4 years ago committed by Mark Paluch
parent
commit
ffceed8da9
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 258
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ArithmeticOperators.java
  2. 3
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java
  3. 22
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ArithmeticOperatorsUnitTests.java
  4. 15
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java

258
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ArithmeticOperators.java

@ -791,6 +791,68 @@ public class ArithmeticOperators { @@ -791,6 +791,68 @@ public class ArithmeticOperators {
return tan(AngularUnit.RADIANS);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of a numeric value.
*
* @return new instance of {@link ATan}.
*/
public ATan atan() {
return usesFieldRef() ? ATan.atanOf(fieldReference) : ATan.atanOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
* divided by the given numeric value in the argument.
*
* @param the numeric value
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2(Number value) {
Assert.notNull(value, "Value must not be null!");
return createATan2().atan2of(value);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
* divided by the given field reference in the argument.
*
* @param the numeric value
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return createATan2().atan2of(fieldReference);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
* divided by the given {@link AggregationExpression} in the argument.
*
* @param the numeric value
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null!");
return createATan2().atan2of(expression);
}
private ATan2 createATan2() {
return usesFieldRef() ? ATan2.valueOf(fieldReference) : ATan2.valueOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse hyperbolic tangent of a numeric value.
*
* @return new instance of {@link ATanh}.
*/
public ATanh atanh() {
return usesFieldRef() ? ATanh.atanhOf(fieldReference) : ATanh.atanhOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the tangent of a numeric value in the given
* {@link AngularUnit unit}.
@ -2580,6 +2642,148 @@ public class ArithmeticOperators { @@ -2580,6 +2642,148 @@ public class ArithmeticOperators {
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse tangent of a value.
*
*/
public static class ATan extends AbstractAggregationExpression {
private ATan(Object value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
*
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
* @return new instance of {@link ATan}.
*/
public static ATan atanOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new ATan(Fields.field(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
* <p />
*
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
* @return new instance of {@link ATan}.
*/
public static ATan atanOf(AggregationExpression expression) {
return new ATan(expression);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
*
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
* numeric value.
* @return new instance of {@link ATan}.
*/
public static ATan atanof(Number value) {
return new ATan(value);
}
@Override
protected String getMongoMethod() {
return "$atan";
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse
* tangent of y / x, where y and x are the first and second values passed to the
* expression respectively.
*
*/
public static class ATan2 extends AbstractAggregationExpression {
private ATan2(List<?> value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param fieldReference the name of the {@link Field field} that resolves to a
* numeric value.
* @return new instance of {@link ATan2}.
*/
public static ATan2 valueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new ATan2(asFields(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param expression the {@link AggregationExpression expression} that resolves
* to a numeric value.
* @return new instance of {@link ATan2}.
*/
public static ATan2 valueOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null!");
return new ATan2((Collections.singletonList(expression)));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param value anything ({@link Field field}, {@link AggregationExpression
* expression}, ...) that resolves to a numeric value.
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2of(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new ATan2(append(Fields.field(fieldReference)));
}
/**
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
* {@link AngularUnit#RADIANS}.
*
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
* numeric value.
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2of(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null!");
return new ATan2(append(expression));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param value of type {@link Number}
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2of(Number value) {
return new ATan2(append(value));
}
@Override
protected String getMongoMethod() {
return "$atan2";
}
}
/**
* An {@link AggregationExpression expression} that calculates the hyperbolic tangent of a value that is measured in
* {@link AngularUnit#RADIANS}.
@ -2685,6 +2889,60 @@ public class ArithmeticOperators { @@ -2685,6 +2889,60 @@ public class ArithmeticOperators {
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse
* hyperbolic tangent of a value
*
*/
public static class ATanh extends AbstractAggregationExpression {
private ATanh(Object value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* hyperbolic tangent of a value.
*
* @param fieldReference the name of the {@link Field field} that resolves to a
* numeric value.
* @return new instance of {@link ATanh}.
*/
public static ATanh atanhOf(String fieldReference) {
return new ATanh(Fields.field(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* hyperbolic tangent of a value.
* <p />
*
* @param expression the {@link AggregationExpression expression} that resolves
* to a numeric value.
* @return new instance of {@link ATanh}.
*/
public static ATanh atanhOf(AggregationExpression expression) {
return new ATanh(expression);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* hyperbolic tangent of a value.
*
* @param value anything ({@link Field field}, {@link AggregationExpression
* expression}, ...) that resolves to a numeric value.
* @return new instance of {@link ATanh}.
*/
public static ATanh atanhof(Object value) {
return new ATanh(value);
}
@Override
protected String getMongoMethod() {
return "$atanh";
}
}
/**
* {@link Rand} returns a floating value between 0 and 1.
*

3
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/spel/MethodReferenceNode.java

@ -100,6 +100,9 @@ public class MethodReferenceNode extends ExpressionNode { @@ -100,6 +100,9 @@ public class MethodReferenceNode extends ExpressionNode {
map.put("tan", singleArgRef().forOperator("$tan"));
map.put("tanh", singleArgRef().forOperator("$tanh"));
map.put("rand", emptyRef().forOperator("$rand"));
map.put("atan", singleArgRef().forOperator("$atan"));
map.put("atan2", arrayArgRef().forOperator("$atan2"));
map.put("atanh", singleArgRef().forOperator("$atanh"));
// STRING OPERATORS
map.put("concat", arrayArgRef().forOperator("$concat"));

22
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ArithmeticOperatorsUnitTests.java

@ -167,6 +167,28 @@ class ArithmeticOperatorsUnitTests { @@ -167,6 +167,28 @@ class ArithmeticOperatorsUnitTests {
.isEqualTo("{ $tanh : { $degreesToRadians : \"$angle\" } }");
}
@Test // DATAMONGO - 3709
void rendersATan() {
assertThat(valueOf("field").atan().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $atan : \"$field\" }");
}
@Test // DATAMONGO - 3709
void rendersATan2() {
assertThat(valueOf("field1").atan2("field2").toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $atan2 : [ \"$field1\" , \"$field2\" ] }");
}
@Test // DATAMONGO - 3709
void rendersATanh() {
assertThat(valueOf("field").atanh().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $atanh : \"$field\" }");
}
@Test // GH-3724
void rendersRand() {
assertThat(rand().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo(new Document("$rand", new Document()));

15
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SpelExpressionTransformerUnitTests.java

@ -1099,6 +1099,21 @@ public class SpelExpressionTransformerUnitTests { @@ -1099,6 +1099,21 @@ public class SpelExpressionTransformerUnitTests {
assertThat(transform("tanh(angle)")).isEqualTo("{ \"$tanh\" : \"$angle\"}");
}
@Test // DATAMONGO - 3709
void shouldRenderATan() {
assertThat(transform("atan(number)")).isEqualTo("{ \"$atan\" : \"$number\"}");
}
@Test // DATAMONGO - 3709
void shouldRenderATan2() {
assertThat(transform("atan2(number1,number2)")).isEqualTo("{ \"$atan2\" : [ \"$number1\" , \"$number2\" ] }");
}
@Test // DATAMONGO - 3709
void shouldRenderATanh() {
assertThat(transform("atanh(number)")).isEqualTo("{ \"$atanh\" : \"$number\"}");
}
@Test // GH-3713
void shouldRenderDateAdd() {
assertThat(transform("dateAdd(purchaseDate, 'day', 3)"))

Loading…
Cancel
Save