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/08/02 22:07:31 UTC

[groovy] branch master updated: GROOVY-11145: add `join()` extension

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


The following commit(s) were added to refs/heads/master by this push:
     new 4e6238dd3c GROOVY-11145: add `join()` extension
4e6238dd3c is described below

commit 4e6238dd3c83aa83e95a8477f2a1b2f4a40ff5df
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Aug 2 16:29:57 2023 -0500

    GROOVY-11145: add `join()` extension
---
 build.bat                                          |   1 +
 .../groovy/runtime/ArrayGroovyMethods.java         | 120 ++++++++++++++++++++-
 .../groovy/runtime/DefaultGroovyMethods.java       |  29 ++++-
 .../org/codehaus/groovy/util/ArrayIterable.java    |   4 +-
 .../org/codehaus/groovy/util/ArrayIterator.java    |  17 ++-
 .../codehaus/groovy/util/BooleanArrayIterator.java |  16 ++-
 .../codehaus/groovy/util/ByteArrayIterator.java    |  16 ++-
 .../codehaus/groovy/util/CharArrayIterator.java    |  16 ++-
 .../codehaus/groovy/util/DoubleArrayIterable.java  |   4 +-
 .../codehaus/groovy/util/DoubleArrayIterator.java  |  16 ++-
 .../codehaus/groovy/util/FloatArrayIterator.java   |  16 ++-
 .../org/codehaus/groovy/util/IntArrayIterable.java |   4 +-
 .../org/codehaus/groovy/util/IntArrayIterator.java |  16 ++-
 .../codehaus/groovy/util/LongArrayIterable.java    |   4 +-
 .../codehaus/groovy/util/LongArrayIterator.java    |  16 ++-
 .../codehaus/groovy/util/ShortArrayIterator.java   |  16 ++-
 16 files changed, 214 insertions(+), 97 deletions(-)

diff --git a/build.bat b/build.bat
new file mode 100644
index 0000000000..1ff7ce2ea5
--- /dev/null
+++ b/build.bat
@@ -0,0 +1 @@
+.\gradlew --max-workers 1 --no-build-cache --no-daemon --no-scan %*
diff --git a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
index e1b8169568..7acf5f280e 100644
--- a/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java
@@ -4651,12 +4651,118 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 1.6.4
      */
     public static <T> Iterator<T> iterator(final T[] self) {
-        return Arrays.asList(self).iterator();
+        return new ArrayIterator<>(self);
     }
 
     //--------------------------------------------------------------------------
     // join
 
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of boolean
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(boolean[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of byte
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(byte[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of char
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(char[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of short
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(short[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of int
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(int[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of long
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(long[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of float
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(float[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * @param self an array of double
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(double[] self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the string representation of each item in this array.
+     *
+     * <pre class="groovyTestCase">
+     * def array = new Number[]{1,2L,3G}
+     * assert array.join() == "123"
+     * </pre>
+     *
+     * @param self an array of Object
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(Object[] self) {
+        return join(self, null);
+    }
+
+    //
+
     /**
      * Concatenates the string representation of each item in this array,
      * with the given String as a separator between each item.
@@ -4762,16 +4868,20 @@ public class ArrayGroovyMethods extends DefaultGroovyMethodsSupport {
     }
 
     /**
-     * Concatenates the <code>toString()</code> representation of each
-     * item in this array, with the given String as a separator between each
-     * item.
+     * Concatenates the string representation of each item in this array,
+     * with the given String as a separator between each item.
+     *
+     * <pre class="groovyTestCase">
+     * Serializable[] array = [1,2L,-3G]
+     * assert array.join("+") == "1+2+-3"
+     * </pre>
      *
      * @param self      an array of Object
      * @param separator a String separator
      * @return the joined String
      * @since 1.0
      */
-    public static <T> String join(T[] self, String separator) {
+    public static String join(Object[] self, String separator) {
         return DefaultGroovyMethods.join(new ArrayIterator<>(self), separator);
     }
 
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 477a2929c6..987ffb1823 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -8026,6 +8026,32 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     //--------------------------------------------------------------------------
     // join
 
+    /**
+     * Concatenates the <code>toString()</code> representation of each item from
+     * the Iterator. The iterator will be exhausted of elements.
+     *
+     * @param self an Iterator of objects
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(final Iterator<?> self) {
+        return join(self, null);
+    }
+
+    /**
+     * Concatenates the <code>toString()</code> representation of each item from
+     * the Iterable.
+     *
+     * <pre class="groovyTestCase">assert [1,2,3].join() == "123"</pre>
+     *
+     * @param self an Iterable of objects
+     * @return the joined String
+     * @since 5.0.0
+     */
+    public static String join(final Iterable<?> self) {
+        return join(self.iterator(), null);
+    }
+
     /**
      * Concatenates the <code>toString()</code> representation of each item from
      * the Iterator, with the given String as a separator between each item. The
@@ -8050,8 +8076,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
     /**
      * Concatenates the <code>toString()</code> representation of each item from
      * the Iterable, with the given String as a separator between each item.
-     * <p>
-     * <pre class="groovyTestCase">assert [1,2,3].join() == "123"</pre>
+     *
      * <pre class="groovyTestCase">assert [1,2,3].join(", ") == "1, 2, 3"</pre>
      *
      * @param self      an Iterable of objects
diff --git a/src/main/java/org/codehaus/groovy/util/ArrayIterable.java b/src/main/java/org/codehaus/groovy/util/ArrayIterable.java
index 8e1b2466fa..5f19eddc29 100644
--- a/src/main/java/org/codehaus/groovy/util/ArrayIterable.java
+++ b/src/main/java/org/codehaus/groovy/util/ArrayIterable.java
@@ -21,14 +21,14 @@ package org.codehaus.groovy.util;
 import java.util.Iterator;
 
 /**
- * Allow an array to be used where an Iterable is expected.
+ * Allows an array to be used where an Iterable is expected.
  *
  * @since 3.0.8
  */
 public class ArrayIterable<T> implements Iterable<T> {
     private final T[] array;
 
-    public ArrayIterable(T[] array) {
+    public ArrayIterable(final T[] array) {
         this.array = array;
     }
 
diff --git a/src/main/java/org/codehaus/groovy/util/ArrayIterator.java b/src/main/java/org/codehaus/groovy/util/ArrayIterator.java
index 4999d90302..a667267f6e 100644
--- a/src/main/java/org/codehaus/groovy/util/ArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/ArrayIterator.java
@@ -18,35 +18,32 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow an array to be used where an Iterator is expected.
+ * Allows an array to be used where an Iterator is expected.
  */
 public class ArrayIterator<T> implements Iterator<T> {
     private final T[] array;
-    private final int length;
     private int index;
 
-    public ArrayIterator(T[] array) {
+    public ArrayIterator(final T[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
-    @SuppressWarnings("unchecked")
     public T next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return (T) Array.get(array, index++);
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/BooleanArrayIterator.java b/src/main/java/org/codehaus/groovy/util/BooleanArrayIterator.java
index e4818f4992..338f909ace 100644
--- a/src/main/java/org/codehaus/groovy/util/BooleanArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/BooleanArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow a boolean array to be used where an Iterator is expected.
+ * Allows a boolean array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class BooleanArrayIterator implements Iterator<Boolean> {
     private final boolean[] array;
-    private final int length;
     private int index;
 
-    public BooleanArrayIterator(boolean[] array) {
+    public BooleanArrayIterator(final boolean[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Boolean next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/ByteArrayIterator.java b/src/main/java/org/codehaus/groovy/util/ByteArrayIterator.java
index f0c7ca24c6..624214aead 100644
--- a/src/main/java/org/codehaus/groovy/util/ByteArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/ByteArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow a byte array to be used where an Iterator is expected.
+ * Allows a byte array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class ByteArrayIterator implements Iterator<Byte> {
     private final byte[] array;
-    private final int length;
     private int index;
 
-    public ByteArrayIterator(byte[] array) {
+    public ByteArrayIterator(final byte[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Byte next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/CharArrayIterator.java b/src/main/java/org/codehaus/groovy/util/CharArrayIterator.java
index d1673a7538..6305f8e50b 100644
--- a/src/main/java/org/codehaus/groovy/util/CharArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/CharArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow a char array to be used where an Iterator is expected.
+ * Allows a char array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class CharArrayIterator implements Iterator<Character> {
     private final char[] array;
-    private final int length;
     private int index;
 
-    public CharArrayIterator(char[] array) {
+    public CharArrayIterator(final char[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Character next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/DoubleArrayIterable.java b/src/main/java/org/codehaus/groovy/util/DoubleArrayIterable.java
index b9f41d92f0..7248e358c1 100644
--- a/src/main/java/org/codehaus/groovy/util/DoubleArrayIterable.java
+++ b/src/main/java/org/codehaus/groovy/util/DoubleArrayIterable.java
@@ -21,14 +21,14 @@ package org.codehaus.groovy.util;
 import java.util.Iterator;
 
 /**
- * Allow a double array to be used where an Iterable is expected.
+ * Allows a double array to be used where an Iterable is expected.
  *
  * @since 3.0.8
  */
 public class DoubleArrayIterable implements Iterable<Double> {
     private final double[] array;
 
-    public DoubleArrayIterable(double[] array) {
+    public DoubleArrayIterable(final double[] array) {
         this.array = array;
     }
 
diff --git a/src/main/java/org/codehaus/groovy/util/DoubleArrayIterator.java b/src/main/java/org/codehaus/groovy/util/DoubleArrayIterator.java
index 78cd1766ba..d3089e5c62 100644
--- a/src/main/java/org/codehaus/groovy/util/DoubleArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/DoubleArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow a double array to be used where an Iterator is expected.
+ * Allows a double array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class DoubleArrayIterator implements Iterator<Double> {
     private final double[] array;
-    private final int length;
     private int index;
 
-    public DoubleArrayIterator(double[] array) {
+    public DoubleArrayIterator(final double[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Double next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/FloatArrayIterator.java b/src/main/java/org/codehaus/groovy/util/FloatArrayIterator.java
index e13d73b480..4a8f2df6ed 100644
--- a/src/main/java/org/codehaus/groovy/util/FloatArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/FloatArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow a float array to be used where an Iterator is expected.
+ * Allows a float array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class FloatArrayIterator implements Iterator<Float> {
     private final float[] array;
-    private final int length;
     private int index;
 
-    public FloatArrayIterator(float[] array) {
+    public FloatArrayIterator(final float[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Float next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/IntArrayIterable.java b/src/main/java/org/codehaus/groovy/util/IntArrayIterable.java
index 292f25d274..91545ce076 100644
--- a/src/main/java/org/codehaus/groovy/util/IntArrayIterable.java
+++ b/src/main/java/org/codehaus/groovy/util/IntArrayIterable.java
@@ -21,14 +21,14 @@ package org.codehaus.groovy.util;
 import java.util.Iterator;
 
 /**
- * Allow an int array to be used where an Iterable is expected.
+ * Allows an int array to be used where an Iterable is expected.
  *
  * @since 3.0.8
  */
 public class IntArrayIterable implements Iterable<Integer> {
     private final int[] array;
 
-    public IntArrayIterable(int[] array) {
+    public IntArrayIterable(final int[] array) {
         this.array = array;
     }
 
diff --git a/src/main/java/org/codehaus/groovy/util/IntArrayIterator.java b/src/main/java/org/codehaus/groovy/util/IntArrayIterator.java
index 652c8bde3d..f366293d5d 100644
--- a/src/main/java/org/codehaus/groovy/util/IntArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/IntArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow an int array to be used where an Iterator is expected.
+ * Allows an int array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class IntArrayIterator implements Iterator<Integer> {
     private final int[] array;
-    private final int length;
     private int index;
 
-    public IntArrayIterator(int[] array) {
+    public IntArrayIterator(final int[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Integer next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/LongArrayIterable.java b/src/main/java/org/codehaus/groovy/util/LongArrayIterable.java
index 12c8a3e39d..339560b543 100644
--- a/src/main/java/org/codehaus/groovy/util/LongArrayIterable.java
+++ b/src/main/java/org/codehaus/groovy/util/LongArrayIterable.java
@@ -21,14 +21,14 @@ package org.codehaus.groovy.util;
 import java.util.Iterator;
 
 /**
- * Allow a long array to be used where an Iterable is expected.
+ * Allows a long array to be used where an Iterable is expected.
  *
  * @since 3.0.8
  */
 public class LongArrayIterable implements Iterable<Long> {
     private final long[] array;
 
-    public LongArrayIterable(long[] array) {
+    public LongArrayIterable(final long[] array) {
         this.array = array;
     }
 
diff --git a/src/main/java/org/codehaus/groovy/util/LongArrayIterator.java b/src/main/java/org/codehaus/groovy/util/LongArrayIterator.java
index 638fc25fa1..103af3a69d 100644
--- a/src/main/java/org/codehaus/groovy/util/LongArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/LongArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow a long array to be used where an Iterator is expected.
+ * Allows a long array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class LongArrayIterator implements Iterator<Long> {
     private final long[] array;
-    private final int length;
     private int index;
 
-    public LongArrayIterator(long[] array) {
+    public LongArrayIterator(final long[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Long next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override
diff --git a/src/main/java/org/codehaus/groovy/util/ShortArrayIterator.java b/src/main/java/org/codehaus/groovy/util/ShortArrayIterator.java
index 542a2b3278..afaeb3d3c8 100644
--- a/src/main/java/org/codehaus/groovy/util/ShortArrayIterator.java
+++ b/src/main/java/org/codehaus/groovy/util/ShortArrayIterator.java
@@ -18,36 +18,34 @@
  */
 package org.codehaus.groovy.util;
 
-import java.lang.reflect.Array;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 /**
- * Allow a short array to be used where an Iterator is expected.
+ * Allows a short array to be used where an Iterator is expected.
  *
  * @since 3.0.8
  */
 public class ShortArrayIterator implements Iterator<Short> {
     private final short[] array;
-    private final int length;
     private int index;
 
-    public ShortArrayIterator(short[] array) {
+    public ShortArrayIterator(final short[] array) {
         this.array = array;
-        length = Array.getLength(array);
     }
 
     @Override
     public boolean hasNext() {
-        return index < length;
+        return array.length > index;
     }
 
     @Override
     public Short next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+        try {
+            return array[index++];
+        } catch (IndexOutOfBoundsException e) {
+            throw new NoSuchElementException(e.getMessage());
         }
-        return array[index++];
     }
 
     @Override