Skip to content

Commit 7c5b04f

Browse files
Add support for $tan & $tanh aggregation operators.
Closes: #3730
1 parent 3235485 commit 7c5b04f

File tree

4 files changed

+292
-0
lines changed

4 files changed

+292
-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 tangent 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 Tan tan() {
586+
return tan(AngularDimension.RADIANS);
587+
}
588+
589+
/**
590+
* Creates new {@link AggregationExpression} that calculates the tangent 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 Tan tan(AngularDimension unit) {
598+
return usesFieldRef() ? Tan.tanOf(fieldReference, unit) : Tan.tanOf(expression, unit);
599+
}
600+
601+
/**
602+
* Creates new {@link AggregationExpression} that calculates the hyperbolic tangent 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 Tanh tanh() {
609+
return tanh(AngularDimension.RADIANS);
610+
}
611+
612+
/**
613+
* Creates new {@link AggregationExpression} that calculates the hyperbolic tangent 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 Tanh tanh(AngularDimension unit) {
620+
return usesFieldRef() ? Tanh.tanhOf(fieldReference, unit) : Tanh.tanhOf(expression, unit);
621+
}
622+
578623
private boolean usesFieldRef() {
579624
return fieldReference != null;
580625
}
@@ -1911,4 +1956,210 @@ protected String getMongoMethod() {
19111956
return "$sinh";
19121957
}
19131958
}
1959+
1960+
/**
1961+
* An {@link AggregationExpression expression} that calculates the tangent of a value that is measured in radians.
1962+
*
1963+
* @author Christoph Strobl
1964+
* @since 3.3
1965+
*/
1966+
public static class Tan extends AbstractAggregationExpression {
1967+
1968+
private Tan(Object value) {
1969+
super(value);
1970+
}
1971+
1972+
/**
1973+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in
1974+
* {@link AngularDimension#RADIANS radians}.
1975+
* <p />
1976+
* Use {@code tanOf("angle", DEGREES)} as shortcut for
1977+
*
1978+
* <pre>
1979+
* { $tan : { $degreesToRadians : "$angle" } }
1980+
* </pre>
1981+
*
1982+
* .
1983+
*
1984+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
1985+
* @return new instance of {@link Tan}.
1986+
*/
1987+
public static Tan tanOf(String fieldReference) {
1988+
return tanOf(fieldReference, AngularDimension.RADIANS);
1989+
}
1990+
1991+
/**
1992+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in the given
1993+
* {@link AngularDimension unit}.
1994+
*
1995+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
1996+
* @param unit the unit of measure used by the value of the given field.
1997+
* @return new instance of {@link Tan}.
1998+
*/
1999+
public static Tan tanOf(String fieldReference, AngularDimension unit) {
2000+
return tan(Fields.field(fieldReference), unit);
2001+
}
2002+
2003+
/**
2004+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in
2005+
* {@link AngularDimension#RADIANS}.
2006+
*
2007+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2008+
* @return new instance of {@link Tan}.
2009+
*/
2010+
public static Tan tanOf(AggregationExpression expression) {
2011+
return tanOf(expression, AngularDimension.RADIANS);
2012+
}
2013+
2014+
/**
2015+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in the given
2016+
* {@link AngularDimension unit}.
2017+
*
2018+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2019+
* @param unit the unit of measure used by the value of the given field.
2020+
* @return new instance of {@link Tan}.
2021+
*/
2022+
public static Tan tanOf(AggregationExpression expression, AngularDimension unit) {
2023+
return tan(expression, unit);
2024+
}
2025+
2026+
/**
2027+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in
2028+
* {@link AngularDimension#RADIANS}.
2029+
*
2030+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2031+
* numeric value
2032+
* @return new instance of {@link Tan}.
2033+
*/
2034+
public static Tan tan(Object value) {
2035+
return tan(value, AngularDimension.RADIANS);
2036+
}
2037+
2038+
/**
2039+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in the given
2040+
* {@link AngularDimension unit}.
2041+
*
2042+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2043+
* numeric value.
2044+
* @param unit the unit of measure used by the value of the given field.
2045+
* @return new instance of {@link Tan}.
2046+
*/
2047+
public static Tan tan(Object value, AngularDimension unit) {
2048+
2049+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2050+
return new Tan(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2051+
}
2052+
return new Tan(value);
2053+
}
2054+
2055+
@Override
2056+
protected String getMongoMethod() {
2057+
return "$tan";
2058+
}
2059+
}
2060+
2061+
/**
2062+
* An {@link AggregationExpression expression} that calculates the hyperbolic tangent of a value that is measured in
2063+
* {@link AngularDimension#RADIANS}.
2064+
*
2065+
* @author Christoph Strobl
2066+
* @since 3.3
2067+
*/
2068+
public static class Tanh extends AbstractAggregationExpression {
2069+
2070+
private Tanh(Object value) {
2071+
super(value);
2072+
}
2073+
2074+
/**
2075+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2076+
* {@link AngularDimension#RADIANS}.
2077+
*
2078+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2079+
* @return new instance of {@link Tanh}.
2080+
*/
2081+
public static Tanh tanhOf(String fieldReference) {
2082+
return tanhOf(fieldReference, AngularDimension.RADIANS);
2083+
}
2084+
2085+
/**
2086+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2087+
* the given {@link AngularDimension unit}.
2088+
* <p />
2089+
* Use {@code tanhOf("angle", DEGREES)} as shortcut for
2090+
*
2091+
* <pre>
2092+
* { $tanh : { $degreesToRadians : "$angle" } }
2093+
* </pre>
2094+
*
2095+
* .
2096+
*
2097+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2098+
* @param unit the unit of measure used by the value of the given field.
2099+
* @return new instance of {@link Tanh}.
2100+
*/
2101+
public static Tanh tanhOf(String fieldReference, AngularDimension unit) {
2102+
return tanh(Fields.field(fieldReference), unit);
2103+
}
2104+
2105+
/**
2106+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2107+
* {@link AngularDimension#RADIANS}.
2108+
* <p />
2109+
* Use {@code sinhOf("angle", DEGREES)} as shortcut for eg.
2110+
* {@code sinhOf(ConvertOperators.valueOf("angle").degreesToRadians())}.
2111+
*
2112+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2113+
* @return new instance of {@link Tanh}.
2114+
*/
2115+
public static Tanh tanhOf(AggregationExpression expression) {
2116+
return tanhOf(expression, AngularDimension.RADIANS);
2117+
}
2118+
2119+
/**
2120+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2121+
* the given {@link AngularDimension unit}.
2122+
*
2123+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2124+
* @param unit the unit of measure used by the value of the given field.
2125+
* @return new instance of {@link Tanh}.
2126+
*/
2127+
public static Tanh tanhOf(AggregationExpression expression, AngularDimension unit) {
2128+
return tanh(expression, unit);
2129+
}
2130+
2131+
/**
2132+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2133+
* {@link AngularDimension#RADIANS}.
2134+
*
2135+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2136+
* numeric value.
2137+
* @return new instance of {@link Tanh}.
2138+
*/
2139+
public static Tanh tanh(Object value) {
2140+
return tanh(value, AngularDimension.RADIANS);
2141+
}
2142+
2143+
/**
2144+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2145+
* the given {@link AngularDimension unit}.
2146+
*
2147+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2148+
* numeric value
2149+
* @param unit the unit of measure used by the value of the given field.
2150+
* @return new instance of {@link Tanh}.
2151+
*/
2152+
public static Tanh tanh(Object value, AngularDimension unit) {
2153+
2154+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2155+
return new Tanh(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2156+
}
2157+
return new Tanh(value);
2158+
}
2159+
2160+
@Override
2161+
protected String getMongoMethod() {
2162+
return "$tanh";
2163+
}
2164+
}
19142165
}

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("tan", singleArgRef().forOperator("$tan"));
91+
map.put("tanh", singleArgRef().forOperator("$tanh"));
9092

9193
// STRING OPERATORS
9294
map.put("concat", arrayArgRef().forOperator("$concat"));

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

+29
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,33 @@ void rendersSinhWithValueInDegrees() {
8787
assertThat(valueOf("angle").sinh(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
8888
.isEqualTo(Document.parse("{ $sinh : { $degreesToRadians : \"$angle\" } }"));
8989
}
90+
91+
@Test // GH-3730
92+
void rendersTan() {
93+
94+
assertThat(valueOf("angle").tan().toDocument(Aggregation.DEFAULT_CONTEXT))
95+
.isEqualTo(Document.parse("{ $tan : \"$angle\" }"));
96+
}
97+
98+
@Test // GH-3730
99+
void rendersTanWithValueInDegrees() {
100+
101+
assertThat(valueOf("angle").tan(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
102+
.isEqualTo(Document.parse("{ $tan : { $degreesToRadians : \"$angle\" } }"));
103+
}
104+
105+
@Test // GH-3730
106+
void rendersTanh() {
107+
108+
assertThat(valueOf("angle").tanh().toDocument(Aggregation.DEFAULT_CONTEXT))
109+
.isEqualTo(Document.parse("{ $tanh : \"$angle\" }"));
110+
}
111+
112+
@Test // GH-3730
113+
void rendersTanhWithValueInDegrees() {
114+
115+
assertThat(valueOf("angle").tanh(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
116+
.isEqualTo(Document.parse("{ $tanh : { $degreesToRadians : \"$angle\" } }"));
117+
}
118+
90119
}

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-3730
965+
void shouldRenderTan() {
966+
assertThat(transform("tan(angle)")).isEqualTo(Document.parse("{ \"$tan\" : \"$angle\"}"));
967+
}
968+
969+
@Test // GH-3730
970+
void shouldRenderTanh() {
971+
assertThat(transform("tanh(angle)")).isEqualTo(Document.parse("{ \"$tanh\" : \"$angle\"}"));
972+
}
973+
964974
private Object transform(String expression, Object... params) {
965975
Object result = transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
966976
return result == null ? null : (!(result instanceof org.bson.Document) ? result.toString() : result);

0 commit comments

Comments
 (0)