Skip to content

Commit 59d0042

Browse files
divyajnu08mp911de
authored andcommitted
Add support for $asin and $asinh aggregation operators.
Closes #3708 Original pull request: #3796.
1 parent 8af904b commit 59d0042

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

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

+116
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,24 @@ public Sinh sinh(AngularUnit unit) {
735735
return usesFieldRef() ? Sinh.sinhOf(fieldReference, unit) : Sinh.sinhOf(expression, unit);
736736
}
737737

738+
/**
739+
* Creates new {@link AggregationExpression} that calculates the inverse sine of a numeric value.
740+
*
741+
* @return new instance of {@link ASin}.
742+
*/
743+
public ASin asin() {
744+
return usesFieldRef() ? ASin.asinOf(fieldReference) : ASin.asinOf(expression);
745+
}
746+
747+
/**
748+
* Creates new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a numeric value.
749+
*
750+
* @return new instance of {@link ASinh}.
751+
*/
752+
public ASinh asinh() {
753+
return usesFieldRef() ? ASinh.asinhOf(fieldReference) : ASinh.asinhOf(expression);
754+
}
755+
738756
/**
739757
* Creates new {@link AggregationExpression} that calculates the cosine of a numeric value given in
740758
* {@link AngularUnit#RADIANS radians}.
@@ -2339,6 +2357,104 @@ protected String getMongoMethod() {
23392357
return "$sinh";
23402358
}
23412359
}
2360+
2361+
/**
2362+
* An {@link AggregationExpression expression} that calculates the inverse sine of a value.
2363+
*
2364+
*/
2365+
public static class ASin extends AbstractAggregationExpression {
2366+
2367+
private ASin(Object value) {
2368+
super(value);
2369+
}
2370+
2371+
/**
2372+
* Creates a new {@link AggregationExpression} that calculates the inverse sine of a value.
2373+
*
2374+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2375+
* @return new instance of {@link ASin}.
2376+
*/
2377+
public static ASin asinOf(String fieldReference) {
2378+
2379+
Assert.notNull(fieldReference, "FieldReference must not be null!");
2380+
return new ASin(Fields.field(fieldReference));
2381+
}
2382+
2383+
/**
2384+
* Creates a new {@link AggregationExpression} that calculates the inverse sine of a value.
2385+
* <p />
2386+
*
2387+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2388+
* @return new instance of {@link ASin}.
2389+
*/
2390+
public static ASin asinOf(AggregationExpression expression) {
2391+
return new ASin(expression);
2392+
}
2393+
2394+
/**
2395+
* Creates a new {@link AggregationExpression} that calculates the inverse sine of a value.
2396+
*
2397+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2398+
* numeric value.
2399+
* @return new instance of {@link ASin}.
2400+
*/
2401+
public static ASin asinOf(Number value) {
2402+
return new ASin(value);
2403+
}
2404+
2405+
@Override
2406+
protected String getMongoMethod() {
2407+
return "$asin";
2408+
}
2409+
}
2410+
2411+
/**
2412+
* An {@link AggregationExpression expression} that calculates the inverse hyperbolic sine of a value
2413+
*/
2414+
public static class ASinh extends AbstractAggregationExpression {
2415+
2416+
private ASinh(Object value) {
2417+
super(value);
2418+
}
2419+
2420+
/**
2421+
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a value.
2422+
*
2423+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2424+
* @return new instance of {@link ASinh}.
2425+
*/
2426+
public static ASinh asinhOf(String fieldReference) {
2427+
return new ASinh(Fields.field(fieldReference));
2428+
}
2429+
2430+
/**
2431+
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a value.
2432+
* <p />
2433+
*
2434+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2435+
* @return new instance of {@link ASinh}.
2436+
*/
2437+
public static ASinh asinhOf(AggregationExpression expression) {
2438+
return new ASinh(expression);
2439+
}
2440+
2441+
/**
2442+
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a value.
2443+
*
2444+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2445+
* numeric value.
2446+
* @return new instance of {@link ASinh}.
2447+
*/
2448+
public static ASinh asinhOf(Object value) {
2449+
return new ASinh(value);
2450+
}
2451+
2452+
@Override
2453+
protected String getMongoMethod() {
2454+
return "$asinh";
2455+
}
2456+
}
2457+
23422458

23432459
/**
23442460
* An {@link AggregationExpression expression} that calculates the cosine of a value that is measured in radians.

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

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public class MethodReferenceNode extends ExpressionNode {
9595
map.put("integral", mapArgRef().forOperator("$integral").mappingParametersTo("input", "unit"));
9696
map.put("sin", singleArgRef().forOperator("$sin"));
9797
map.put("sinh", singleArgRef().forOperator("$sinh"));
98+
map.put("asin", singleArgRef().forOperator("$asin"));
99+
map.put("asinh", singleArgRef().forOperator("$asinh"));
98100
map.put("cos", singleArgRef().forOperator("$cos"));
99101
map.put("cosh", singleArgRef().forOperator("$cosh"));
100102
map.put("tan", singleArgRef().forOperator("$tan"));

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

+14
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ void rendersSinhWithValueInDegrees() {
109109
assertThat(valueOf("angle").sinh(AngularUnit.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
110110
.isEqualTo("{ $sinh : { $degreesToRadians : \"$angle\" } }");
111111
}
112+
113+
@Test // DATAMONGO - 3708
114+
void rendersASin() {
115+
116+
assertThat(valueOf("field").asin().toDocument(Aggregation.DEFAULT_CONTEXT))
117+
.isEqualTo("{ $asin : \"$field\" }");
118+
}
119+
120+
@Test // DATAMONGO - 3708
121+
void rendersASinh() {
122+
123+
assertThat(valueOf("field").asinh().toDocument(Aggregation.DEFAULT_CONTEXT))
124+
.isEqualTo("{ $asinh : \"$field\" }");
125+
}
112126

113127
@Test // GH-3710
114128
void rendersCos() {

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

+10
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,16 @@ void shouldRenderSin() {
10781078
void shouldRenderSinh() {
10791079
assertThat(transform("sinh(angle)")).isEqualTo("{ \"$sinh\" : \"$angle\"}");
10801080
}
1081+
1082+
@Test // DATAMONGO-3708
1083+
void shouldRenderASin() {
1084+
assertThat(transform("asin(number)")).isEqualTo("{ \"$asin\" : \"$number\"}");
1085+
}
1086+
1087+
@Test // DATAMONGO-3708
1088+
void shouldRenderASinh() {
1089+
assertThat(transform("asinh(number)")).isEqualTo("{ \"$asinh\" : \"$number\"}");
1090+
}
10811091

10821092
@Test // GH-3710
10831093
void shouldRenderCos() {

0 commit comments

Comments
 (0)