-
Enhancement
-
Resolution: Unresolved
-
P4
-
None
-
6
-
Cause Known
A DESCRIPTION OF THE REQUEST :
I propose to add method for joining arrays to java.util.Arrays class.
JUSTIFICATION :
copyOf methods added in mustang could be not sufficient. To join arrays (of course arrays of the compatible types) using this method one have to determine desired array length (sum of subarray's length), create it (possibly utilizing Arrays.copyOf method, to get array of the same type and not use "(T[]) Array.newInstance(type.getComponentType(), newLength)" idiom explicitly). This way one have copy of the first array, and nulls on rest of indexes which should be filed with content of next arrays. One method to do all this things could be usefull.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The similar methods can be provided for arrays of primitive types.
---------- BEGIN SOURCE ----------
public static <T> T[] join(final T[] ... arrays) {
Validate.notNull(arrays, "arrays cannot be null");
Validate.noNullElements(arrays, "arrays contains null elements");
final Class type = arrays.getClass().getComponentType().getComponentType();
int newLength = 0;
for (final T[] array : arrays) {
newLength += array.length;
}
final T[] result = (T[]) Array.newInstance(type, newLength);
int offset = 0;
for (final T[] array : arrays) {
final int length = array.length;
System.arraycopy(array, 0, result, offset, length);
offset += length;
}
return result;
}
---- junit ------ ArrayUtils should be replaced with Arrays
public final class ArrayUtilsTest {
/**
* Call without any arguments.
*/
@Test public void joinEmptyArguments() {
final Object[] result = ArrayUtils.join();
assertNotNull(result);
assertEquals("wrong length", 0, result.length);
assertEquals("wrong component type", Object.class, result.getClass().getComponentType());
}
/**
* Call with the <code>null</code> argument.
*/
@Test(expected=IllegalArgumentException.class)
public void joinOneNull() {
ArrayUtils.join(null);
}
/**
* Call with two <code>null</code> arguments.
*/
@Test(expected=IllegalArgumentException.class)
public void joinTwoNulls() {
ArrayUtils.join(null, null);
}
/**
* Call with three <code>null</code> argument.
*/
@Test(expected=IllegalArgumentException.class)
public void joinThreeNulls() {
ArrayUtils.join(null, null, null);
}
/**
* Various test array join tests.
*/
@Test public void joinTestArrays() {
assertEquals(new Object[0], ArrayUtils.join());
assertEquals(new Object[0], ArrayUtils.join(new Object[0]));
assertEquals(new Object[0], ArrayUtils.join(new Object[0], new Object[0]));
assertEquals(new Object[] { null }, ArrayUtils.join(new Object[] { null }));
assertEquals(new Object[] { null, null }, ArrayUtils.join(new Object[] { null, null }));
assertEquals(new Object[] { null, null }, ArrayUtils.join(new Object[] { null }, new Object[] { null }));
assertEquals(new Object[] { null, "foo" }, ArrayUtils.join(new Object[] { null, "foo" }));
assertEquals(new Object[] { "bar", null }, ArrayUtils.join(new Object[] { "bar", null }));
assertEquals(new Object[] { "bar", new Integer(1) }, ArrayUtils.join(new Object[] { "bar", new Integer(1) }));
assertEquals(new String[0], ArrayUtils.join(new String[0]));
assertEquals(new String[0], ArrayUtils.join(new String[0], new String[0]));
assertEquals(new String[] { null }, ArrayUtils.join(new String[] { null }));
assertEquals(new String[] { "the", "quick", "brown", "fox" }, ArrayUtils.join(new String[] { "the" }, new String[] { "quick", "brown", "fox" }) );
assertEquals(new String[0], ArrayUtils.join(new String[0], new Integer[0]));
assertEquals(new String[] { "foo" }, ArrayUtils.join(new String[] { "foo" }, new Integer[0]));
assertEquals(new Integer[] { new Integer(1) }, ArrayUtils.join(new String[0], new Integer[] { new Integer(1) }));
final String[][] input1 = { {}, { "1" }, { "1", "2" } };
final String[][] input2 = { { "1", "2", "3"}, { "1", "2", "3", "4" } };
final String[][] expected = { {}, { "1" }, { "1", "2" }, { "1", "2", "3"}, { "1", "2", "3", "4" } };
final String[][] result = ArrayUtils.join(input1, input2);
assertEquals("array lengths differed", result.length, expected.length);
for (int i = 0; i < result.length; i++) {
assertEquals(expected[i], result[i]);
}
}
}
---------- END SOURCE ----------
I propose to add method for joining arrays to java.util.Arrays class.
JUSTIFICATION :
copyOf methods added in mustang could be not sufficient. To join arrays (of course arrays of the compatible types) using this method one have to determine desired array length (sum of subarray's length), create it (possibly utilizing Arrays.copyOf method, to get array of the same type and not use "(T[]) Array.newInstance(type.getComponentType(), newLength)" idiom explicitly). This way one have copy of the first array, and nulls on rest of indexes which should be filed with content of next arrays. One method to do all this things could be usefull.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The similar methods can be provided for arrays of primitive types.
---------- BEGIN SOURCE ----------
public static <T> T[] join(final T[] ... arrays) {
Validate.notNull(arrays, "arrays cannot be null");
Validate.noNullElements(arrays, "arrays contains null elements");
final Class type = arrays.getClass().getComponentType().getComponentType();
int newLength = 0;
for (final T[] array : arrays) {
newLength += array.length;
}
final T[] result = (T[]) Array.newInstance(type, newLength);
int offset = 0;
for (final T[] array : arrays) {
final int length = array.length;
System.arraycopy(array, 0, result, offset, length);
offset += length;
}
return result;
}
---- junit ------ ArrayUtils should be replaced with Arrays
public final class ArrayUtilsTest {
/**
* Call without any arguments.
*/
@Test public void joinEmptyArguments() {
final Object[] result = ArrayUtils.join();
assertNotNull(result);
assertEquals("wrong length", 0, result.length);
assertEquals("wrong component type", Object.class, result.getClass().getComponentType());
}
/**
* Call with the <code>null</code> argument.
*/
@Test(expected=IllegalArgumentException.class)
public void joinOneNull() {
ArrayUtils.join(null);
}
/**
* Call with two <code>null</code> arguments.
*/
@Test(expected=IllegalArgumentException.class)
public void joinTwoNulls() {
ArrayUtils.join(null, null);
}
/**
* Call with three <code>null</code> argument.
*/
@Test(expected=IllegalArgumentException.class)
public void joinThreeNulls() {
ArrayUtils.join(null, null, null);
}
/**
* Various test array join tests.
*/
@Test public void joinTestArrays() {
assertEquals(new Object[0], ArrayUtils.join());
assertEquals(new Object[0], ArrayUtils.join(new Object[0]));
assertEquals(new Object[0], ArrayUtils.join(new Object[0], new Object[0]));
assertEquals(new Object[] { null }, ArrayUtils.join(new Object[] { null }));
assertEquals(new Object[] { null, null }, ArrayUtils.join(new Object[] { null, null }));
assertEquals(new Object[] { null, null }, ArrayUtils.join(new Object[] { null }, new Object[] { null }));
assertEquals(new Object[] { null, "foo" }, ArrayUtils.join(new Object[] { null, "foo" }));
assertEquals(new Object[] { "bar", null }, ArrayUtils.join(new Object[] { "bar", null }));
assertEquals(new Object[] { "bar", new Integer(1) }, ArrayUtils.join(new Object[] { "bar", new Integer(1) }));
assertEquals(new String[0], ArrayUtils.join(new String[0]));
assertEquals(new String[0], ArrayUtils.join(new String[0], new String[0]));
assertEquals(new String[] { null }, ArrayUtils.join(new String[] { null }));
assertEquals(new String[] { "the", "quick", "brown", "fox" }, ArrayUtils.join(new String[] { "the" }, new String[] { "quick", "brown", "fox" }) );
assertEquals(new String[0], ArrayUtils.join(new String[0], new Integer[0]));
assertEquals(new String[] { "foo" }, ArrayUtils.join(new String[] { "foo" }, new Integer[0]));
assertEquals(new Integer[] { new Integer(1) }, ArrayUtils.join(new String[0], new Integer[] { new Integer(1) }));
final String[][] input1 = { {}, { "1" }, { "1", "2" } };
final String[][] input2 = { { "1", "2", "3"}, { "1", "2", "3", "4" } };
final String[][] expected = { {}, { "1" }, { "1", "2" }, { "1", "2", "3"}, { "1", "2", "3", "4" } };
final String[][] result = ArrayUtils.join(input1, input2);
assertEquals("array lengths differed", result.length, expected.length);
for (int i = 0; i < result.length; i++) {
assertEquals(expected[i], result[i]);
}
}
}
---------- END SOURCE ----------