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 09:36:41 UTC

[groovy] 01/02: add another example to Javadoc

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit da926ca9dfca17f50ab07a431e229868ef7b7291
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 50486af..079a126 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -6757,10 +6757,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) {
@@ -6771,7 +6774,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
@@ -6790,6 +6795,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