Skip to content

Commit 578441e

Browse files
DATAMONGO-1327 - Polishing.
Just added overloads for stdDevSamp and stdDevPop taking AggregationExpression and updated the doc. Also replaced String operation based MongoDB operation building by using operators directly. Original Pull Request: #360
1 parent 36838ff commit 578441e

File tree

1 file changed

+37
-15
lines changed
  • spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation

1 file changed

+37
-15
lines changed

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

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Thomas Darimont
4141
* @author Oliver Gierke
4242
* @author Gustavo de Geus
43+
* @author Christoph Strobl
4344
* @since 1.3
4445
*/
4546
public class GroupOperation implements FieldsExposingAggregationOperation {
@@ -312,23 +313,47 @@ public GroupOperationBuilder max(AggregationExpression expr) {
312313
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevSamp}-expression that for the given
313314
* field-reference.
314315
*
315-
* @param reference
316-
* @return
316+
* @param reference must not be {@literal null}.
317+
* @return never {@literal null}.
318+
* @since 1.10
317319
*/
318320
public GroupOperationBuilder stdDevSamp(String reference) {
319321
return newBuilder(GroupOps.STD_DEV_SAMP, reference, null);
320322
}
321323

324+
/**
325+
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevSamp}-expression that for the given {@link AggregationExpression}.
326+
*
327+
* @param expr must not be {@literal null}.
328+
* @return never {@literal null}.
329+
* @since 1.10
330+
*/
331+
public GroupOperationBuilder stdDevSamp(AggregationExpression expr) {
332+
return newBuilder(GroupOps.STD_DEV_SAMP, null, expr);
333+
}
334+
322335
/**
323336
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevPop}-expression that for the given field-reference.
324337
*
325-
* @param reference
326-
* @return
338+
* @param reference must not be {@literal null}.
339+
* @return never {@literal null}.
340+
* @since 1.10
327341
*/
328342
public GroupOperationBuilder stdDevPop(String reference) {
329343
return newBuilder(GroupOps.STD_DEV_POP, reference, null);
330344
}
331345

346+
/**
347+
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevPop}-expression that for the given {@link AggregationExpression}.
348+
*
349+
* @param expr must not be {@literal null}.
350+
* @return never {@literal null}.
351+
* @since 1.10
352+
*/
353+
public GroupOperationBuilder stdDevPop(AggregationExpression expr) {
354+
return newBuilder(GroupOps.STD_DEV_POP, null, expr);
355+
}
356+
332357
private GroupOperationBuilder newBuilder(Keyword keyword, String reference, Object value) {
333358
return new GroupOperationBuilder(this, new Operation(keyword, null, reference, value));
334359
}
@@ -393,21 +418,18 @@ interface Keyword {
393418

394419
private static enum GroupOps implements Keyword {
395420

396-
SUM, LAST, FIRST, PUSH, AVG, MIN, MAX, ADD_TO_SET, COUNT, STD_DEV_SAMP, STD_DEV_POP;
397-
398-
@Override
399-
public String toString() {
421+
SUM("$sum"), LAST("$last"), FIRST("$first"), PUSH("$push"), AVG("$avg"), MIN("$min"), MAX("$max"), ADD_TO_SET("$addToSet"), STD_DEV_POP("$stdDevPop"), STD_DEV_SAMP("$stdDevSamp");
400422

401-
String[] parts = name().split("_");
423+
private String mongoOperator;
402424

403-
StringBuilder builder = new StringBuilder();
425+
GroupOps(String mongoOperator) {
426+
this.mongoOperator = mongoOperator;
427+
}
404428

405-
for (String part : parts) {
406-
String lowerCase = part.toLowerCase(Locale.US);
407-
builder.append(builder.length() == 0 ? lowerCase : StringUtils.capitalize(lowerCase));
408-
}
409429

410-
return "$" + builder.toString();
430+
@Override
431+
public String toString() {
432+
return mongoOperator;
411433
}
412434
}
413435

0 commit comments

Comments
 (0)