Skip to content

Commit ffceed8

Browse files
divyajnu08mp911de
authored andcommitted
Add support for $atan, $atan2 and $atanh aggregation operators.
Closes #3709 Original pull request: #3794.
1 parent 34d66a2 commit ffceed8

File tree

5 files changed

+299
-1
lines changed

5 files changed

+299
-1
lines changed

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

+258
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,68 @@ public Cosh cosh(AngularUnit unit) {
790790
public Tan tan() {
791791
return tan(AngularUnit.RADIANS);
792792
}
793+
794+
/**
795+
* Creates new {@link AggregationExpression} that calculates the inverse tangent of a numeric value.
796+
*
797+
* @return new instance of {@link ATan}.
798+
*/
799+
public ATan atan() {
800+
return usesFieldRef() ? ATan.atanOf(fieldReference) : ATan.atanOf(expression);
801+
}
802+
803+
/**
804+
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
805+
* divided by the given numeric value in the argument.
806+
*
807+
* @param the numeric value
808+
* @return new instance of {@link ATan2}.
809+
*/
810+
public ATan2 atan2(Number value) {
811+
812+
Assert.notNull(value, "Value must not be null!");
813+
return createATan2().atan2of(value);
814+
}
815+
816+
/**
817+
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
818+
* divided by the given field reference in the argument.
819+
*
820+
* @param the numeric value
821+
* @return new instance of {@link ATan2}.
822+
*/
823+
public ATan2 atan2(String fieldReference) {
824+
825+
Assert.notNull(fieldReference, "FieldReference must not be null!");
826+
return createATan2().atan2of(fieldReference);
827+
}
828+
829+
/**
830+
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
831+
* divided by the given {@link AggregationExpression} in the argument.
832+
*
833+
* @param the numeric value
834+
* @return new instance of {@link ATan2}.
835+
*/
836+
public ATan2 atan2(AggregationExpression expression) {
837+
838+
Assert.notNull(expression, "Expression must not be null!");
839+
return createATan2().atan2of(expression);
840+
}
841+
842+
private ATan2 createATan2() {
843+
844+
return usesFieldRef() ? ATan2.valueOf(fieldReference) : ATan2.valueOf(expression);
845+
}
846+
847+
/**
848+
* Creates new {@link AggregationExpression} that calculates the inverse hyperbolic tangent of a numeric value.
849+
*
850+
* @return new instance of {@link ATanh}.
851+
*/
852+
public ATanh atanh() {
853+
return usesFieldRef() ? ATanh.atanhOf(fieldReference) : ATanh.atanhOf(expression);
854+
}
793855

794856
/**
795857
* Creates new {@link AggregationExpression} that calculates the tangent of a numeric value in the given
@@ -2579,6 +2641,148 @@ protected String getMongoMethod() {
25792641
return "$tan";
25802642
}
25812643
}
2644+
2645+
2646+
/**
2647+
* An {@link AggregationExpression expression} that calculates the inverse tangent of a value.
2648+
*
2649+
*/
2650+
public static class ATan extends AbstractAggregationExpression {
2651+
2652+
private ATan(Object value) {
2653+
super(value);
2654+
}
2655+
2656+
/**
2657+
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
2658+
*
2659+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2660+
* @return new instance of {@link ATan}.
2661+
*/
2662+
public static ATan atanOf(String fieldReference) {
2663+
2664+
Assert.notNull(fieldReference, "FieldReference must not be null!");
2665+
return new ATan(Fields.field(fieldReference));
2666+
}
2667+
2668+
/**
2669+
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
2670+
* <p />
2671+
*
2672+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2673+
* @return new instance of {@link ATan}.
2674+
*/
2675+
public static ATan atanOf(AggregationExpression expression) {
2676+
return new ATan(expression);
2677+
}
2678+
2679+
/**
2680+
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
2681+
*
2682+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2683+
* numeric value.
2684+
* @return new instance of {@link ATan}.
2685+
*/
2686+
public static ATan atanof(Number value) {
2687+
return new ATan(value);
2688+
}
2689+
2690+
@Override
2691+
protected String getMongoMethod() {
2692+
return "$atan";
2693+
}
2694+
}
2695+
2696+
/**
2697+
* An {@link AggregationExpression expression} that calculates the inverse
2698+
* tangent of y / x, where y and x are the first and second values passed to the
2699+
* expression respectively.
2700+
*
2701+
*/
2702+
public static class ATan2 extends AbstractAggregationExpression {
2703+
2704+
private ATan2(List<?> value) {
2705+
super(value);
2706+
}
2707+
2708+
/**
2709+
* Creates a new {@link AggregationExpression} that calculates the inverse
2710+
* tangent of of y / x, where y and x are the first and second values passed to
2711+
* the expression respectively.
2712+
*
2713+
* @param fieldReference the name of the {@link Field field} that resolves to a
2714+
* numeric value.
2715+
* @return new instance of {@link ATan2}.
2716+
*/
2717+
public static ATan2 valueOf(String fieldReference) {
2718+
2719+
Assert.notNull(fieldReference, "FieldReference must not be null!");
2720+
return new ATan2(asFields(fieldReference));
2721+
}
2722+
2723+
/**
2724+
* Creates a new {@link AggregationExpression} that calculates the inverse
2725+
* tangent of of y / x, where y and x are the first and second values passed to
2726+
* the expression respectively.
2727+
*
2728+
* @param expression the {@link AggregationExpression expression} that resolves
2729+
* to a numeric value.
2730+
* @return new instance of {@link ATan2}.
2731+
*/
2732+
public static ATan2 valueOf(AggregationExpression expression) {
2733+
2734+
Assert.notNull(expression, "Expression must not be null!");
2735+
return new ATan2((Collections.singletonList(expression)));
2736+
}
2737+
2738+
2739+
/**
2740+
* Creates a new {@link AggregationExpression} that calculates the inverse
2741+
* tangent of of y / x, where y and x are the first and second values passed to
2742+
* the expression respectively.
2743+
*
2744+
* @param value anything ({@link Field field}, {@link AggregationExpression
2745+
* expression}, ...) that resolves to a numeric value.
2746+
* @return new instance of {@link ATan2}.
2747+
*/
2748+
public ATan2 atan2of(String fieldReference) {
2749+
2750+
Assert.notNull(fieldReference, "FieldReference must not be null!");
2751+
return new ATan2(append(Fields.field(fieldReference)));
2752+
}
2753+
2754+
/**
2755+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2756+
* {@link AngularUnit#RADIANS}.
2757+
*
2758+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2759+
* numeric value.
2760+
* @return new instance of {@link ATan2}.
2761+
*/
2762+
public ATan2 atan2of(AggregationExpression expression) {
2763+
2764+
Assert.notNull(expression, "Expression must not be null!");
2765+
return new ATan2(append(expression));
2766+
}
2767+
2768+
/**
2769+
* Creates a new {@link AggregationExpression} that calculates the inverse
2770+
* tangent of of y / x, where y and x are the first and second values passed to
2771+
* the expression respectively.
2772+
*
2773+
* @param value of type {@link Number}
2774+
* @return new instance of {@link ATan2}.
2775+
*/
2776+
public ATan2 atan2of(Number value) {
2777+
2778+
return new ATan2(append(value));
2779+
}
2780+
2781+
@Override
2782+
protected String getMongoMethod() {
2783+
return "$atan2";
2784+
}
2785+
}
25822786

25832787
/**
25842788
* An {@link AggregationExpression expression} that calculates the hyperbolic tangent of a value that is measured in
@@ -2684,6 +2888,60 @@ protected String getMongoMethod() {
26842888
return "$tanh";
26852889
}
26862890
}
2891+
2892+
/**
2893+
* An {@link AggregationExpression expression} that calculates the inverse
2894+
* hyperbolic tangent of a value
2895+
*
2896+
*/
2897+
public static class ATanh extends AbstractAggregationExpression {
2898+
2899+
private ATanh(Object value) {
2900+
super(value);
2901+
}
2902+
2903+
/**
2904+
* Creates a new {@link AggregationExpression} that calculates the inverse
2905+
* hyperbolic tangent of a value.
2906+
*
2907+
* @param fieldReference the name of the {@link Field field} that resolves to a
2908+
* numeric value.
2909+
* @return new instance of {@link ATanh}.
2910+
*/
2911+
public static ATanh atanhOf(String fieldReference) {
2912+
return new ATanh(Fields.field(fieldReference));
2913+
}
2914+
2915+
/**
2916+
* Creates a new {@link AggregationExpression} that calculates the inverse
2917+
* hyperbolic tangent of a value.
2918+
* <p />
2919+
*
2920+
* @param expression the {@link AggregationExpression expression} that resolves
2921+
* to a numeric value.
2922+
* @return new instance of {@link ATanh}.
2923+
*/
2924+
public static ATanh atanhOf(AggregationExpression expression) {
2925+
return new ATanh(expression);
2926+
}
2927+
2928+
/**
2929+
* Creates a new {@link AggregationExpression} that calculates the inverse
2930+
* hyperbolic tangent of a value.
2931+
*
2932+
* @param value anything ({@link Field field}, {@link AggregationExpression
2933+
* expression}, ...) that resolves to a numeric value.
2934+
* @return new instance of {@link ATanh}.
2935+
*/
2936+
public static ATanh atanhof(Object value) {
2937+
return new ATanh(value);
2938+
}
2939+
2940+
@Override
2941+
protected String getMongoMethod() {
2942+
return "$atanh";
2943+
}
2944+
}
26872945

26882946
/**
26892947
* {@link Rand} returns a floating value between 0 and 1.

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

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public class MethodReferenceNode extends ExpressionNode {
100100
map.put("tan", singleArgRef().forOperator("$tan"));
101101
map.put("tanh", singleArgRef().forOperator("$tanh"));
102102
map.put("rand", emptyRef().forOperator("$rand"));
103+
map.put("atan", singleArgRef().forOperator("$atan"));
104+
map.put("atan2", arrayArgRef().forOperator("$atan2"));
105+
map.put("atanh", singleArgRef().forOperator("$atanh"));
103106

104107
// STRING OPERATORS
105108
map.put("concat", arrayArgRef().forOperator("$concat"));

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/RegexFlags.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,4 @@ public static int toRegexFlag(char c) {
113113

114114
return flag;
115115
}
116-
}
116+
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,28 @@ void rendersTanhWithValueInDegrees() {
166166
assertThat(valueOf("angle").tanh(AngularUnit.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
167167
.isEqualTo("{ $tanh : { $degreesToRadians : \"$angle\" } }");
168168
}
169+
170+
@Test // DATAMONGO - 3709
171+
void rendersATan() {
172+
173+
assertThat(valueOf("field").atan().toDocument(Aggregation.DEFAULT_CONTEXT))
174+
.isEqualTo("{ $atan : \"$field\" }");
175+
}
176+
177+
@Test // DATAMONGO - 3709
178+
void rendersATan2() {
179+
180+
assertThat(valueOf("field1").atan2("field2").toDocument(Aggregation.DEFAULT_CONTEXT))
181+
.isEqualTo("{ $atan2 : [ \"$field1\" , \"$field2\" ] }");
182+
}
183+
184+
@Test // DATAMONGO - 3709
185+
void rendersATanh() {
186+
187+
assertThat(valueOf("field").atanh().toDocument(Aggregation.DEFAULT_CONTEXT))
188+
.isEqualTo("{ $atanh : \"$field\" }");
189+
}
190+
169191

170192
@Test // GH-3724
171193
void rendersRand() {

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

+15
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,21 @@ void shouldRenderTan() {
10981098
void shouldRenderTanh() {
10991099
assertThat(transform("tanh(angle)")).isEqualTo("{ \"$tanh\" : \"$angle\"}");
11001100
}
1101+
1102+
@Test // DATAMONGO - 3709
1103+
void shouldRenderATan() {
1104+
assertThat(transform("atan(number)")).isEqualTo("{ \"$atan\" : \"$number\"}");
1105+
}
1106+
1107+
@Test // DATAMONGO - 3709
1108+
void shouldRenderATan2() {
1109+
assertThat(transform("atan2(number1,number2)")).isEqualTo("{ \"$atan2\" : [ \"$number1\" , \"$number2\" ] }");
1110+
}
1111+
1112+
@Test // DATAMONGO - 3709
1113+
void shouldRenderATanh() {
1114+
assertThat(transform("atanh(number)")).isEqualTo("{ \"$atanh\" : \"$number\"}");
1115+
}
11011116

11021117
@Test // GH-3713
11031118
void shouldRenderDateAdd() {

0 commit comments

Comments
 (0)