Skip to content

Commit 329396d

Browse files
christophstroblmp911de
authored andcommitted
Add support for $tan and $tanh aggregation operators.
Closes: #3730 Original pull request: #3755.
1 parent 5f915ec commit 329396d

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
@@ -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 tangent 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 Tan tan() {
730+
return tan(AngularDimension.RADIANS);
731+
}
732+
733+
/**
734+
* Creates new {@link AggregationExpression} that calculates the tangent 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 Tan tan(AngularDimension unit) {
742+
return usesFieldRef() ? Tan.tanOf(fieldReference, unit) : Tan.tanOf(expression, unit);
743+
}
744+
745+
/**
746+
* Creates new {@link AggregationExpression} that calculates the hyperbolic tangent 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 Tanh tanh() {
753+
return tanh(AngularDimension.RADIANS);
754+
}
755+
756+
/**
757+
* Creates new {@link AggregationExpression} that calculates the hyperbolic tangent 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 Tanh tanh(AngularDimension unit) {
764+
return usesFieldRef() ? Tanh.tanhOf(fieldReference, unit) : Tanh.tanhOf(expression, unit);
765+
}
766+
722767
private boolean usesFieldRef() {
723768
return fieldReference != null;
724769
}
@@ -2153,4 +2198,210 @@ protected String getMongoMethod() {
21532198
return "$sinh";
21542199
}
21552200
}
2201+
2202+
/**
2203+
* An {@link AggregationExpression expression} that calculates the tangent of a value that is measured in radians.
2204+
*
2205+
* @author Christoph Strobl
2206+
* @since 3.3
2207+
*/
2208+
public static class Tan extends AbstractAggregationExpression {
2209+
2210+
private Tan(Object value) {
2211+
super(value);
2212+
}
2213+
2214+
/**
2215+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in
2216+
* {@link AngularDimension#RADIANS radians}.
2217+
* <p />
2218+
* Use {@code tanOf("angle", DEGREES)} as shortcut for
2219+
*
2220+
* <pre>
2221+
* { $tan : { $degreesToRadians : "$angle" } }
2222+
* </pre>
2223+
*
2224+
* .
2225+
*
2226+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2227+
* @return new instance of {@link Tan}.
2228+
*/
2229+
public static Tan tanOf(String fieldReference) {
2230+
return tanOf(fieldReference, AngularDimension.RADIANS);
2231+
}
2232+
2233+
/**
2234+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in the given
2235+
* {@link AngularDimension unit}.
2236+
*
2237+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2238+
* @param unit the unit of measure used by the value of the given field.
2239+
* @return new instance of {@link Tan}.
2240+
*/
2241+
public static Tan tanOf(String fieldReference, AngularDimension unit) {
2242+
return tan(Fields.field(fieldReference), unit);
2243+
}
2244+
2245+
/**
2246+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in
2247+
* {@link AngularDimension#RADIANS}.
2248+
*
2249+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2250+
* @return new instance of {@link Tan}.
2251+
*/
2252+
public static Tan tanOf(AggregationExpression expression) {
2253+
return tanOf(expression, AngularDimension.RADIANS);
2254+
}
2255+
2256+
/**
2257+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in the given
2258+
* {@link AngularDimension unit}.
2259+
*
2260+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2261+
* @param unit the unit of measure used by the value of the given field.
2262+
* @return new instance of {@link Tan}.
2263+
*/
2264+
public static Tan tanOf(AggregationExpression expression, AngularDimension unit) {
2265+
return tan(expression, unit);
2266+
}
2267+
2268+
/**
2269+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in
2270+
* {@link AngularDimension#RADIANS}.
2271+
*
2272+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2273+
* numeric value
2274+
* @return new instance of {@link Tan}.
2275+
*/
2276+
public static Tan tan(Object value) {
2277+
return tan(value, AngularDimension.RADIANS);
2278+
}
2279+
2280+
/**
2281+
* Creates a new {@link AggregationExpression} that calculates the tangent of a value that is measured in the given
2282+
* {@link AngularDimension unit}.
2283+
*
2284+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2285+
* numeric value.
2286+
* @param unit the unit of measure used by the value of the given field.
2287+
* @return new instance of {@link Tan}.
2288+
*/
2289+
public static Tan tan(Object value, AngularDimension unit) {
2290+
2291+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2292+
return new Tan(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2293+
}
2294+
return new Tan(value);
2295+
}
2296+
2297+
@Override
2298+
protected String getMongoMethod() {
2299+
return "$tan";
2300+
}
2301+
}
2302+
2303+
/**
2304+
* An {@link AggregationExpression expression} that calculates the hyperbolic tangent of a value that is measured in
2305+
* {@link AngularDimension#RADIANS}.
2306+
*
2307+
* @author Christoph Strobl
2308+
* @since 3.3
2309+
*/
2310+
public static class Tanh extends AbstractAggregationExpression {
2311+
2312+
private Tanh(Object value) {
2313+
super(value);
2314+
}
2315+
2316+
/**
2317+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2318+
* {@link AngularDimension#RADIANS}.
2319+
*
2320+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2321+
* @return new instance of {@link Tanh}.
2322+
*/
2323+
public static Tanh tanhOf(String fieldReference) {
2324+
return tanhOf(fieldReference, AngularDimension.RADIANS);
2325+
}
2326+
2327+
/**
2328+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2329+
* the given {@link AngularDimension unit}.
2330+
* <p />
2331+
* Use {@code tanhOf("angle", DEGREES)} as shortcut for
2332+
*
2333+
* <pre>
2334+
* { $tanh : { $degreesToRadians : "$angle" } }
2335+
* </pre>
2336+
*
2337+
* .
2338+
*
2339+
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
2340+
* @param unit the unit of measure used by the value of the given field.
2341+
* @return new instance of {@link Tanh}.
2342+
*/
2343+
public static Tanh tanhOf(String fieldReference, AngularDimension unit) {
2344+
return tanh(Fields.field(fieldReference), unit);
2345+
}
2346+
2347+
/**
2348+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2349+
* {@link AngularDimension#RADIANS}.
2350+
* <p />
2351+
* Use {@code sinhOf("angle", DEGREES)} as shortcut for eg.
2352+
* {@code sinhOf(ConvertOperators.valueOf("angle").degreesToRadians())}.
2353+
*
2354+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2355+
* @return new instance of {@link Tanh}.
2356+
*/
2357+
public static Tanh tanhOf(AggregationExpression expression) {
2358+
return tanhOf(expression, AngularDimension.RADIANS);
2359+
}
2360+
2361+
/**
2362+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2363+
* the given {@link AngularDimension unit}.
2364+
*
2365+
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
2366+
* @param unit the unit of measure used by the value of the given field.
2367+
* @return new instance of {@link Tanh}.
2368+
*/
2369+
public static Tanh tanhOf(AggregationExpression expression, AngularDimension unit) {
2370+
return tanh(expression, unit);
2371+
}
2372+
2373+
/**
2374+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2375+
* {@link AngularDimension#RADIANS}.
2376+
*
2377+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2378+
* numeric value.
2379+
* @return new instance of {@link Tanh}.
2380+
*/
2381+
public static Tanh tanh(Object value) {
2382+
return tanh(value, AngularDimension.RADIANS);
2383+
}
2384+
2385+
/**
2386+
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
2387+
* the given {@link AngularDimension unit}.
2388+
*
2389+
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
2390+
* numeric value
2391+
* @param unit the unit of measure used by the value of the given field.
2392+
* @return new instance of {@link Tanh}.
2393+
*/
2394+
public static Tanh tanh(Object value, AngularDimension unit) {
2395+
2396+
if (ObjectUtils.nullSafeEquals(AngularDimension.DEGREES, unit)) {
2397+
return new Tanh(ConvertOperators.DegreesToRadians.degreesToRadians(value));
2398+
}
2399+
return new Tanh(value);
2400+
}
2401+
2402+
@Override
2403+
protected String getMongoMethod() {
2404+
return "$tanh";
2405+
}
2406+
}
21562407
}

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

99101
// STRING OPERATORS
100102
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
@@ -109,4 +109,33 @@ void rendersSinhWithValueInDegrees() {
109109
assertThat(valueOf("angle").sinh(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
110110
.isEqualTo(Document.parse("{ $sinh : { $degreesToRadians : \"$angle\" } }"));
111111
}
112+
113+
@Test // GH-3730
114+
void rendersTan() {
115+
116+
assertThat(valueOf("angle").tan().toDocument(Aggregation.DEFAULT_CONTEXT))
117+
.isEqualTo(Document.parse("{ $tan : \"$angle\" }"));
118+
}
119+
120+
@Test // GH-3730
121+
void rendersTanWithValueInDegrees() {
122+
123+
assertThat(valueOf("angle").tan(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
124+
.isEqualTo(Document.parse("{ $tan : { $degreesToRadians : \"$angle\" } }"));
125+
}
126+
127+
@Test // GH-3730
128+
void rendersTanh() {
129+
130+
assertThat(valueOf("angle").tanh().toDocument(Aggregation.DEFAULT_CONTEXT))
131+
.isEqualTo(Document.parse("{ $tanh : \"$angle\" }"));
132+
}
133+
134+
@Test // GH-3730
135+
void rendersTanhWithValueInDegrees() {
136+
137+
assertThat(valueOf("angle").tanh(AngularDimension.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
138+
.isEqualTo(Document.parse("{ $tanh : { $degreesToRadians : \"$angle\" } }"));
139+
}
140+
112141
}

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-3730
1018+
void shouldRenderTan() {
1019+
assertThat(transform("tan(angle)")).isEqualTo(Document.parse("{ \"$tan\" : \"$angle\"}"));
1020+
}
1021+
1022+
@Test // GH-3730
1023+
void shouldRenderTanh() {
1024+
assertThat(transform("tanh(angle)")).isEqualTo(Document.parse("{ \"$tanh\" : \"$angle\"}"));
1025+
}
1026+
10171027
private Object transform(String expression, Object... params) {
10181028
Object result = transformer.transform(expression, Aggregation.DEFAULT_CONTEXT, params);
10191029
return result == null ? null : (!(result instanceof org.bson.Document) ? result.toString() : result);

0 commit comments

Comments
 (0)