You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2023/11/23 16:24:55 UTC

(groovy) 02/02: GROOVY-11213: array `indexOf` and `lastIndexOf`

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

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

commit 63c4682474416f04f8637c53f506dca362773ffb
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Thu Nov 23 10:24:41 2023 -0600

    GROOVY-11213: array `indexOf` and `lastIndexOf`
---
 .../groovy/runtime/ArrayGroovyMethods.java         | 54 ++++++++++++++++++++
 .../groovy/runtime/DefaultGroovyMethods.java       | 58 +++++++++++-----------
 2 files changed, 83 insertions(+), 29 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
index 753702412a..54c2acb443 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
@@ -4318,6 +4318,33 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
         return self[0];
     }
 
+    //--------------------------------------------------------------------------
+    // indexOf
+
+    /**
+     * Returns the index of the first occurrence of the specified element in the
+     * array. That is, the lowest index {@code i} such that
+     * {@code Objects.equals(array[i], object)} or -1 if there is no such index.
+     *
+     * <pre class="groovyTestCase">
+     * String[] array = ['foo','bar','foo']
+     * assert array.indexOf('foo') ==  0
+     * assert array.indexOf('bar') ==  1
+     * assert array.indexOf('baz') == -1
+     * assert array.indexOf(12345) == -1
+     * </pre>
+     *
+     * @since 5.0.0
+     */
+    public static int indexOf(Object[] self, Object object) {
+        for (int i = 0; i < self.length; i += 1) {
+            if (Objects.equals(self[i], object)) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
     //--------------------------------------------------------------------------
     // indexed
 
@@ -5104,6 +5131,33 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
         return self[self.length - 1];
     }
 
+    //--------------------------------------------------------------------------
+    // lastIndexOf
+
+    /**
+     * Returns the index of the last occurrence of the specified element in the
+     * array. That is, the highest index {@code i} such that
+     * {@code Objects.equals(array[i], object)} or -1 if there is no such index.
+     *
+     * <pre class="groovyTestCase">
+     * String[] array = ['foo','bar','foo']
+     * assert array.lastIndexOf('foo') ==  2
+     * assert array.lastIndexOf('bar') ==  1
+     * assert array.lastIndexOf('baz') == -1
+     * assert array.lastIndexOf(12345) == -1
+     * </pre>
+     *
+     * @since 5.0.0
+     */
+    public static int lastIndexOf(Object[] self, Object object) {
+        for (int i = self.length - 1; i >= 0; i -= 1) {
+            if (Objects.equals(self[i], object)) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
     //--------------------------------------------------------------------------
     // max
 
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 6c07882d8f..2bf5658e85 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -4492,6 +4492,35 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         return true;
     }
 
+    //--------------------------------------------------------------------------
+    // equalsIgnoreZeroSign
+
+    /**
+     * Compares this object against the specified object returning the same result
+     * as {@link Float#equals(Object)} but returning true if this object
+     * and the specified object are both zero and negative zero respectively or vice versa.
+     *
+     * @since 3.0.8
+     */
+    public static boolean equalsIgnoreZeroSign(Float number, Object other) {
+        if (other instanceof Float && 0.0f == number && 0.0f == (Float) other)
+            return true;
+        return number.equals(other);
+    }
+
+    /**
+     * Compares this object against the specified object returning the same result
+     * as {@link Double#equals(Object)} but returning true if this object
+     * and the specified object are both zero and negative zero respectively or vice versa.
+     *
+     * @since 3.0.8
+     */
+    public static boolean equalsIgnoreZeroSign(Double number, Object other) {
+        if (other instanceof Double && 0.0d == number && 0.0d == (Double) other)
+            return true;
+        return number.equals(other);
+    }
+
     //--------------------------------------------------------------------------
     // every
 
@@ -4601,35 +4630,6 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         return true;
     }
 
-    //--------------------------------------------------------------------------
-    // equalsIgnoreZeroSign
-
-    /**
-     * Compares this object against the specified object returning the same result
-     * as {@link Float#equals(Object)} but returning true if this object
-     * and the specified object are both zero and negative zero respectively or vice versa.
-     *
-     * @since 3.0.8
-     */
-    public static boolean equalsIgnoreZeroSign(Float number, Object other) {
-        if (other instanceof Float && 0.0f == number && 0.0f == (Float) other)
-            return true;
-        return number.equals(other);
-    }
-
-    /**
-     * Compares this object against the specified object returning the same result
-     * as {@link Double#equals(Object)} but returning true if this object
-     * and the specified object are both zero and negative zero respectively or vice versa.
-     *
-     * @since 3.0.8
-     */
-    public static boolean equalsIgnoreZeroSign(Double number, Object other) {
-        if (other instanceof Double && 0.0d == number && 0.0d == (Double) other)
-            return true;
-        return number.equals(other);
-    }
-
     //--------------------------------------------------------------------------
     // find