Skip to content

Commit 54f098a

Browse files
divyajnu08christophstrobl
authored andcommitted
Add support for $acos & $acosh aggregation operators.
Resolves: #3707 Original Pull Request: #3858
1 parent 885d059 commit 54f098a

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

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

+118
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,26 @@ public Cosh cosh() {
799799
public Cosh cosh(AngularUnit unit) {
800800
return usesFieldRef() ? Cosh.coshOf(fieldReference, unit) : Cosh.coshOf(expression, unit);
801801
}
802+
803+
/**
804+
* Creates new {@link AggregationExpression} that calculates the inverse cosine of a numeric value.
805+
*
806+
* @return new instance of {@link ACos}.
807+
* @since 3.3
808+
*/
809+
public ACos acos() {
810+
return usesFieldRef() ? ACos.acosOf(fieldReference) : ACos.acosOf(expression);
811+
}
812+
813+
/**
814+
* Creates new {@link AggregationExpression} that calculates the inverse hyperbolic cosine of a numeric value.
815+
*
816+
* @return new instance of {@link ACosh}.
817+
* @since 3.3
818+
*/
819+
public ACosh acosh() {
820+
return usesFieldRef() ? ACosh.acoshOf(fieldReference) : ACosh.acoshOf(expression);
821+
}
802822

803823
/**
804824
* Creates new {@link AggregationExpression} that calculates the tangent of a numeric value given in
@@ -2664,6 +2684,104 @@ protected String getMongoMethod() {
26642684
return "$cosh";
26652685
}
26662686
}
2687+
2688+
/**
2689+
* An {@link AggregationExpression expression} that calculates the inverse cosine of a value.
2690+
*
2691+
*/
2692+
public static class ACos extends AbstractAggregationExpression {
2693+
2694+
private ACos(Object value) {
2695+
super(value);
2696+
}
2697+
2698+
/**
2699+
* Creates a new {@link AggregationExpression} that calculates the inverse cosine of a value.
2700+
*
2701+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2702+
* @return new instance of {@link ACos}.
2703+
*/
2704+
public static ACos acosOf(String fieldReference) {
2705+
2706+
Assert.notNull(fieldReference, "FieldReference must not be null!");
2707+
return new ACos(Fields.field(fieldReference));
2708+
}
2709+
2710+
/**
2711+
* Creates a new {@link AggregationExpression} that calculates the inverse cosine of a value.
2712+
* <br />
2713+
*
2714+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2715+
* @return new instance of {@link ACos}.
2716+
*/
2717+
public static ACos acosOf(AggregationExpression expression) {
2718+
return new ACos(expression);
2719+
}
2720+
2721+
/**
2722+
* Creates a new {@link AggregationExpression} that calculates the inverse cosine of a value.
2723+
*
2724+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2725+
* numeric value.
2726+
* @return new instance of {@link ACos}.
2727+
*/
2728+
public static ACos acosOf(Number value) {
2729+
return new ACos(value);
2730+
}
2731+
2732+
@Override
2733+
protected String getMongoMethod() {
2734+
return "$acos";
2735+
}
2736+
}
2737+
2738+
/**
2739+
* An {@link AggregationExpression expression} that calculates the inverse hyperbolic cosine of a value
2740+
*
2741+
*/
2742+
public static class ACosh extends AbstractAggregationExpression {
2743+
2744+
private ACosh(Object value) {
2745+
super(value);
2746+
}
2747+
2748+
/**
2749+
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic cosine of a value.
2750+
*
2751+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2752+
* @return new instance of {@link ACosh}.
2753+
*/
2754+
public static ACosh acoshOf(String fieldReference) {
2755+
return new ACosh(Fields.field(fieldReference));
2756+
}
2757+
2758+
/**
2759+
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic cosine of a value.
2760+
* <br />
2761+
*
2762+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2763+
* @return new instance of {@link ACosh}.
2764+
*/
2765+
public static ACosh acoshOf(AggregationExpression expression) {
2766+
return new ACosh(expression);
2767+
}
2768+
2769+
/**
2770+
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic cosine of a value.
2771+
*
2772+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2773+
* numeric value.
2774+
* @return new instance of {@link ACosh}.
2775+
*/
2776+
public static ACosh acoshOf(Object value) {
2777+
return new ACosh(value);
2778+
}
2779+
2780+
@Override
2781+
protected String getMongoMethod() {
2782+
return "$acosh";
2783+
}
2784+
}
26672785

26682786
/**
26692787
* An {@link AggregationExpression expression} that calculates the tangent of a value that is measured in radians.

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

+10
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ void rendersCosh() {
138138

139139
assertThat(valueOf("angle").cosh().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo("{ $cosh : \"$angle\" }");
140140
}
141+
142+
@Test
143+
void rendersACos() {
144+
assertThat(valueOf("field").acos().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo("{ $acos : \"$field\" }");
145+
}
146+
147+
@Test
148+
void rendersACosh() {
149+
assertThat(valueOf("field").acosh().toDocument(Aggregation.DEFAULT_CONTEXT)).isEqualTo("{ $acosh : \"$field\" }");
150+
}
141151

142152
@Test // GH-3710
143153
void rendersCoshWithValueInDegrees() {

0 commit comments

Comments
 (0)