You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2020/12/20 07:13:06 UTC
[groovy] branch master updated: add another example to Javadoc
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new c36156b add another example to Javadoc
c36156b is described below
commit c36156b4656fd4449b9a23c82d1f99a97e832f5e
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Dec 20 17:12:43 2020 +1000
add another example to Javadoc
---
.../groovy/runtime/DefaultGroovyMethods.java | 30 ++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 0792622..78cc345 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -6383,10 +6383,13 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* Averages the items in an Iterable. This is equivalent to invoking the
* "plus" method on all items in the Iterable and then dividing by the
* total count using the "div" method for the resulting sum.
- * <pre class="groovyTestCase">assert 3 == [1, 2, 6].average()</pre>
+ * <pre class="groovyTestCase">
+ * assert 3 == [1, 2, 6].average()
+ * </pre>
*
* @param self Iterable of values to average
* @return The average of all of the items
+ * @see #average(Iterator)
* @since 3.0.0
*/
public static Object average(Iterable<?> self) {
@@ -6397,7 +6400,9 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* Averages the items in an array. This is equivalent to invoking the
* "plus" method on all items in the array and then dividing by the
* total count using the "div" method for the resulting sum.
- * <pre class="groovyTestCase">assert 3 == ([1, 2, 6] as Integer[]).average()</pre>
+ * <pre class="groovyTestCase">
+ * assert 3 == ([1, 2, 6] as Integer[]).average()
+ * </pre>
*
* @param self The array of values to average
* @return The average of all of the items
@@ -6416,6 +6421,27 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
* "plus" method on all items in the array and then dividing by the
* total count using the "div" method for the resulting sum.
* The iterator will become exhausted of elements after determining the average value.
+ * While most frequently used with aggregates of numbers,
+ * {@code average} will work with any class supporting {@code plus} and {@code div}, e.g.:
+ * <pre class="groovyTestCase">
+ * class Stars {
+ * int numStars = 0
+ * String toString() {
+ * '*' * numStars
+ * }
+ * Stars plus(Stars other) {
+ * new Stars(numStars: numStars + other.numStars)
+ * }
+ * Stars div(Number divisor) {
+ * int newSize = numStars.intdiv(divisor)
+ * new Stars(numStars: newSize)
+ * }
+ * }
+ *
+ * def stars = [new Stars(numStars: 1), new Stars(numStars: 3)]
+ * assert stars*.toString() == ['*', '***']
+ * assert stars.average().toString() == '**'
+ * </pre>
*
* @param self an Iterator for the values to average
* @return The average of all of the items