Permalink
Browse files

GROOVY-5402: Provide a DGM plus() method for arrays

  • Loading branch information...
1 parent 0a7d04a commit 7f883f105e647f1aeaf9f0d8827fdc85aba721df @paulk-asert paulk-asert committed Apr 10, 2012
Showing with 31 additions and 22 deletions.
  1. +28 −22 src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
  2. +3 −0 src/test/groovy/ArrayTest.groovy
View
50 src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -6900,50 +6900,56 @@ public static boolean asBoolean(Number number) {
/**
* Create an array as a union of two arrays.
- * <pre class="groovyTestCase">Integer[] a = [ 1, 2, 3 ]
- * Integer[] b = [ 4, 5, 6 ]
- * assert a + b == [1,2,3,4,5,6] as Integer[]</pre>
+ * <pre class="groovyTestCase">
+ * Integer[] a = [1, 2, 3]
+ * Integer[] b = [4, 5, 6]
+ * assert a + b == [1, 2, 3, 4, 5, 6] as Integer[]
+ * </pre>
*
* @param left the left Array
* @param right the right Array
* @return A new array containing right appended to left.
* @since 1.8.7
*/
@SuppressWarnings("unchecked")
- public static <T> T[] plus( T[] left, T[] right ) {
- return (T[])plus( toList( left), toList( right ) ).toArray() ;
+ public static <T> T[] plus(T[] left, T[] right) {
+ return (T[]) plus(toList(left), toList(right)).toArray();
}
/**
- * Append a value on to an array.
- * <pre class="groovyTestCase">Integer[] a = [ 1, 2, 3 ]
+ * Create an array containing elements from an original array plus an additional appended element.
+ * <pre class="groovyTestCase">
+ * Integer[] a = [1, 2, 3]
* Integer[] result = a + 4
- * assert result == [ 1, 2, 3, 4 ] as Integer[]</pre>
+ * assert result == [1, 2, 3, 4] as Integer[]
+ * </pre>
*
- * @param self the array
- * @param val the value to append
- * @return A new array containing self with val appended to it.
+ * @param left the array
+ * @param right the value to append
+ * @return A new array containing left with right appended to it.
* @since 1.8.7
*/
@SuppressWarnings("unchecked")
- public static <T> T[] plus( T[] left, T right ) {
- return (T[])plus( toList( left), right ).toArray() ;
+ public static <T> T[] plus(T[] left, T right) {
+ return (T[]) plus(toList(left), right).toArray();
}
/**
- * Append a Collection on to an array.
- * <pre class="groovyTestCase">Integer[] a = [ 1, 2, 3 ]
- * def additions = [ 7, 8 ]
- * assert a + additions == [1,2,3,7,8] as Integer[] </pre>
+ * Create an array containing elements from an original array plus those from a Collection.
+ * <pre class="groovyTestCase">
+ * Integer[] a = [1, 2, 3]
+ * def additions = [7, 8]
+ * assert a + additions == [1, 2, 3, 7, 8] as Integer[]
+ * </pre>
*
- * @param self the array
- * @param val A Collection to be appended
- * @return A new array containing self with val appended to it.
+ * @param left the array
+ * @param right A Collection to be appended
+ * @return A new array containing left with right appended to it.
* @since 1.8.7
*/
@SuppressWarnings("unchecked")
- public static <T> T[] plus( T[] left, Collection<T> right ) {
- return (T[])plus( toList( left), right ).toArray() ;
+ public static <T> T[] plus(T[] left, Collection<T> right) {
+ return (T[]) plus(toList(left), right).toArray();
}
/**
View
3 src/test/groovy/ArrayTest.groovy
@@ -418,6 +418,9 @@ class ArrayTest extends GroovyTestCase {
def result = a + b
assert result == [ 1, 2, 3, 3, 4, 5 ]
assert result.class.isArray()
+ // check the originals are untouched
+ assert a == [ 1, 2, 3 ]
+ assert b == [ 3, 4, 5 ]
result = a + 4
assert result == [ 1, 2, 3, 4 ]

0 comments on commit 7f883f1

Please sign in to comment.