This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new b1baf631fd GROOVY-11702: Tuple changes related to its backing array
b1baf631fd is described below
commit b1baf631fde703a55fec3863b3acbdf8478321e3
Author: Choosechee <[email protected]>
AuthorDate: Wed Jun 18 13:08:50 2025 -0500
GROOVY-11702: Tuple changes related to its backing array
---
src/main/java/groovy/lang/Tuple.java | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/src/main/java/groovy/lang/Tuple.java
b/src/main/java/groovy/lang/Tuple.java
index da8f31ea1a..ffa490cfb3 100644
--- a/src/main/java/groovy/lang/Tuple.java
+++ b/src/main/java/groovy/lang/Tuple.java
@@ -22,13 +22,15 @@ import
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import java.io.Serializable;
import java.util.AbstractList;
+import java.util.Arrays;
import java.util.List;
import java.util.Objects;
+import java.util.RandomAccess;
/**
* Represents a list of Objects.
*/
-public class Tuple<E> extends AbstractList<E> implements Serializable,
Cloneable, Comparable<Tuple<E>> {
+public class Tuple<E> extends AbstractList<E> implements Serializable,
Cloneable, Comparable<Tuple<E>>, RandomAccess {
private static final long serialVersionUID = -6707770506387821031L;
private final E[] contents;
@@ -52,9 +54,25 @@ public class Tuple<E> extends AbstractList<E> implements
Serializable, Cloneable
return contents.length;
}
+ @SuppressWarnings("unchecked")
@Override
public E[] toArray() {
- return contents;
+ int size = size();
+ E[] copy = (E[]) new Object[size];
+ System.arraycopy(contents, 0, copy, 0, size);
+ return copy;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public <T> T[] toArray(T[] a) {
+ int size = size();
+ if (a.length < size)
+ a = (T[]) new Object[size];
+
+ System.arraycopy(contents, 0, a, 0, size);
+ Arrays.fill(a, size, a.length, null);
+ return a;
}
@SuppressWarnings("unchecked")