/driver-core/src/main/com/mongodb/client/model/Accumulators.java

https://github.com/foursquare/mongo-java-driver · Java · 183 lines · 38 code · 14 blank · 131 comment · 0 complexity · f3e95e79e847b43bcf2c5deedf5a4f76 MD5 · raw file

  1. /*
  2. * Copyright 2015 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package com.mongodb.client.model;
  15. /**
  16. * Builders for accumulators used in the group pipeline stage of an aggregation pipeline.
  17. *
  18. * @mongodb.driver.manual core/aggregation-pipeline/ Aggregation pipeline
  19. * @mongodb.driver.manual operator/aggregation/group/#accumulator-operator Accumulators
  20. * @mongodb.driver.manual meta/aggregation-quick-reference/#aggregation-expressions Expressions
  21. * @mongodb.server.release 2.2
  22. * @since 3.1
  23. */
  24. public final class Accumulators {
  25. /**
  26. * Gets a field name for a $group operation representing the sum of the values of the given expression when applied to all members of
  27. * the group.
  28. *
  29. * @param fieldName the field name
  30. * @param expression the expression
  31. * @param <TExpression> the expression type
  32. * @return the field
  33. * @mongodb.driver.manual reference/operator/aggregation/sum/ $sum
  34. */
  35. public static <TExpression> BsonField sum(final String fieldName, final TExpression expression) {
  36. return accumulator("$sum", fieldName, expression);
  37. }
  38. /**
  39. * Gets a field name for a $group operation representing the average of the values of the given expression when applied to all
  40. * members of the group.
  41. *
  42. * @param fieldName the field name
  43. * @param expression the expression
  44. * @param <TExpression> the expression type
  45. * @return the field
  46. * @mongodb.driver.manual reference/operator/aggregation/avg/ $avg
  47. */
  48. public static <TExpression> BsonField avg(final String fieldName, final TExpression expression) {
  49. return accumulator("$avg", fieldName, expression);
  50. }
  51. /**
  52. * Gets a field name for a $group operation representing the value of the given expression when applied to the first member of
  53. * the group.
  54. *
  55. * @param fieldName the field name
  56. * @param expression the expression
  57. * @param <TExpression> the expression type
  58. * @return the field
  59. * @mongodb.driver.manual reference/operator/aggregation/first/ $first
  60. */
  61. public static <TExpression> BsonField first(final String fieldName, final TExpression expression) {
  62. return accumulator("$first", fieldName, expression);
  63. }
  64. /**
  65. * Gets a field name for a $group operation representing the value of the given expression when applied to the last member of
  66. * the group.
  67. *
  68. * @param fieldName the field name
  69. * @param expression the expression
  70. * @param <TExpression> the expression type
  71. * @return the field
  72. * @mongodb.driver.manual reference/operator/aggregation/last/ $last
  73. */
  74. public static <TExpression> BsonField last(final String fieldName, final TExpression expression) {
  75. return accumulator("$last", fieldName, expression);
  76. }
  77. /**
  78. * Gets a field name for a $group operation representing the maximum of the values of the given expression when applied to all
  79. * members of the group.
  80. *
  81. * @param fieldName the field name
  82. * @param expression the expression
  83. * @param <TExpression> the expression type
  84. * @return the field
  85. * @mongodb.driver.manual reference/operator/aggregation/max/ $max
  86. */
  87. public static <TExpression> BsonField max(final String fieldName, final TExpression expression) {
  88. return accumulator("$max", fieldName, expression);
  89. }
  90. /**
  91. * Gets a field name for a $group operation representing the minimum of the values of the given expression when applied to all
  92. * members of the group.
  93. *
  94. * @param fieldName the field name
  95. * @param expression the expression
  96. * @param <TExpression> the expression type
  97. * @return the field
  98. * @mongodb.driver.manual reference/operator/aggregation/min/ $min
  99. */
  100. public static <TExpression> BsonField min(final String fieldName, final TExpression expression) {
  101. return accumulator("$min", fieldName, expression);
  102. }
  103. /**
  104. * Gets a field name for a $group operation representing an array of all values that results from applying an expression to each
  105. * document in a group of documents that share the same group by key.
  106. *
  107. * @param fieldName the field name
  108. * @param expression the expression
  109. * @param <TExpression> the expression type
  110. * @return the field
  111. * @mongodb.driver.manual reference/operator/aggregation/push/ $push
  112. */
  113. public static <TExpression> BsonField push(final String fieldName, final TExpression expression) {
  114. return accumulator("$push", fieldName, expression);
  115. }
  116. /**
  117. * Gets a field name for a $group operation representing all unique values that results from applying the given expression to each
  118. * document in a group of documents that share the same group by key.
  119. *
  120. * @param fieldName the field name
  121. * @param expression the expression
  122. * @param <TExpression> the expression type
  123. * @return the field
  124. * @mongodb.driver.manual reference/operator/aggregation/addToSet/ $addToSet
  125. */
  126. public static <TExpression> BsonField addToSet(final String fieldName, final TExpression expression) {
  127. return accumulator("$addToSet", fieldName, expression);
  128. }
  129. /**
  130. * Gets a field name for a $group operation representing the sample standard deviation of the values of the given expression
  131. * when applied to all members of the group.
  132. *
  133. * <p>Use if the values encompass the entire population of data you want to represent and do not wish to generalize about
  134. * a larger population.</p>
  135. *
  136. * @param fieldName the field name
  137. * @param expression the expression
  138. * @param <TExpression> the expression type
  139. * @return the field
  140. * @mongodb.driver.manual reference/operator/aggregation/stdDevPop/ $stdDevPop
  141. * @mongodb.server.release 3.2
  142. * @since 3.2
  143. */
  144. public static <TExpression> BsonField stdDevPop(final String fieldName, final TExpression expression) {
  145. return accumulator("$stdDevPop", fieldName, expression);
  146. }
  147. /**
  148. * Gets a field name for a $group operation representing the sample standard deviation of the values of the given expression
  149. * when applied to all members of the group.
  150. *
  151. * <p>Use if the values encompass a sample of a population of data from which to generalize about the population.</p>
  152. *
  153. * @param fieldName the field name
  154. * @param expression the expression
  155. * @param <TExpression> the expression type
  156. * @return the field
  157. * @mongodb.driver.manual reference/operator/aggregation/stdDevSamp/ $stdDevSamp
  158. * @mongodb.server.release 3.2
  159. * @since 3.2
  160. */
  161. public static <TExpression> BsonField stdDevSamp(final String fieldName, final TExpression expression) {
  162. return accumulator("$stdDevSamp", fieldName, expression);
  163. }
  164. private static <TExpression> BsonField accumulator(final String name, final String fieldName, final TExpression expression) {
  165. return new BsonField(fieldName, new SimpleExpression<TExpression>(name, expression));
  166. }
  167. private Accumulators() {
  168. }
  169. }