From be6bdde17268ec755f879384d2be4414abca1ccf Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Fri, 5 Dec 2003 23:37:18 +0000 Subject: [PATCH] Add subarray methods for primitive types Change subarray for Objects to return same type as input from Ashwin S git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137720 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/ArrayUtils.java | 323 ++++++++- .../apache/commons/lang/ArrayUtilsTest.java | 672 +++++++++++++++++- 2 files changed, 956 insertions(+), 39 deletions(-) diff --git a/src/java/org/apache/commons/lang/ArrayUtils.java b/src/java/org/apache/commons/lang/ArrayUtils.java index e8f94d284..269a0480b 100644 --- a/src/java/org/apache/commons/lang/ArrayUtils.java +++ b/src/java/org/apache/commons/lang/ArrayUtils.java @@ -53,6 +53,7 @@ */ package org.apache.commons.lang; +import java.lang.reflect.Array; import java.util.HashMap; import java.util.Map; @@ -62,8 +63,8 @@ import org.apache.commons.lang.builder.ToStringStyle; /** - *

Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays - * (like Integer[]).

+ *

Operations on arrays, primitive arrays (like int[]) and + * primitive wrapper arrays (like Integer[]).

* *

This class tries to handle null input gracefully. * An exception will not be thrown for a null @@ -80,7 +81,7 @@ * @author Gary Gregory * @author Ashwin S * @since 2.0 - * @version $Id: ArrayUtils.java,v 1.28 2003/11/30 13:36:08 scolebourne Exp $ + * @version $Id: ArrayUtils.java,v 1.29 2003/12/05 23:37:18 scolebourne Exp $ */ public class ArrayUtils { @@ -441,21 +442,64 @@ public static boolean[] clone(final boolean[] array) { /** *

Produces a new array containing the elements between * the start and end indices.

- * + * *

The start index is inclusive, the end index exclusive. - * Null array input produces null output. - * The result is always an Object[] instance

+ * Null array input produces null output.

+ * + *

The component type of the subarray is always the same as + * that of the input array. Thus, if the input is an array of type + * Date, the following usage is envisaged:

+ * + *
+     * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
+     * 
* * @param array the array - * @param startIndex the starting index. Undervalue (<0) + * @param startIndexInclusive the starting index. Undervalue (<0) * is promoted to 0, overvalue (>array.length) results * in an empty array. - * @param endIndex elements upto endIndex-1 are present in the + * @param endIndexExclusive elements up to endIndex-1 are present in the * returned subarray. Undervalue (< startIndex) produces * empty array, overvalue (>array.length) is demoted to * array length. */ - public static Object[] subArray(Object[] array, int startIndexInclusive, int endIndexExclusive) { + public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + Class type = array.getClass().getComponentType(); + if (newSize <= 0) { + return (Object[]) Array.newInstance(type, 0); + } + Object[] subarray = (Object[]) Array.newInstance(type, newSize); + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new long array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static long[] subarray(long[] array, int startIndexInclusive, int endIndexExclusive) { if (array == null) { return null; } @@ -467,11 +511,264 @@ public static Object[] subArray(Object[] array, int startIndexInclusive, int end } int newSize = endIndexExclusive - startIndexInclusive; if (newSize <= 0) { - return EMPTY_OBJECT_ARRAY; + return EMPTY_LONG_ARRAY; } - Object[] subArray = new Object[newSize]; - System.arraycopy(array, startIndexInclusive, subArray, 0, newSize); - return subArray; + + long[] subarray = new long[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new int array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static int[] subarray(int[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_INT_ARRAY; + } + + int[] subarray = new int[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new short array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static short[] subarray(short[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_SHORT_ARRAY; + } + + short[] subarray = new short[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new char array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static char[] subarray(char[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_CHAR_ARRAY; + } + + char[] subarray = new char[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new byte array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_BYTE_ARRAY; + } + + byte[] subarray = new byte[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new double array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static double[] subarray(double[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_DOUBLE_ARRAY; + } + + double[] subarray = new double[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new float array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static float[] subarray(float[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_FLOAT_ARRAY; + } + + float[] subarray = new float[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; + } + + /** + *

Produces a new boolean array containing the elements + * between the start and end indices.

+ * + *

The start index is inclusive, the end index exclusive. + * Null array input produces null output.

+ * + * @param array the array + * @param startIndexInclusive the starting index. Undervalue (<0) + * is promoted to 0, overvalue (>array.length) results + * in an empty array. + * @param endIndexExclusive elements up to endIndex-1 are present in the + * returned subarray. Undervalue (< startIndex) produces + * empty array, overvalue (>array.length) is demoted to + * array length. + */ + public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) { + if (array == null) { + return null; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive > array.length) { + endIndexExclusive = array.length; + } + int newSize = endIndexExclusive - startIndexInclusive; + if (newSize <= 0) { + return EMPTY_BOOLEAN_ARRAY; + } + + boolean[] subarray = new boolean[newSize]; + System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); + return subarray; } // Is same length diff --git a/src/test/org/apache/commons/lang/ArrayUtilsTest.java b/src/test/org/apache/commons/lang/ArrayUtilsTest.java index 216a77ee5..9ed87327d 100644 --- a/src/test/org/apache/commons/lang/ArrayUtilsTest.java +++ b/src/test/org/apache/commons/lang/ArrayUtilsTest.java @@ -56,6 +56,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import java.util.Arrays; +import java.util.Date; import java.util.Map; import junit.framework.Test; @@ -72,7 +73,7 @@ * @author Matthew Hawthorne * @author Tim O'Brien * @author Ashwin S - * @version $Id: ArrayUtilsTest.java,v 1.17 2003/11/30 13:36:08 scolebourne Exp $ + * @version $Id: ArrayUtilsTest.java,v 1.18 2003/12/05 23:37:18 scolebourne Exp $ */ public class ArrayUtilsTest extends TestCase { @@ -274,47 +275,666 @@ public void testCloneFloat() { //----------------------------------------------------------------------- - public void testSubArray() { - Object[] inarray = { "a", "b", "c", "d", "e", "f"}; + public void testSubarrayObject() { + Object[] nullArray = null; + Object[] objectArray = { "a", "b", "c", "d", "e", "f"}; assertEquals("0 start, mid end", "abcd", - StringUtils.join(ArrayUtils.subArray(inarray, 0, 4))); + StringUtils.join(ArrayUtils.subarray(objectArray, 0, 4))); assertEquals("0 start, length end", "abcdef", - StringUtils.join(ArrayUtils.subArray(inarray, 0, inarray.length))); + StringUtils.join(ArrayUtils.subarray(objectArray, 0, objectArray.length))); assertEquals("mid start, mid end", "bcd", - StringUtils.join(ArrayUtils.subArray(inarray, 1, 4))); + StringUtils.join(ArrayUtils.subarray(objectArray, 1, 4))); assertEquals("mid start, length end", "bcdef", - StringUtils.join(ArrayUtils.subArray(inarray, 1, inarray.length))); + StringUtils.join(ArrayUtils.subarray(objectArray, 1, objectArray.length))); - assertNull("null input", ArrayUtils.subArray(null, 0, 3)); + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); assertEquals("empty array", "", - StringUtils.join(ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2))); + StringUtils.join(ArrayUtils.subarray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2))); assertEquals("start > end", "", - StringUtils.join(ArrayUtils.subArray(inarray, 4, 2))); + StringUtils.join(ArrayUtils.subarray(objectArray, 4, 2))); assertEquals("start == end", "", - StringUtils.join(ArrayUtils.subArray(inarray, 3, 3))); + StringUtils.join(ArrayUtils.subarray(objectArray, 3, 3))); assertEquals("start undershoot, normal end", "abcd", - StringUtils.join(ArrayUtils.subArray(inarray, -2, 4))); + StringUtils.join(ArrayUtils.subarray(objectArray, -2, 4))); assertEquals("start overshoot, any end", "", - StringUtils.join(ArrayUtils.subArray(inarray, 33, 4))); + StringUtils.join(ArrayUtils.subarray(objectArray, 33, 4))); assertEquals("normal start, end overshoot", "cdef", - StringUtils.join(ArrayUtils.subArray(inarray, 2, 33))); + StringUtils.join(ArrayUtils.subarray(objectArray, 2, 33))); assertEquals("start undershoot, end overshoot", "abcdef", - StringUtils.join(ArrayUtils.subArray(inarray, -2, 12))); + StringUtils.join(ArrayUtils.subarray(objectArray, -2, 12))); - // object-level tests - assertSame("empty array, object test", ArrayUtils.EMPTY_OBJECT_ARRAY, - ArrayUtils.subArray(ArrayUtils.EMPTY_OBJECT_ARRAY, 1, 2)); - assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY, - ArrayUtils.subArray(inarray, 4, 1)); - assertSame("start > end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY, - ArrayUtils.subArray(inarray, 33, 1)); - assertSame("start == end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY, - ArrayUtils.subArray(inarray, 3, 3)); - assertSame("start overshoot, any end, object test", ArrayUtils.EMPTY_OBJECT_ARRAY, - ArrayUtils.subArray(inarray, 8733, 4)); + // array type tests + Date[] dateArray = { new java.sql.Date(new Date().getTime()), + new Date(), new Date(), new Date(), new Date() }; + + assertSame("Object type", Object.class, + ArrayUtils.subarray(objectArray, 2, 4).getClass().getComponentType()); + assertSame("java.util.Date type", java.util.Date.class, + ArrayUtils.subarray(dateArray, 1, 4).getClass().getComponentType()); + assertNotSame("java.sql.Date type", java.sql.Date.class, + ArrayUtils.subarray(dateArray, 1, 4).getClass().getComponentType()); + try { + Object dummy = (java.sql.Date[])ArrayUtils.subarray(dateArray, 1,3); + fail("Invalid downcast"); + } catch (ClassCastException e) {} } + public void testSubarrayLong() { + long[] nullArray = null; + long[] array = { 999910, 999911, 999912, 999913, 999914, 999915 }; + long[] leftSubarray = { 999910, 999911, 999912, 999913 }; + long[] midSubarray = { 999911, 999912, 999913, 999914 }; + long[] rightSubarray = { 999912, 999913, 999914, 999915 }; + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_LONG_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_LONG_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_LONG_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("long type", long.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + + public void testSubarrayInt() { + int[] nullArray = null; + int[] array = { 10, 11, 12, 13, 14, 15 }; + int[] leftSubarray = { 10, 11, 12, 13 }; + int[] midSubarray = { 11, 12, 13, 14 }; + int[] rightSubarray = { 12, 13, 14, 15 }; + + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_INT_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_INT_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_INT_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("int type", int.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + + public void testSubarrayShort() { + short[] nullArray = null; + short[] array = { 10, 11, 12, 13, 14, 15 }; + short[] leftSubarray = { 10, 11, 12, 13 }; + short[] midSubarray = { 11, 12, 13, 14 }; + short[] rightSubarray = { 12, 13, 14, 15 }; + + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_SHORT_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_SHORT_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_SHORT_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("short type", short.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + + public void testSubarrChar() { + char[] nullArray = null; + char[] array = { 'a', 'b', 'c', 'd', 'e', 'f' }; + char[] leftSubarray = { 'a', 'b', 'c', 'd', }; + char[] midSubarray = { 'b', 'c', 'd', 'e', }; + char[] rightSubarray = { 'c', 'd', 'e', 'f', }; + + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_CHAR_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_CHAR_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("char type", char.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + + public void testSubarrayByte() { + byte[] nullArray = null; + byte[] array = { 10, 11, 12, 13, 14, 15 }; + byte[] leftSubarray = { 10, 11, 12, 13 }; + byte[] midSubarray = { 11, 12, 13, 14 }; + byte[] rightSubarray = { 12, 13, 14, 15 }; + + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_BYTE_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_BYTE_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_BYTE_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("byte type", byte.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + + public void testSubarrayDouble() { + double[] nullArray = null; + double[] array = { 10.123, 11.234, 12.345, 13.456, 14.567, 15.678 }; + double[] leftSubarray = { 10.123, 11.234, 12.345, 13.456, }; + double[] midSubarray = { 11.234, 12.345, 13.456, 14.567, }; + double[] rightSubarray = { 12.345, 13.456, 14.567, 15.678 }; + + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_DOUBLE_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_DOUBLE_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_DOUBLE_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("double type", double.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + + public void testSubarrayFloat() { + float[] nullArray = null; + float[] array = { 10, 11, 12, 13, 14, 15 }; + float[] leftSubarray = { 10, 11, 12, 13 }; + float[] midSubarray = { 11, 12, 13, 14 }; + float[] rightSubarray = { 12, 13, 14, 15 }; + + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_FLOAT_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_FLOAT_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_FLOAT_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("float type", float.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + + public void testSubarrayBoolean() { + boolean[] nullArray = null; + boolean[] array = { true, true, false, true, false, true }; + boolean[] leftSubarray = { true, true, false, true }; + boolean[] midSubarray = { true, false, true, false }; + boolean[] rightSubarray = { false, true, false, true }; + + + assertTrue("0 start, mid end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, 0, 4))); + + assertTrue("0 start, length end", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, 0, array.length))); + + assertTrue("mid start, mid end", + ArrayUtils.isEquals(midSubarray, + ArrayUtils.subarray(array, 1, 5))); + + assertTrue("mid start, length end", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, array.length))); + + + assertNull("null input", ArrayUtils.subarray(nullArray, 0, 3)); + + assertEquals("empty array", ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_BOOLEAN_ARRAY, 1, 2)); + + assertEquals("start > end", ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(array, 4, 2)); + + assertEquals("start == end", ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertTrue("start undershoot, normal end", + ArrayUtils.isEquals(leftSubarray, + ArrayUtils.subarray(array, -2, 4))); + + assertEquals("start overshoot, any end", + ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(array, 33, 4)); + + assertTrue("normal start, end overshoot", + ArrayUtils.isEquals(rightSubarray, + ArrayUtils.subarray(array, 2, 33))); + + assertTrue("start undershoot, end overshoot", + ArrayUtils.isEquals(array, + ArrayUtils.subarray(array, -2, 12))); + + // empty-return tests + + assertSame("empty array, object test", + ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(ArrayUtils.EMPTY_BOOLEAN_ARRAY, 1, 2)); + + assertSame("start > end, object test", + ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(array, 4, 1)); + + assertSame("start == end, object test", + ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(array, 3, 3)); + + assertSame("start overshoot, any end, object test", + ArrayUtils.EMPTY_BOOLEAN_ARRAY, + ArrayUtils.subarray(array, 8733, 4)); + + // array type tests + + assertSame("boolean type", boolean.class, + ArrayUtils.subarray(array, 2, 4).getClass().getComponentType()); + + } + //----------------------------------------------------------------------- public void testSameLength() { Object[] nullArray = null;