You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2015/04/28 21:15:27 UTC
[2/7] [lang] swap and shift for arrays
swap and shift for arrays
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/a3995141
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/a3995141
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/a3995141
Branch: refs/heads/master
Commit: a3995141bf1b8a478e86fdbecc95f2b98f892647
Parents: 63d8a02
Author: beradrian <be...@yahoo.com>
Authored: Wed Mar 4 10:22:18 2015 +0200
Committer: beradrian <be...@yahoo.com>
Committed: Tue Apr 28 12:58:11 2015 +0300
----------------------------------------------------------------------
.../org/apache/commons/lang3/ArrayUtils.java | 1007 ++++++++++++++++++
.../apache/commons/lang3/ArrayUtilsTest.java | 723 +++++++++++++
2 files changed, 1730 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/a3995141/src/main/java/org/apache/commons/lang3/ArrayUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index 9d0acee..8f21726 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -1847,6 +1847,1013 @@ public class ArrayUtils {
}
}
+ // Swap
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>There is no special handling for multi-dimensional arrays.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps two elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element to swap
+ * @param offset1 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);
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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;
+ }
+ }
+
+ /**
+ * <p>Swaps a series of elements in the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @param array the array to swap, may be {@code null}
+ * @param offset1 the index of the first element in the series to swap
+ * @param offset1 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
+ //-----------------------------------------------------------------------
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>There is no special handling for multi-dimensional arrays.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>Shifts the order of the given array.</p>
+ *
+ * <p>This method does nothing for a {@code null} input array.</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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;
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Shifts the order of the given array in the given range.
+ * </p>
+ *
+ * <p>
+ * This method does nothing for a {@code null} input array.
+ * </p>
+ *
+ * @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
// ----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/a3995141/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
index 609b169..805375a 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
@@ -2118,7 +2118,730 @@ public class ArrayUtilsTest {
assertEquals(null, array);
}
+ //-----------------------------------------------------------------------
+ @Test
+ public void testSwapChar() {
+ char[] array = new char[] {1, 2, 3};
+ ArrayUtils.swap(array, 0, 2);
+ assertEquals(3, array[0]);
+ assertEquals(2, array[1]);
+ assertEquals(1, array[2]);
+ }
+
+ @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(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() {