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 2024/01/24 15:17:11 UTC
(groovy) branch GROOVY_3_0_X updated: GROOVY-11296: DGM: `shuffled` preserves input order
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
new 68110cc88c GROOVY-11296: DGM: `shuffled` preserves input order
68110cc88c is described below
commit 68110cc88cda6af26757c014f80c8be790f99b79
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Jan 24 09:08:35 2024 -0600
GROOVY-11296: DGM: `shuffled` preserves input order
4_0_X backport
---
.../groovy/runtime/DefaultGroovyMethods.java | 55 ++++++++++++----------
1 file changed, 30 insertions(+), 25 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index a82f1cb31d..84a229a678 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -12426,13 +12426,14 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
- * Creates a new list containing the elements of the specified list
- * but in a random order.
+ * Creates a new list containing the elements of the specified list but in a random order.
* <pre class="groovyTestCase">
- * def orig = ["a", 4, false]
- * def shuffled = orig.shuffled()
- * assert orig.size() == shuffled.size()
- * assert orig.every{ shuffled.contains(it) }
+ * def list = ["a", 4, false]
+ * def result = list.shuffled()
+ * assert list !== result
+ * assert list == ["a", 4, false]
+ * assert list.size() == result.size()
+ * assert list.every{ result.contains(it) }
* </pre>
*
* @param self a List
@@ -12446,14 +12447,16 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
- * Creates a new list containing the elements of the specified list but in a random
- * order using the specified random instance as the source of randomness.
+ * Creates a new list containing the elements of the specified list but in a random order
+ * using the specified random instance as the source of randomness.
* <pre class="groovyTestCase">
* def r = new Random()
- * def orig = ["a", 4, false]
- * def shuffled = orig.shuffled(r)
- * assert orig.size() == shuffled.size()
- * assert orig.every{ shuffled.contains(it) }
+ * def list = ["a", 4, false]
+ * def result = list.shuffled(r)
+ * assert list !== result
+ * assert list == ["a", 4, false]
+ * assert list.size() == result.size()
+ * assert list.every{ result.contains(it) }
* </pre>
*
* @param self a List
@@ -12517,10 +12520,12 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
/**
* Creates a new array containing the elements of the specified array but in a random order.
* <pre class="groovyTestCase">
- * Integer[] orig = [10, 5, 20]
- * def array = orig.shuffled()
- * assert orig.size() == array.size()
- * assert orig.every{ array.contains(it) }
+ * Integer[] array = [10, 5, 20]
+ * def result = array.shuffled()
+ * assert array !== result
+ * assert array.length == result.length
+ * assert array.every{ result.contains(it) }
+ * assert array == new Integer[] {10, 5, 20}
* </pre>
*
* @param self an array
@@ -12535,14 +12540,16 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
}
/**
- * Creates a new array containing the elements of the specified array but in a random
- * order using the specified random instance as the source of randomness.
+ * Creates a new array containing the elements of the specified array but in a random order
+ * using the specified random instance as the source of randomness.
* <pre class="groovyTestCase">
* def r = new Random()
- * Integer[] orig = [10, 5, 20]
- * def array = orig.shuffled(r)
- * assert orig.size() == array.size()
- * assert orig.every{ array.contains(it) }
+ * Integer[] array = [10, 5, 20]
+ * def result = array.shuffled(r)
+ * assert array !== result
+ * assert array.length == result.length
+ * assert array.every{ result.contains(it) }
+ * assert array == new Integer[] {10, 5, 20}
* </pre>
*
* @param self an array
@@ -12551,9 +12558,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
*/
public static <T> T[] shuffled(T[] self, Random rnd) {
T[] result = self.clone();
- List<T> items = Arrays.asList(self);
- Collections.shuffle(items, rnd);
- System.arraycopy(items.toArray(), 0, result, 0, items.size());
+ Collections.shuffle(Arrays.asList(result), rnd);
return result;
}