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 2023/01/13 05:18:30 UTC

[groovy] branch master updated: add missing AGM min/max variants

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 0961a69817 add missing AGM min/max variants
0961a69817 is described below

commit 0961a6981700e7a366d400819b340c8f8920309e
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Jan 13 15:18:19 2023 +1000

    add missing AGM min/max variants
---
 .../groovy/runtime/ArrayGroovyMethods.java         | 88 ++++++++++++++++++++++
 .../groovy/runtime/DefaultGroovyMethods.java       |  2 +
 2 files changed, 90 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
index 09611af2b9..32de74c0fb 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
@@ -45,6 +45,7 @@ import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -3659,6 +3660,35 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
         return answer;
     }
 
+    /**
+     * Selects the minimum value found from the int array
+     * using the closure to determine the correct ordering.
+     * <p>
+     * <pre class="groovyTestCase">
+     * int[] nums = [30, 45, 60, 90]
+     * assert 30 == nums.min{ Math.sin(Math.toRadians(it)) }
+     * assert 90 == nums.min{ Math.cos(Math.toRadians(it)) } // cos(90) == 0
+     * </pre>
+     * <p>
+     * If the closure has two parameters
+     * it is used like a traditional Comparator. I.e., it should compare
+     * its two parameters for order, returning a negative integer,
+     * zero, or a positive integer when the first parameter is less than,
+     * equal to, or greater than the second respectively. Otherwise,
+     * the Closure is assumed to take a single parameter and return a
+     * Comparable (typically an Integer) which is then used for
+     * further comparison.
+     *
+     * @param self    an int array
+     * @param closure a Closure used to determine the correct ordering
+     * @return the minimum value
+     * @see DefaultGroovyMethods#min(Iterator, groovy.lang.Closure)
+     * @since 5.0.0
+     */
+    public static int min(int[] self, @ClosureParams(FirstParam.Component.class) Closure<?> closure) {
+        return DefaultGroovyMethods.min(new IntArrayIterable(self), closure);
+    }
+
     /**
      * Adds min() method to long arrays.
      * <p/>
@@ -3683,6 +3713,35 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
         return answer;
     }
 
+    /**
+     * Selects the minimum value found from the long array
+     * using the closure to determine the correct ordering.
+     * <p>
+     * <pre class="groovyTestCase">
+     * long[] nums = [-20L, 10L, 30L]
+     * assert -20L == nums.min{ a, b {@code ->} a {@code <=>} b }
+     * assert 10L == nums.min{ it.abs() }
+     * </pre>
+     * <p>
+     * If the closure has two parameters
+     * it is used like a traditional Comparator. I.e., it should compare
+     * its two parameters for order, returning a negative integer,
+     * zero, or a positive integer when the first parameter is less than,
+     * equal to, or greater than the second respectively. Otherwise,
+     * the Closure is assumed to take a single parameter and return a
+     * Comparable (typically an Integer) which is then used for
+     * further comparison.
+     *
+     * @param self    a long array
+     * @param closure a Closure used to determine the correct ordering
+     * @return the minimum value
+     * @see DefaultGroovyMethods#min(Iterator, groovy.lang.Closure)
+     * @since 5.0.0
+     */
+    public static long min(long[] self, @ClosureParams(FirstParam.Component.class) Closure<?> closure) {
+        return DefaultGroovyMethods.min(new LongArrayIterator(self), closure);
+    }
+
     /**
      * Adds min() method to double arrays.
      * <p/>
@@ -3707,6 +3766,35 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
         return answer;
     }
 
+    /**
+     * Selects the minimum value found from the double array
+     * using the closure to determine the correct ordering.
+     * <p>
+     * <pre class="groovyTestCase">
+     * double[] nums = [-20.0d, 10.0d, 30.0d]
+     * assert -20.0d == nums.min{ a, b {@code ->} a {@code <=>} b }
+     * assert 10.0d == nums.min{ it.abs() }
+     * </pre>
+     * <p>
+     * If the closure has two parameters
+     * it is used like a traditional Comparator. I.e., it should compare
+     * its two parameters for order, returning a negative integer,
+     * zero, or a positive integer when the first parameter is less than,
+     * equal to, or greater than the second respectively. Otherwise,
+     * the Closure is assumed to take a single parameter and return a
+     * Comparable (typically an Integer) which is then used for
+     * further comparison.
+     *
+     * @param self    a double array
+     * @param closure a Closure used to determine the correct ordering
+     * @return the minimum value
+     * @see DefaultGroovyMethods#min(Iterator, groovy.lang.Closure)
+     * @since 5.0.0
+     */
+    public static double min(double[] self, @ClosureParams(FirstParam.Component.class) Closure<?> closure) {
+        return DefaultGroovyMethods.min(new DoubleArrayIterator(self), closure);
+    }
+
     //-------------------------------------------------------------------------
     // minus
     //-------------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 60be07178f..5eaec28b21 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -7196,6 +7196,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.5.5
      */
     public static <T> T min(Iterator<T> self, Comparator<? super T> comparator) {
+        Objects.requireNonNull(self);
         T answer = null;
         boolean first = true;
         while (self.hasNext()) {
@@ -7360,6 +7361,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         if (params != 1) {
             return min(self, new ClosureComparator<>(closure));
         }
+        Objects.requireNonNull(self);
         boolean first = true;
         T answer = null;
         Object answerValue = null;