@@ -199,11 +199,61 @@ private CovarianceSamp covarianceSamp() {
199
199
: CovarianceSamp .covarianceSampOf (expression );
200
200
}
201
201
202
+ /**
203
+ * Creates new {@link ExpMovingAvgBuilder} that to build {@link AggregationExpression expMovingAvg} that calculates
204
+ * the exponential moving average of numeric values
205
+ *
206
+ * @return new instance of {@link ExpMovingAvg}.
207
+ * @since 3.3
208
+ */
209
+ public ExpMovingAvgBuilder expMovingAvg () {
210
+
211
+ ExpMovingAvg expMovingAvg = usesFieldRef () ? ExpMovingAvg .expMovingAvgOf (fieldReference )
212
+ : ExpMovingAvg .expMovingAvgOf (expression );
213
+ return new ExpMovingAvgBuilder () {
214
+
215
+ @ Override
216
+ public ExpMovingAvg historicalDocuments (int numberOfHistoricalDocuments ) {
217
+ return expMovingAvg .n (numberOfHistoricalDocuments );
218
+ }
219
+
220
+ @ Override
221
+ public ExpMovingAvg alpha (double exponentialDecayValue ) {
222
+ return expMovingAvg .alpha (exponentialDecayValue );
223
+ }
224
+ };
225
+ }
226
+
202
227
private boolean usesFieldRef () {
203
228
return fieldReference != null ;
204
229
}
205
230
}
206
231
232
+ /**
233
+ * Builder for {@link ExpMovingAvg}.
234
+ *
235
+ * @since 3.3
236
+ */
237
+ public interface ExpMovingAvgBuilder {
238
+
239
+ /**
240
+ * Define the number of historical documents with significant mathematical weight.
241
+ *
242
+ * @param numberOfHistoricalDocuments
243
+ * @return new instance of {@link ExpMovingAvg}.
244
+ */
245
+ ExpMovingAvg historicalDocuments (int numberOfHistoricalDocuments );
246
+
247
+ /**
248
+ * Define the exponential decay value.
249
+ *
250
+ * @param exponentialDecayValue
251
+ * @return new instance of {@link ExpMovingAvg}.
252
+ */
253
+ ExpMovingAvg alpha (double exponentialDecayValue );
254
+
255
+ }
256
+
207
257
/**
208
258
* {@link AggregationExpression} for {@code $sum}.
209
259
*
@@ -835,4 +885,65 @@ protected String getMongoMethod() {
835
885
return "$covarianceSamp" ;
836
886
}
837
887
}
888
+
889
+ /**
890
+ * {@link ExpMovingAvg} calculates the exponential moving average of numeric values.
891
+ *
892
+ * @author Christoph Strobl
893
+ * @since 3.3
894
+ */
895
+ public static class ExpMovingAvg extends AbstractAggregationExpression {
896
+
897
+ private ExpMovingAvg (Object value ) {
898
+ super (value );
899
+ }
900
+
901
+ /**
902
+ * Create a new {@link ExpMovingAvg} by defining the field holding the value to be used as input.
903
+ *
904
+ * @param fieldReference must not be {@literal null}.
905
+ * @return new instance of {@link ExpMovingAvg}.
906
+ */
907
+ public static ExpMovingAvg expMovingAvgOf (String fieldReference ) {
908
+ return new ExpMovingAvg (Collections .singletonMap ("input" , Fields .field (fieldReference )));
909
+ }
910
+
911
+ /**
912
+ * Create a new {@link ExpMovingAvg} by defining the {@link AggregationExpression expression} to compute the value
913
+ * to be used as input.
914
+ *
915
+ * @param expression must not be {@literal null}.
916
+ * @return new instance of {@link ExpMovingAvg}.
917
+ */
918
+ public static ExpMovingAvg expMovingAvgOf (AggregationExpression expression ) {
919
+ return new ExpMovingAvg (Collections .singletonMap ("input" , expression ));
920
+ }
921
+
922
+ /**
923
+ * Define the number of historical documents with significant mathematical weight. <br />
924
+ * Specify either {@link #n(int) N} or {@link #alpha(double) aplha}. Not both!
925
+ *
926
+ * @param numberOfHistoricalDocuments
927
+ * @return new instance of {@link ExpMovingAvg}.
928
+ */
929
+ public ExpMovingAvg n /*umber of historical documents*/ (int numberOfHistoricalDocuments ) {
930
+ return new ExpMovingAvg (append ("N" , numberOfHistoricalDocuments ));
931
+ }
932
+
933
+ /**
934
+ * Define the exponential decay value. <br />
935
+ * Specify either {@link #alpha(double) aplha} or {@link #n(int) N}. Not both!
936
+ *
937
+ * @param exponentialDecayValue
938
+ * @return new instance of {@link ExpMovingAvg}.
939
+ */
940
+ public ExpMovingAvg alpha (double exponentialDecayValue ) {
941
+ return new ExpMovingAvg (append ("alpha" , exponentialDecayValue ));
942
+ }
943
+
944
+ @ Override
945
+ protected String getMongoMethod () {
946
+ return "$expMovingAvg" ;
947
+ }
948
+ }
838
949
}
0 commit comments