Skip to content

Commit 7ef231f

Browse files
Add support for $cos and $cosh aggregation operators.
Closes: #3710
1 parent 7c5b04f commit 7ef231f

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
@@ -575,6 +575,51 @@ public Sinh sinh(AngularDimension unit) {
575575
return usesFieldRef() ? Sinh.sinhOf(fieldReference, unit) : Sinh.sinhOf(expression, unit);
576576
}
577577

578+
/**
579+
* Creates new {@link AggregationExpression} that calculates the cosine of a numeric value given in
580+
* {@link AngularDimension#RADIANS radians}.
581+
*
582+
* @return new instance of {@link Sin}.
583+
* @since 3.3
584+
*/
585+
public Cos cos() {
586+
return cos(AngularDimension.RADIANS);
587+
}
588+
589+
/**
590+
* Creates new {@link AggregationExpression} that calculates the cosine of a numeric value in the given
591+
* {@link AngularDimension unit}.
592+
*
593+
* @param unit the unit of measure.
594+
* @return new instance of {@link Sin}.
595+
* @since 3.3
596+
*/
597+
public Cos cos(AngularDimension unit) {
598+
return usesFieldRef() ? Cos.cosOf(fieldReference, unit) : Cos.cosOf(expression, unit);
599+
}
600+
601+
/**
602+
* Creates new {@link AggregationExpression} that calculates the hyperbolic cosine of a numeric value given in
603+
* {@link AngularDimension#RADIANS radians}.
604+
*
605+
* @return new instance of {@link Sin}.
606+
* @since 3.3
607+
*/
608+
public Cosh cosh() {
609+
return cosh(AngularDimension.RADIANS);
610+
}
611+
612+
/**
613+
* Creates new {@link AggregationExpression} that calculates the hyperbolic cosine of a numeric value.
614+
*
615+
* @param unit the unit of measure.
616+
* @return new instance of {@link Sin}.
617+
* @since 3.3
618+
*/
619+
public Cosh cosh(AngularDimension unit) {
620+
return usesFieldRef() ? Cosh.coshOf(fieldReference, unit) : Cosh.coshOf(expression, unit);
621+
}
622+
578623
/**
579624
* Creates new {@link AggregationExpression} that calculates the tangent of a numeric value given in
580625
* {@link AngularDimension#RADIANS radians}.
@@ -1957,6 +2002,212 @@ protected String getMongoMethod() {
19572002
}
19582003
}
19592004

2005+
/**
2006+
* An {@link AggregationExpression expression} that calculates the cosine of a value that is measured in radians.
2007+
*
2008+
* @author Christoph Strobl
2009+
* @since 3.3
2010+
*/
2011+
public static class Cos extends AbstractAggregationExpression {
2012+
2013+
private Cos(Object value) {
2014+
super(value);
2015+
}
2016+
2017+
/**
2018+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in
2019+
* {@link AngularDimension#RADIANS radians}.
2020+
* <p />
2021+
* Use {@code cosOf("angle", DEGREES)} as shortcut for
2022+
*
2023+
* <pre>
2024+
* { $cos : { $degreesToRadians : "$angle" } }
2025+
* </pre>
2026+
*
2027+
* .
2028+
*
2029+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2030+
* @return new instance of {@link Cos}.
2031+
*/
2032+
public static Cos cosOf(String fieldReference) {
2033+
return cosOf(fieldReference, AngularDimension.RADIANS);
2034+
}
2035+
2036+
/**
2037+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in the given
2038+
* {@link AngularDimension unit}.
2039+
*
2040+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2041+
* @param unit the unit of measure used by the value of the given field.
2042+
* @return new instance of {@link Cos}.
2043+
*/
2044+
public static Cos cosOf(String fieldReference, AngularDimension unit) {
2045+
return cos(Fields.field(fieldReference), unit);
2046+
}
2047+
2048+
/**
2049+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in
2050+
* {@link AngularDimension#RADIANS}.
2051+
*
2052+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2053+
* @return new instance of {@link Cos}.
2054+
*/
2055+
public static Cos cosOf(AggregationExpression expression) {
2056+
return cosOf(expression, AngularDimension.RADIANS);
2057+
}
2058+
2059+
/**
2060+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in the given
2061+
* {@link AngularDimension unit}.
2062+
*
2063+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2064+
* @param unit the unit of measure used by the value of the given field.
2065+
* @return new instance of {@link Cos}.
2066+
*/
2067+
public static Cos cosOf(AggregationExpression expression, AngularDimension unit) {
2068+
return cos(expression, unit);
2069+
}
2070+
2071+
/**
2072+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in
2073+
* {@link AngularDimension#RADIANS}.
2074+
*
2075+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2076+
* numeric value
2077+
* @return new instance of {@link Cos}.
2078+
*/
2079+
public static Cos cos(Object value) {
2080+
return cos(value, AngularDimension.RADIANS);
2081+
}
2082+
2083+
/**
2084+
* Creates a new {@link AggregationExpression} that calculates the cosine of a value that is measured in the given
2085+
* {@link AngularDimension unit}.
2086+
*
2087+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2088+
* numeric value.
2089+
* @param unit the unit of measure used by the value of the given field.
2090+
* @return new instance of {@link Cos}.
2091+
*/
2092+
public static Cos cos(Object value, AngularDimension unit) {
2093+
2094+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2095+
return new Cos(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2096+
}
2097+
return new Cos(value);
2098+
}
2099+
2100+
@Override
2101+
protected String getMongoMethod() {
2102+
return "$cos";
2103+
}
2104+
}
2105+
2106+
/**
2107+
* An {@link AggregationExpression expression} that calculates the hyperbolic cosine of a value that is measured in
2108+
* {@link AngularDimension#RADIANS}.
2109+
*
2110+
* @author Christoph Strobl
2111+
* @since 3.3
2112+
*/
2113+
public static class Cosh extends AbstractAggregationExpression {
2114+
2115+
private Cosh(Object value) {
2116+
super(value);
2117+
}
2118+
2119+
/**
2120+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2121+
* {@link AngularDimension#RADIANS}.
2122+
*
2123+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2124+
* @return new instance of {@link Cosh}.
2125+
*/
2126+
public static Cosh coshOf(String fieldReference) {
2127+
return coshOf(fieldReference, AngularDimension.RADIANS);
2128+
}
2129+
2130+
/**
2131+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2132+
* the given {@link AngularDimension unit}.
2133+
* <p />
2134+
* Use {@code coshOf("angle", DEGREES)} as shortcut for
2135+
*
2136+
* <pre>
2137+
* { $cosh : { $degreesToRadians : "$angle" } }
2138+
* </pre>
2139+
*
2140+
* .
2141+
*
2142+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2143+
* @param unit the unit of measure used by the value of the given field.
2144+
* @return new instance of {@link Cosh}.
2145+
*/
2146+
public static Cosh coshOf(String fieldReference, AngularDimension unit) {
2147+
return cosh(Fields.field(fieldReference), unit);
2148+
}
2149+
2150+
/**
2151+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2152+
* {@link AngularDimension#RADIANS}.
2153+
* <p />
2154+
* Use {@code sinhOf("angle", DEGREES)} as shortcut for eg.
2155+
* {@code sinhOf(ConvertOperators.valueOf("angle").degreesToRadians())}.
2156+
*
2157+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2158+
* @return new instance of {@link Cosh}.
2159+
*/
2160+
public static Cosh coshOf(AggregationExpression expression) {
2161+
return coshOf(expression, AngularDimension.RADIANS);
2162+
}
2163+
2164+
/**
2165+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2166+
* the given {@link AngularDimension unit}.
2167+
*
2168+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2169+
* @param unit the unit of measure used by the value of the given field.
2170+
* @return new instance of {@link Cosh}.
2171+
*/
2172+
public static Cosh coshOf(AggregationExpression expression, AngularDimension unit) {
2173+
return cosh(expression, unit);
2174+
}
2175+
2176+
/**
2177+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2178+
* {@link AngularDimension#RADIANS}.
2179+
*
2180+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2181+
* numeric value.
2182+
* @return new instance of {@link Cosh}.
2183+
*/
2184+
public static Cosh cosh(Object value) {
2185+
return cosh(value, AngularDimension.RADIANS);
2186+
}
2187+
2188+
/**
2189+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic cosine of a value that is measured in
2190+
* the given {@link AngularDimension unit}.
2191+
*
2192+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2193+
* numeric value
2194+
* @param unit the unit of measure used by the value of the given field.
2195+
* @return new instance of {@link Cosh}.
2196+
*/
2197+
public static Cosh cosh(Object value, AngularDimension unit) {
2198+
2199+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2200+
return new Cosh(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2201+
}
2202+
return new Cosh(value);
2203+
}
2204+
2205+
@Override
2206+
protected String getMongoMethod() {
2207+
return "$cosh";
2208+
}
2209+
}
2210+
19602211
/**
19612212
* An {@link AggregationExpression expression} that calculates the tangent of a value that is measured in radians.
19622213
*

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

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public class MethodReferenceNode extends ExpressionNode {
8787
map.put("round", arrayArgRef().forOperator("$round"));
8888
map.put("sin", singleArgRef().forOperator("$sin"));
8989
map.put("sinh", singleArgRef().forOperator("$sinh"));
90+
map.put("cos", singleArgRef().forOperator("$cos"));
91+
map.put("cosh", singleArgRef().forOperator("$cosh"));
9092
map.put("tan", singleArgRef().forOperator("$tan"));
9193
map.put("tanh", singleArgRef().forOperator("$tanh"));
9294

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

+28
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,34 @@ void rendersSinhWithValueInDegrees() {
8888
.isEqualTo(Document.parse("{ $sinh : { $degreesToRadians : \"$angle\" } }"));
8989
}
9090

91+
@Test // GH-3710
92+
void rendersCos() {
93+
94+
assertThat(valueOf("angle").cos().toDocument(Aggregation.DEFAULT_CONTEXT))
95+
.isEqualTo(Document.parse("{ $cos : \"$angle\" }"));
96+
}
97+
98+
@Test // GH-3710
99+
void rendersCosWithValueInDegrees() {
100+
101+
assertThat(valueOf("angle").cos(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
102+
.isEqualTo(Document.parse("{ $cos : { $degreesToRadians : \"$angle\" } }"));
103+
}
104+
105+
@Test // GH-3710
106+
void rendersCosh() {
107+
108+
assertThat(valueOf("angle").cosh().toDocument(Aggregation.DEFAULT_CONTEXT))
109+
.isEqualTo(Document.parse("{ $cosh : \"$angle\" }"));
110+
}
111+
112+
@Test // GH-3710
113+
void rendersCoshWithValueInDegrees() {
114+
115+
assertThat(valueOf("angle").cosh(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
116+
.isEqualTo(Document.parse("{ $cosh : { $degreesToRadians : \"$angle\" } }"));
117+
}
118+
91119
@Test // GH-3730
92120
void rendersTan() {
93121

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

+10
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,16 @@ void shouldRenderSinh() {
961961
assertThat(transform("sinh(angle)")).isEqualTo(Document.parse("{ \"$sinh\" : \"$angle\"}"));
962962
}
963963

964+
@Test // GH-3710
965+
void shouldRenderCos() {
966+
assertThat(transform("cos(angle)")).isEqualTo(Document.parse("{ \"$cos\" : \"$angle\"}"));
967+
}
968+
969+
@Test // GH-3710
970+
void shouldRenderCosh() {
971+
assertThat(transform("cosh(angle)")).isEqualTo(Document.parse("{ \"$cosh\" : \"$angle\"}"));
972+
}
973+
964974
@Test // GH-3730
965975
void shouldRenderTan() {
966976
assertThat(transform("tan(angle)")).isEqualTo(Document.parse("{ \"$tan\" : \"$angle\"}"));

0 commit comments

Comments
 (0)