diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 3e25e0694..5918d808d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Add swap and shift operations for arrays to ArrayUtils TypeUtils.parameterizeWithOwner - wrong format descriptor for "invalid number of type parameters". MultilineRecursiveToStringStyle largely unusable due to being package-private. StringUtils.uncapitalize performance improvement diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 9d0aceecc..0afe0e406 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -1847,6 +1847,1175 @@ public class ArrayUtils { } } + // Swap + //----------------------------------------------------------------------- + /** + *

Swaps two elements in the given array.

+ * + *

There is no special handling for multi-dimensional arrays.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final Object[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final long[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final int[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final short[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final char[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final byte[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final double[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final float[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps two elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element to swap + * @param offset2 the index of the second element to swap + * @throws ArrayIndexOutOfBoundsException if one of the indices is out of range + */ + public static void swap(final boolean[] array, int offset1, int offset2) { + if (array == null) { + return; + } + swap(array, offset1, offset2, 1); + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final boolean[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + boolean aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + + public static void swap(final byte[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + byte aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final char[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + char aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final double[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + double aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final float[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + float aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final int[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + int aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final long[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + long aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final Object[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + Object aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + /** + *

Swaps a series of elements in the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + *

Examples: + *

+ *

+ * + * @param array the array to swap, may be {@code null} + * @param offset1 the index of the first element in the series to swap + * @param offset2 the index of the second element in the series to swap + * @param len the number of elements to swap starting with the given indices + * @throws ArrayIndexOutOfBoundsException if one of the indices (plus the length of the series to swap) is out of range + */ + public static void swap(final short[] array, int offset1, int offset2, int len) { + if (array == null) { + return; + } + for (int i = 0; i < len; i++) { + short aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } + } + + // Shift + //----------------------------------------------------------------------- + /** + *

Shifts the order of the given array.

+ * + *

There is no special handling for multi-dimensional arrays.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + * @param offset how many position to the right to shift the array, if negative it will be shiftd to the left. + */ + public static void shift(final Object[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final long[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final int[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final short[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final char[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final byte[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final double[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final float[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

Shifts the order of the given array.

+ * + *

This method does nothing for a {@code null} input array.

+ * + * @param array the array to shift, may be {@code null} + */ + public static void shift(final boolean[] array, int offset) { + if (array == null) { + return; + } + shift(array, 0, array.length, offset); + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + * @since 3.2 + */ + public static void shift(final boolean[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + * @since 3.2 + */ + public static void shift(final byte[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + * @since 3.2 + */ + public static void shift(final char[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + * @since 3.2 + */ + public static void shift(final double[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + * @since 3.2 + */ + public static void shift(final float[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + * @since 3.2 + */ + public static void shift(final int[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + */ + public static void shift(final long[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + */ + public static void shift(final Object[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + + /** + *

+ * Shifts the order of the given array in the given range. + *

+ * + *

+ * This method does nothing for a {@code null} input array. + *

+ * + * @param array + * the array to shift, may be {@code null} + * @param startIndexInclusive + * the starting index. Undervalue (<0) is promoted to 0, overvalue (>array.length) results in no + * change. + * @param endIndexExclusive + * elements up to endIndex-1 are shiftd in the array. Undervalue (< start index) results in no + * change. Overvalue (>array.length) is demoted to array length. + * @since 3.2 + */ + public static void shift(final short[] array, int startIndexInclusive, int endIndexExclusive, int offset) { + if (array == null) { + return; + } + if (startIndexInclusive >= array.length - 1 || endIndexExclusive <= 0) { + return; + } + if (startIndexInclusive < 0) { + startIndexInclusive = 0; + } + if (endIndexExclusive >= array.length) { + endIndexExclusive = array.length; + } + int n = endIndexExclusive - startIndexInclusive; + if (n <= 1) { + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } + } + } + // IndexOf search // ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 609b16957..be3e11960 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -2118,7 +2118,748 @@ public class ArrayUtilsTest { assertEquals(null, array); } + //----------------------------------------------------------------------- + @Test + public void testSwapChar() { + char[] array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2); + assertArrayEquals(new char[] {3, 2, 1}, array); + + array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 0); + assertArrayEquals(new char[] {1, 2, 3}, array); + + array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 1, 0); + assertArrayEquals(new char[] {2, 1, 3}, array); + } + + @Test + public void testSwapCharRange() { + char[] array = new char[] {1, 2, 3, 4}; + ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapCharOutOfRange() { + char[] array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 3); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapCharOutOfRangeLen() { + char[] array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2, 2); + } + + @Test + public void testSwapFloat() { + float[] array = new float[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2); + assertEquals(3, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(1, array[2], 0); + } + + @Test + public void testSwapFloatRange() { + float[] array = new float[] {1, 2, 3, 4}; + ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0], 0); + assertEquals(4, array[1], 0); + assertEquals(1, array[2], 0); + assertEquals(2, array[3], 0); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapFloatOutOfRange() { + float[] array = new float[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 3); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapFloatOutOfRangeLen() { + float[] array = new float[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2, 2); + } + + @Test + public void testSwapDouble() { + double[] array = new double[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2); + assertEquals(3, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(1, array[2], 0); + } + + @Test + public void testSwapDoubleRange() { + double[] array = new double[] {1, 2, 3, 4}; + ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0], 0); + assertEquals(4, array[1], 0); + assertEquals(1, array[2], 0); + assertEquals(2, array[3], 0); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapDoubleOutOfRange() { + double[] array = new double[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 3); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapDoubleOutOfRangeLen() { + double[] array = new double[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2, 2); + } + + @Test + public void testSwapInt() { + int[] array = new int[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2); + assertEquals(3, array[0]); + assertEquals(2, array[1]); + assertEquals(1, array[2]); + } + + @Test + public void testSwapIntRange() { + int[] array = new int[] {1, 2, 3, 4}; + ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + + @Test + public void testSwapIntExchangedOffsets() { + int[] array; + array = new int[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 1, 2); + assertArrayEquals(new int[] {2, 3, 1}, array); + + array = new int[] {1, 2, 3}; + ArrayUtils.swap(array, 1, 0, 2); + assertArrayEquals(new int[] {2, 3, 1}, array); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapIntOutOfRange() { + int[] array = new int[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 3); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapIntOutOfRangeLen() { + int[] array = new int[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2, 2); + } + + @Test + public void testSwapLong() { + long[] array = new long[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2); + assertEquals(3, array[0]); + assertEquals(2, array[1]); + assertEquals(1, array[2]); + } + + @Test + public void testSwapLongRange() { + long[] array = new long[] {1, 2, 3, 4}; + ArrayUtils.swap(array, 0, 2, 2); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapLongOutOfRange() { + long[] array = new long[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 3); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapLongOutOfRangeLen() { + long[] array = new long[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 2, 2); + } + + @Test + public void testSwapObject() { + String[] array = new String[] {"1", "2", "3"}; + ArrayUtils.swap(array, 0, 2); + assertEquals("3", array[0]); + assertEquals("2", array[1]); + assertEquals("1", array[2]); + } + + @Test + public void testSwapObjectRange() { + String[] array = new String[] {"1", "2", "3", "4"}; + ArrayUtils.swap(array, 0, 2, 2); + assertEquals("3", array[0]); + assertEquals("4", array[1]); + assertEquals("1", array[2]); + assertEquals("2", array[3]); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapObjectOutOfRange() { + String[] array = new String[] {"1", "2", "3"}; + ArrayUtils.swap(array, 0, 3); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapObjectOutOfRangeLen() { + String[] array = new String[] {"1", "2", "3"}; + ArrayUtils.swap(array, 0, 2, 2); + } + + //----------------------------------------------------------------------- + @Test + public void testShiftDouble() { + double[] array = new double[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1); + assertEquals(4, array[0], 0); + assertEquals(1, array[1], 0); + assertEquals(2, array[2], 0); + assertEquals(3, array[3], 0); + ArrayUtils.shift(array, -1); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + ArrayUtils.shift(array, 5); + assertEquals(4, array[0], 0); + assertEquals(1, array[1], 0); + assertEquals(2, array[2], 0); + assertEquals(3, array[3], 0); + ArrayUtils.shift(array, -3); + assertEquals(3, array[0], 0); + assertEquals(4, array[1], 0); + assertEquals(1, array[2], 0); + assertEquals(2, array[3], 0); + } + + @Test + public void testShiftRangeDouble() { + double[] array = new double[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals(1, array[0], 0); + assertEquals(3, array[1], 0); + assertEquals(2, array[2], 0); + assertEquals(4, array[3], 0); + assertEquals(5, array[4], 0); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(4, array[2], 0); + assertEquals(3, array[3], 0); + assertEquals(5, array[4], 0); + } + + @Test + public void testShiftRangeNoElemDouble() { + double[] array = new double[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + } + + @Test + public void testShiftAllDouble() { + double[] array = new double[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 4); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + ArrayUtils.shift(array, -4); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + } + + @Test + public void testShiftFloat() { + float[] array = new float[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1); + assertEquals(4, array[0], 0); + assertEquals(1, array[1], 0); + assertEquals(2, array[2], 0); + assertEquals(3, array[3], 0); + ArrayUtils.shift(array, -1); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + ArrayUtils.shift(array, 5); + assertEquals(4, array[0], 0); + assertEquals(1, array[1], 0); + assertEquals(2, array[2], 0); + assertEquals(3, array[3], 0); + ArrayUtils.shift(array, -3); + assertEquals(3, array[0], 0); + assertEquals(4, array[1], 0); + assertEquals(1, array[2], 0); + assertEquals(2, array[3], 0); + } + + @Test + public void testShiftRangeFloat() { + float[] array = new float[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals(1, array[0], 0); + assertEquals(3, array[1], 0); + assertEquals(2, array[2], 0); + assertEquals(4, array[3], 0); + assertEquals(5, array[4], 0); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(4, array[2], 0); + assertEquals(3, array[3], 0); + assertEquals(5, array[4], 0); + } + + @Test + public void testShiftRangeNoElemFloat() { + float[] array = new float[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + } + + @Test + public void testShiftAllFloat() { + float[] array = new float[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 4); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + ArrayUtils.shift(array, -4); + assertEquals(1, array[0], 0); + assertEquals(2, array[1], 0); + assertEquals(3, array[2], 0); + assertEquals(4, array[3], 0); + } + + @Test + public void testShiftShort() { + short[] array = new short[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, 5); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -3); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + + @Test + public void testShiftRangeShort() { + short[] array = new short[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals(1, array[0]); + assertEquals(3, array[1]); + assertEquals(2, array[2]); + assertEquals(4, array[3]); + assertEquals(5, array[4]); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(4, array[2]); + assertEquals(3, array[3]); + assertEquals(5, array[4]); + } + + @Test + public void testShiftRangeNoElemShort() { + short[] array = new short[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftAllShort() { + short[] array = new short[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, -4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftByte() { + byte[] array = new byte[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, 5); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -3); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + + @Test + public void testShiftRangeByte() { + byte[] array = new byte[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals(1, array[0]); + assertEquals(3, array[1]); + assertEquals(2, array[2]); + assertEquals(4, array[3]); + assertEquals(5, array[4]); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(4, array[2]); + assertEquals(3, array[3]); + assertEquals(5, array[4]); + } + + @Test + public void testShiftRangeNoElemByte() { + byte[] array = new byte[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftAllByte() { + byte[] array = new byte[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, -4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftChar() { + char[] array = new char[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, 5); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -3); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + + @Test + public void testShiftRangeChar() { + char[] array = new char[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals(1, array[0]); + assertEquals(3, array[1]); + assertEquals(2, array[2]); + assertEquals(4, array[3]); + assertEquals(5, array[4]); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(4, array[2]); + assertEquals(3, array[3]); + assertEquals(5, array[4]); + } + + @Test + public void testShiftRangeNoElemChar() { + char[] array = new char[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftAllChar() { + char[] array = new char[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, -4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftLong() { + long[] array = new long[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, 5); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -3); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + + @Test + public void testShiftRangeLong() { + long[] array = new long[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals(1, array[0]); + assertEquals(3, array[1]); + assertEquals(2, array[2]); + assertEquals(4, array[3]); + assertEquals(5, array[4]); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(4, array[2]); + assertEquals(3, array[3]); + assertEquals(5, array[4]); + } + + @Test + public void testShiftRangeNoElemLong() { + long[] array = new long[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftAllLong() { + long[] array = new long[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, -4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftInt() { + int[] array = new int[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, 5); + assertEquals(4, array[0]); + assertEquals(1, array[1]); + assertEquals(2, array[2]); + assertEquals(3, array[3]); + ArrayUtils.shift(array, -3); + assertEquals(3, array[0]); + assertEquals(4, array[1]); + assertEquals(1, array[2]); + assertEquals(2, array[3]); + } + + @Test + public void testShiftRangeInt() { + int[] array = new int[] {1, 2, 3, 4, 5}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals(1, array[0]); + assertEquals(3, array[1]); + assertEquals(2, array[2]); + assertEquals(4, array[3]); + assertEquals(5, array[4]); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(4, array[2]); + assertEquals(3, array[3]); + assertEquals(5, array[4]); + } + + @Test + public void testShiftRangeNoElemInt() { + int[] array = new int[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftAllInt() { + int[] array = new int[] {1, 2, 3, 4}; + ArrayUtils.shift(array, 4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + ArrayUtils.shift(array, -4); + assertEquals(1, array[0]); + assertEquals(2, array[1]); + assertEquals(3, array[2]); + assertEquals(4, array[3]); + } + + @Test + public void testShiftObject() { + String[] array = new String[] {"1", "2", "3", "4"}; + ArrayUtils.shift(array, 1); + assertEquals("4", array[0]); + assertEquals("1", array[1]); + assertEquals("2", array[2]); + assertEquals("3", array[3]); + ArrayUtils.shift(array, -1); + assertEquals("1", array[0]); + assertEquals("2", array[1]); + assertEquals("3", array[2]); + assertEquals("4", array[3]); + ArrayUtils.shift(array, 5); + assertEquals("4", array[0]); + assertEquals("1", array[1]); + assertEquals("2", array[2]); + assertEquals("3", array[3]); + ArrayUtils.shift(array, -3); + assertEquals("3", array[0]); + assertEquals("4", array[1]); + assertEquals("1", array[2]); + assertEquals("2", array[3]); + } + + @Test + public void testShiftRangeObject() { + String[] array = new String[] {"1", "2", "3", "4", "5"}; + ArrayUtils.shift(array, 1, 3, 1); + assertEquals("1", array[0]); + assertEquals("3", array[1]); + assertEquals("2", array[2]); + assertEquals("4", array[3]); + assertEquals("5", array[4]); + ArrayUtils.shift(array, 1, 4, 2); + assertEquals("1", array[0]); + assertEquals("2", array[1]); + assertEquals("4", array[2]); + assertEquals("3", array[3]); + assertEquals("5", array[4]); + } + + @Test + public void testShiftRangeNoElemObject() { + String[] array = new String[] {"1", "2", "3", "4"}; + ArrayUtils.shift(array, 1, 1, 1); + assertEquals("1", array[0]); + assertEquals("2", array[1]); + assertEquals("3", array[2]); + assertEquals("4", array[3]); + } + + @Test + public void testShiftAllObject() { + String[] array = new String[] {"1", "2", "3", "4"}; + ArrayUtils.shift(array, 4); + assertEquals("1", array[0]); + assertEquals("2", array[1]); + assertEquals("3", array[2]); + assertEquals("4", array[3]); + ArrayUtils.shift(array, -4); + assertEquals("1", array[0]); + assertEquals("2", array[1]); + assertEquals("3", array[2]); + assertEquals("4", array[3]); + } + //----------------------------------------------------------------------- @Test public void testIndexOf() {