Skip to content

Commit 0b73993

Browse files
christophstroblmp911de
authored andcommitted
Add support for $cos and $cosh aggregation operators.
Closes: #3710 Original pull request: #3755.
1 parent 329396d commit 0b73993

File tree

4 files changed

+291
-0
lines changed

4 files changed

+291
-0
lines changed

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

+251
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,51 @@ public Sinh sinh(AngularDimension unit) {
719719
return usesFieldRef() ? Sinh.sinhOf(fieldReference, unit) : Sinh.sinhOf(expression, unit);
720720
}
721721

722+
/**
723+
* Creates new {@link AggregationExpression} that calculates the cosine of a numeric value given in
724+
* {@link AngularDimension#RADIANS radians}.
725+
*
726+
* @return new instance of {@link Sin}.
727+
* @since 3.3
728+
*/
729+
public Cos cos() {
730+
return cos(AngularDimension.RADIANS);
731+
}
732+
733+
/**
734+
* Creates new {@link AggregationExpression} that calculates the cosine of a numeric value in the given
735+
* {@link AngularDimension unit}.
736+
*
737+
* @param unit the unit of measure.
738+
* @return new instance of {@link Sin}.
739+
* @since 3.3
740+
*/
741+
public Cos cos(AngularDimension unit) {
742+
return usesFieldRef() ? Cos.cosOf(fieldReference, unit) : Cos.cosOf(expression, unit);
743+
}
744+
745+
/**
746+
* Creates new {@link AggregationExpression} that calculates the hyperbolic cosine of a numeric value given in
747+
* {@link AngularDimension#RADIANS radians}.
748+
*
749+
* @return new instance of {@link Sin}.
750+
* @since 3.3
751+
*/
752+
public Cosh cosh() {
753+
return cosh(AngularDimension.RADIANS);
754+
}
755+
756+
/**
757+
* Creates new {@link AggregationExpression} that calculates the hyperbolic cosine of a numeric value.
758+
*
759+
* @param unit the unit of measure.
760+
* @return new instance of {@link Sin}.
761+
* @since 3.3
762+
*/
763+
public Cosh cosh(AngularDimension unit) {
764+
return usesFieldRef() ? Cosh.coshOf(fieldReference, unit) : Cosh.coshOf(expression, unit);
765+
}
766+
722767
/**
723768
* Creates new {@link AggregationExpression} that calculates the tangent of a numeric value given in
724769
* {@link AngularDimension#RADIANS radians}.
@@ -2199,6 +2244,212 @@ protected String getMongoMethod() {
21992244
}
22002245
}
22012246

2247+
/**
2248+
* An {@link AggregationExpression expression} that calculates the cosine of a value that is measured in radians.
2249+
*
2250+
* @author Christoph Strobl
2251+
* @since 3.3
2252+
*/
2253+
public static class Cos extends AbstractAggregationExpression {
2254+
2255+
private Cos(Object value) {
2256+
super(value);
2257+
}
2258+
2259+
/**
2260+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in
2261+
* {@link AngularDimension#RADIANS radians}.
2262+
* <p />
2263+
* Use {@code cosOf("angle", DEGREES)} as shortcut for
2264+
*
2265+
* <pre>
2266+
* { $cos : { $degreesToRadians : "$angle" } }
2267+
* </pre>
2268+
*
2269+
* .
2270+
*
2271+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2272+
* @return new instance of {@link Cos}.
2273+
*/
2274+
public static Cos cosOf(String fieldReference) {
2275+
return cosOf(fieldReference, AngularDimension.RADIANS);
2276+
}
2277+
2278+
/**
2279+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in the given
2280+
* {@link AngularDimension unit}.
2281+
*
2282+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2283+
* @param unit the unit of measure used by the value of the given field.
2284+
* @return new instance of {@link Cos}.
2285+
*/
2286+
public static Cos cosOf(String fieldReference, AngularDimension unit) {
2287+
return cos(Fields.field(fieldReference), unit);
2288+
}
2289+
2290+
/**
2291+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in
2292+
* {@link AngularDimension#RADIANS}.
2293+
*
2294+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2295+
* @return new instance of {@link Cos}.
2296+
*/
2297+
public static Cos cosOf(AggregationExpression expression) {
2298+
return cosOf(expression, AngularDimension.RADIANS);
2299+
}
2300+
2301+
/**
2302+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in the given
2303+
* {@link AngularDimension unit}.
2304+
*
2305+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2306+
* @param unit the unit of measure used by the value of the given field.
2307+
* @return new instance of {@link Cos}.
2308+
*/
2309+
public static Cos cosOf(AggregationExpression expression, AngularDimension unit) {
2310+
return cos(expression, unit);
2311+
}
2312+
2313+
/**
2314+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in
2315+
* {@link AngularDimension#RADIANS}.
2316+
*
2317+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2318+
* numeric value
2319+
* @return new instance of {@link Cos}.
2320+
*/
2321+
public static Cos cos(Object value) {
2322+
return cos(value, AngularDimension.RADIANS);
2323+
}
2324+
2325+
/**
2326+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in the given
2327+
* {@link AngularDimension unit}.
2328+
*
2329+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2330+
* numeric value.
2331+
* @param unit the unit of measure used by the value of the given field.
2332+
* @return new instance of {@link Cos}.
2333+
*/
2334+
public static Cos cos(Object value, AngularDimension unit) {
2335+
2336+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2337+
return new Cos(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2338+
}
2339+
return new Cos(value);
2340+
}
2341+
2342+
@Override
2343+
protected String getMongoMethod() {
2344+
return "$cos";
2345+
}
2346+
}
2347+
2348+
/**
2349+
* An {@link AggregationExpression expression} that calculates the hyperbolic cosine of a value that is measured in
2350+
* {@link AngularDimension#RADIANS}.
2351+
*
2352+
* @author Christoph Strobl
2353+
* @since 3.3
2354+
*/
2355+
public static class Cosh extends AbstractAggregationExpression {
2356+
2357+
private Cosh(Object value) {
2358+
super(value);
2359+
}
2360+
2361+
/**
2362+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2363+
* {@link AngularDimension#RADIANS}.
2364+
*
2365+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2366+
* @return new instance of {@link Cosh}.
2367+
*/
2368+
public static Cosh coshOf(String fieldReference) {
2369+
return coshOf(fieldReference, AngularDimension.RADIANS);
2370+
}
2371+
2372+
/**
2373+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2374+
* the given {@link AngularDimension unit}.
2375+
* <p />
2376+
* Use {@code coshOf("angle", DEGREES)} as shortcut for
2377+
*
2378+
* <pre>
2379+
* { $cosh : { $degreesToRadians : "$angle" } }
2380+
* </pre>
2381+
*
2382+
* .
2383+
*
2384+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2385+
* @param unit the unit of measure used by the value of the given field.
2386+
* @return new instance of {@link Cosh}.
2387+
*/
2388+
public static Cosh coshOf(String fieldReference, AngularDimension unit) {
2389+
return cosh(Fields.field(fieldReference), unit);
2390+
}
2391+
2392+
/**
2393+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2394+
* {@link AngularDimension#RADIANS}.
2395+
* <p />
2396+
* Use {@code sinhOf("angle", DEGREES)} as shortcut for eg.
2397+
* {@code sinhOf(ConvertOperators.valueOf("angle").degreesToRadians())}.
2398+
*
2399+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2400+
* @return new instance of {@link Cosh}.
2401+
*/
2402+
public static Cosh coshOf(AggregationExpression expression) {
2403+
return coshOf(expression, AngularDimension.RADIANS);
2404+
}
2405+
2406+
/**
2407+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2408+
* the given {@link AngularDimension unit}.
2409+
*
2410+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2411+
* @param unit the unit of measure used by the value of the given field.
2412+
* @return new instance of {@link Cosh}.
2413+
*/
2414+
public static Cosh coshOf(AggregationExpression expression, AngularDimension unit) {
2415+
return cosh(expression, unit);
2416+
}
2417+
2418+
/**
2419+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2420+
* {@link AngularDimension#RADIANS}.
2421+
*
2422+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2423+
* numeric value.
2424+
* @return new instance of {@link Cosh}.
2425+
*/
2426+
public static Cosh cosh(Object value) {
2427+
return cosh(value, AngularDimension.RADIANS);
2428+
}
2429+
2430+
/**
2431+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2432+
* the given {@link AngularDimension unit}.
2433+
*
2434+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2435+
* numeric value
2436+
* @param unit the unit of measure used by the value of the given field.
2437+
* @return new instance of {@link Cosh}.
2438+
*/
2439+
public static Cosh cosh(Object value, AngularDimension unit) {
2440+
2441+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2442+
return new Cosh(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2443+
}
2444+
return new Cosh(value);
2445+
}
2446+
2447+
@Override
2448+
protected String getMongoMethod() {
2449+
return "$cosh";
2450+
}
2451+
}
2452+
22022453
/**
22032454
* An {@link AggregationExpression expression} that calculates the tangent of a value that is measured in radians.
22042455
*

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("cos", singleArgRef().forOperator("$cos"));
99+
map.put("cosh", singleArgRef().forOperator("$cosh"));
98100
map.put("tan", singleArgRef().forOperator("$tan"));
99101
map.put("tanh", singleArgRef().forOperator("$tanh"));
100102

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

+28
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,34 @@ void rendersSinhWithValueInDegrees() {
110110
.isEqualTo(Document.parse("{ $sinh : { $degreesToRadians : \"$angle\" } }"));
111111
}
112112

113+
@Test // GH-3710
114+
void rendersCos() {
115+
116+
assertThat(valueOf("angle").cos().toDocument(Aggregation.DEFAULT_CONTEXT))
117+
.isEqualTo(Document.parse("{ $cos : \"$angle\" }"));
118+
}
119+
120+
@Test // GH-3710
121+
void rendersCosWithValueInDegrees() {
122+
123+
assertThat(valueOf("angle").cos(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
124+
.isEqualTo(Document.parse("{ $cos : { $degreesToRadians : \"$angle\" } }"));
125+
}
126+
127+
@Test // GH-3710
128+
void rendersCosh() {
129+
130+
assertThat(valueOf("angle").cosh().toDocument(Aggregation.DEFAULT_CONTEXT))
131+
.isEqualTo(Document.parse("{ $cosh : \"$angle\" }"));
132+
}
133+
134+
@Test // GH-3710
135+
void rendersCoshWithValueInDegrees() {
136+
137+
assertThat(valueOf("angle").cosh(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
138+
.isEqualTo(Document.parse("{ $cosh : { $degreesToRadians : \"$angle\" } }"));
139+
}
140+
113141
@Test // GH-3730
114142
void rendersTan() {
115143

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

+10
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,16 @@ void shouldRenderSinh() {
10141014
assertThat(transform("sinh(angle)")).isEqualTo(Document.parse("{ \"$sinh\" : \"$angle\"}"));
10151015
}
10161016

1017+
@Test // GH-3710
1018+
void shouldRenderCos() {
1019+
assertThat(transform("cos(angle)")).isEqualTo(Document.parse("{ \"$cos\" : \"$angle\"}"));
1020+
}
1021+
1022+
@Test // GH-3710
1023+
void shouldRenderCosh() {
1024+
assertThat(transform("cosh(angle)")).isEqualTo(Document.parse("{ \"$cosh\" : \"$angle\"}"));
1025+
}
1026+
10171027
@Test // GH-3730
10181028
void shouldRenderTan() {
10191029
assertThat(transform("tan(angle)")).isEqualTo(Document.parse("{ \"$tan\" : \"$angle\"}"));

0 commit comments

Comments
 (0)