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/10 11:28:22 UTC

[groovy] branch master updated: copy min/max DGM primitive array methods to ArrayGroovyMethods

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 b67b500784 copy min/max DGM primitive array methods to ArrayGroovyMethods
b67b500784 is described below

commit b67b50078442cc25c824e938a09d903b8b7ce97f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Jan 10 21:28:05 2023 +1000

    copy min/max DGM primitive array methods to ArrayGroovyMethods
---
 .../groovy/runtime/ArrayGroovyMethods.java         | 135 +++++++++++++++++++++
 .../groovy/runtime/DefaultGroovyMethods.java       | 132 ++------------------
 2 files changed, 147 insertions(+), 120 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
index 4543d05bc4..a452e20018 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
@@ -36,6 +36,7 @@ import org.codehaus.groovy.util.ShortArrayIterator;
 
 import java.math.BigDecimal;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * This class defines new groovy methods which appear on primitive arrays inside the Groovy environment.
@@ -1829,8 +1830,142 @@ public class ArrayGroovyMethods {
     // last
     //-------------------------------------------------------------------------
     // max
+
+    /**
+     * Adds max() method to int arrays.
+     *
+     * @param self an int array
+     * @return the maximum value
+     * @since 3.0.8
+     */
+    public static int max(int[] self) {
+        Objects.requireNonNull(self);
+        if (self.length == 0) {
+            throw new UnsupportedOperationException("Can't call max() on empty int[]");
+        }
+        int answer = self[0];
+        for (int i = 1; i < self.length; i++) {
+            int value = self[i];
+            if (value > answer) answer = value;
+        }
+        return answer;
+    }
+
+    /**
+     * Adds max() method to long arrays.
+     *
+     * @param self a long array
+     * @return the maximum value
+     * @since 3.0.8
+     */
+    public static long max(long[] self) {
+        Objects.requireNonNull(self);
+        if (self.length == 0) {
+            throw new UnsupportedOperationException("Can't call max() on empty long[]");
+        }
+        long answer = self[0];
+        for (int i = 1; i < self.length; i++) {
+            long value = self[i];
+            if (value > answer) answer = value;
+        }
+        return answer;
+    }
+
+    /**
+     * Adds max() method to double arrays.
+     * <p/>
+     * Example usage:
+     * <pre class="groovyTestCase">
+     * double[] nums = [1.1d, 2.2d, 3.3d]
+     * assert 3.3d == nums.max()
+     * </pre>
+     *
+     * @param self a double array
+     * @return the maximum value
+     * @since 3.0.8
+     */
+    public static double max(double[] self) {
+        Objects.requireNonNull(self);
+        if (self.length == 0) {
+            throw new UnsupportedOperationException("Can't call max() on empty double[]");
+        }
+        double answer = self[0];
+        for (int i = 1; i < self.length; i++) {
+            double value = self[i];
+            if (value > answer) answer = value;
+        }
+        return answer;
+    }
+
     //-------------------------------------------------------------------------
     // min
+
+    /**
+     * Adds min() method to int arrays.
+     * <p/>
+     * Example usage:
+     * <pre class="groovyTestCase">
+     * int[] nums = [10, 20, 30]
+     * assert 10 == nums.min()
+     * </pre>
+     *
+     * @param self an int array
+     * @return the minimum value
+     * @since 3.0.8
+     */
+    public static int min(int[] self) {
+        Objects.requireNonNull(self);
+        if (self.length == 0) {
+            throw new UnsupportedOperationException("Can't call min() on empty int[]");
+        }
+        int answer = self[0];
+        for (int i = 1; i < self.length; i++) {
+            int value = self[i];
+            if (value < answer) answer = value;
+        }
+        return answer;
+    }
+
+    /**
+     * Adds min() method to long arrays.
+     *
+     * @param self a long array
+     * @return the minimum value
+     * @since 3.0.8
+     */
+    public static long min(long[] self) {
+        Objects.requireNonNull(self);
+        if (self.length == 0) {
+            throw new UnsupportedOperationException("Can't call min() on empty long[]");
+        }
+        long answer = self[0];
+        for (int i = 1; i < self.length; i++) {
+            long value = self[i];
+            if (value < answer) answer = value;
+        }
+        return answer;
+    }
+
+    /**
+     * Adds min() method to double arrays.
+     *
+     * @param self a double array
+     * @return the minimum value
+     * @since 3.0.8
+     */
+    public static double min(double[] self) {
+        Objects.requireNonNull(self);
+        if (self.length == 0) {
+            throw new UnsupportedOperationException("Can't call min() on empty double[]");
+        }
+        double answer = self[0];
+        for (int i = 1; i < self.length; i++) {
+            double value = self[i];
+            if (value < answer) answer = value;
+        }
+        return answer;
+    }
+
     //-------------------------------------------------------------------------
     // minus
     //-------------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 67cf127d7a..e2f644acd2 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -7156,73 +7156,19 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         return min(new ArrayIterator<>(self));
     }
 
-    /**
-     * Adds min() method to int arrays.
-     * <p/>
-     * Example usage:
-     * <pre class="groovyTestCase">
-     * int[] nums = [10, 20, 30]
-     * assert 10 == nums.min()
-     * </pre>
-     *
-     * @param self an int array
-     * @return the minimum value
-     * @see #min(Object[])
-     * @since 3.0.8
-     */
+    @Deprecated
     public static int min(int[] self) {
-        Objects.requireNonNull(self);
-        if (self.length == 0) {
-            throw new UnsupportedOperationException("Can't call min() on empty int[]");
-        }
-        int answer = self[0];
-        for (int i = 1; i < self.length; i++) {
-            int value = self[i];
-            if (value < answer) answer = value;
-        }
-        return answer;
+        return ArrayGroovyMethods.max(self);
     }
 
-    /**
-     * Adds min() method to long arrays.
-     *
-     * @param self a long array
-     * @return the minimum value
-     * @see #min(Object[])
-     * @since 3.0.8
-     */
+    @Deprecated
     public static long min(long[] self) {
-        Objects.requireNonNull(self);
-        if (self.length == 0) {
-            throw new UnsupportedOperationException("Can't call min() on empty long[]");
-        }
-        long answer = self[0];
-        for (int i = 1; i < self.length; i++) {
-            long value = self[i];
-            if (value < answer) answer = value;
-        }
-        return answer;
+        return ArrayGroovyMethods.max(self);
     }
 
-    /**
-     * Adds min() method to double arrays.
-     *
-     * @param self a double array
-     * @return the minimum value
-     * @see #min(Object[])
-     * @since 3.0.8
-     */
+    @Deprecated
     public static double min(double[] self) {
-        Objects.requireNonNull(self);
-        if (self.length == 0) {
-            throw new UnsupportedOperationException("Can't call min() on empty double[]");
-        }
-        double answer = self[0];
-        for (int i = 1; i < self.length; i++) {
-            double value = self[i];
-            if (value < answer) answer = value;
-        }
-        return answer;
+        return ArrayGroovyMethods.max(self);
     }
 
     /**
@@ -7499,73 +7445,19 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         return max(new ArrayIterator<>(self));
     }
 
-    /**
-     * Adds max() method to int arrays.
-     *
-     * @param self an int array
-     * @return the maximum value
-     * @see #max(Object[])
-     * @since 3.0.8
-     */
+    @Deprecated
     public static int max(int[] self) {
-        Objects.requireNonNull(self);
-        if (self.length == 0) {
-            throw new UnsupportedOperationException("Can't call max() on empty int[]");
-        }
-        int answer = self[0];
-        for (int i = 1; i < self.length; i++) {
-            int value = self[i];
-            if (value > answer) answer = value;
-        }
-        return answer;
+        return ArrayGroovyMethods.max(self);
     }
 
-    /**
-     * Adds max() method to long arrays.
-     *
-     * @param self a long array
-     * @return the maximum value
-     * @see #max(Object[])
-     * @since 3.0.8
-     */
+    @Deprecated
     public static long max(long[] self) {
-        Objects.requireNonNull(self);
-        if (self.length == 0) {
-            throw new UnsupportedOperationException("Can't call max() on empty long[]");
-        }
-        long answer = self[0];
-        for (int i = 1; i < self.length; i++) {
-            long value = self[i];
-            if (value > answer) answer = value;
-        }
-        return answer;
+        return ArrayGroovyMethods.max(self);
     }
 
-    /**
-     * Adds max() method to double arrays.
-     * <p/>
-     * Example usage:
-     * <pre class="groovyTestCase">
-     * double[] nums = [1.1d, 2.2d, 3.3d]
-     * assert 3.3d == nums.max()
-     * </pre>
-     *
-     * @param self a double array
-     * @return the maximum value
-     * @see #max(Object[])
-     * @since 3.0.8
-     */
+    @Deprecated
     public static double max(double[] self) {
-        Objects.requireNonNull(self);
-        if (self.length == 0) {
-            throw new UnsupportedOperationException("Can't call max() on empty double[]");
-        }
-        double answer = self[0];
-        for (int i = 1; i < self.length; i++) {
-            double value = self[i];
-            if (value > answer) answer = value;
-        }
-        return answer;
+        return ArrayGroovyMethods.max(self);
     }
 
     /**