/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static <T> T[] subArray(T[] pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} or * if {@code pArray} is not an array. * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static Object subArray(Object pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Creates an array containing a subset of the original array. * If the {@code pLength} parameter is negative, it will be ignored. * If there are not {@code pLength} elements in the original array * after {@code pStart}, the {@code pLength} parameter will be * ignored. * If the sub array is same length as the original, the original array will * be returned. * * @param pArray the original array * @param pStart the start index of the original array * @param pLength the length of the new array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0 and {@code pLength} is either * negative, or greater or equal to {@code pArray.length}. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ @SuppressWarnings("unchecked") public static <T> T[] subArray(T[] pArray, int pStart, int pLength) { return (T[]) subArray((Object) pArray, pStart, pLength); }
public String getValueAsString() { if (valueCount() > 1) { if (valueCount() < 16) { return arrayToString(value); } else { String first = arrayToString(CollectionUtil.subArray(value, 0, 4)); String last = arrayToString(CollectionUtil.subArray(value, valueCount() - 4, 4)); return String.format("%s ... %s (%d)", first.substring(0, first.length() - 1), last.substring(1), valueCount()); } } if (value != null && value.getClass().isArray() && Array.getLength(value) == 1) { return String.valueOf(Array.get(value, 0)); } return String.valueOf(value); }
@Test public void testSubArrayObject() { String[] strings = CollectionUtil.subArray(new String[] {"foo", "bar", "baz", "xyzzy"}, 1, 2); assertArrayEquals(new String[] {"bar", "baz"}, strings); }
@Test public void testSubArrayNative() { int[] numbers = (int[]) CollectionUtil.subArray(new int[] {1, 2, 3, 4, 5}, 1, 3); assertArrayEquals(new int[] {2, 3, 4}, numbers); }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the origianl array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} or * if {@code pArray} is not an array. * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static Object subArray(Object pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static <T> T[] subArray(T[] pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static <T> T[] subArray(T[] pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} or * if {@code pArray} is not an array. * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static Object subArray(Object pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Creates an array containing a subset of the original array. * If the sub array is same length as the original * ({@code pStart == 0}), the original array will be returned. * * @param pArray the original array * @param pStart the start index of the original array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0. * * @throws IllegalArgumentException if {@code pArray} is {@code null} or * if {@code pArray} is not an array. * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ public static Object subArray(Object pArray, int pStart) { return subArray(pArray, pStart, -1); }
/** * Creates an array containing a subset of the original array. * If the {@code pLength} parameter is negative, it will be ignored. * If there are not {@code pLength} elements in the original array * after {@code pStart}, the {@code pLength} parameter will be * ignored. * If the sub array is same length as the original, the original array will * be returned. * * @param pArray the original array * @param pStart the start index of the original array * @param pLength the length of the new array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0 and {@code pLength} is either * negative, or greater or equal to {@code pArray.length}. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ @SuppressWarnings("unchecked") public static <T> T[] subArray(T[] pArray, int pStart, int pLength) { return (T[]) subArray((Object) pArray, pStart, pLength); }
/** * Creates an array containing a subset of the original array. * If the {@code pLength} parameter is negative, it will be ignored. * If there are not {@code pLength} elements in the original array * after {@code pStart}, the {@code pLength} parameter will be * ignored. * If the sub array is same length as the original, the original array will * be returned. * * @param pArray the original array * @param pStart the start index of the original array * @param pLength the length of the new array * @return a subset of the original array, or the original array itself, * if {@code pStart} is 0 and {@code pLength} is either * negative, or greater or equal to {@code pArray.length}. * * @throws IllegalArgumentException if {@code pArray} is {@code null} * @throws ArrayIndexOutOfBoundsException if {@code pStart} < 0 */ @SuppressWarnings("unchecked") public static <T> T[] subArray(T[] pArray, int pStart, int pLength) { return (T[]) subArray((Object) pArray, pStart, pLength); }
public String getValueAsString() { if (valueCount() > 1) { if (valueCount() < 16) { return arrayToString(value); } else { String first = arrayToString(CollectionUtil.subArray(value, 0, 4)); String last = arrayToString(CollectionUtil.subArray(value, valueCount() - 4, 4)); return String.format("%s ... %s (%d)", first.substring(0, first.length() - 1), last.substring(1), valueCount()); } } if (value != null && value.getClass().isArray() && Array.getLength(value) == 1) { return String.valueOf(Array.get(value, 0)); } return String.valueOf(value); }
public String getValueAsString() { if (valueCount() > 1) { if (valueCount() < 16) { return arrayToString(value); } else { String first = arrayToString(CollectionUtil.subArray(value, 0, 4)); String last = arrayToString(CollectionUtil.subArray(value, valueCount() - 4, 4)); return String.format("%s ... %s (%d)", first.substring(0, first.length() - 1), last.substring(1), valueCount()); } } if (value != null && value.getClass().isArray() && Array.getLength(value) == 1) { return String.valueOf(Array.get(value, 0)); } return String.valueOf(value); }
@Test public void testSubArrayNative() { int[] numbers = (int[]) CollectionUtil.subArray(new int[] {1, 2, 3, 4, 5}, 1, 3); assertArrayEquals(new int[] {2, 3, 4}, numbers); }
@Test public void testSubArrayObject() { String[] strings = CollectionUtil.subArray(new String[] {"foo", "bar", "baz", "xyzzy"}, 1, 2); assertArrayEquals(new String[] {"bar", "baz"}, strings); }
@Test public void testSubArrayObject() { String[] strings = CollectionUtil.subArray(new String[] {"foo", "bar", "baz", "xyzzy"}, 1, 2); assertArrayEquals(new String[] {"bar", "baz"}, strings); }
@Test public void testSubArrayNative() { int[] numbers = (int[]) CollectionUtil.subArray(new int[] {1, 2, 3, 4, 5}, 1, 3); assertArrayEquals(new int[] {2, 3, 4}, numbers); }