consistent treatment for negative indices for swap method

This commit is contained in:
beradrian 2015-05-07 09:50:41 +03:00
parent f431270c59
commit 5b7608d154
2 changed files with 311 additions and 194 deletions

View File

@ -1854,24 +1854,25 @@ public static void reverse(final short[] array, final int startIndexInclusive, f
*
* <p>There is no special handling for multi-dimensional arrays.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap(["1", "2", "3"], 0, 2) -> ["3", "2", "1"]</li>
* <li>ArrayUtils.swap(["1", "2", "3"], 0, 0) -> ["1", "2", "3"]</li>
* <li>ArrayUtils.swap(["1", "2", "3"], 1, 0) -> ["2", "1", "3"]</li>
* <li>ArrayUtils.swap(["1", "2", "3"], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap(["1", "2", "3"], 0, 5) -> ["1", "2", "3"]</li>
* <li>ArrayUtils.swap(["1", "2", "3"], -1, 1) -> ["2", "1", "3"]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -1880,24 +1881,27 @@ public static void swap(final Object[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>There is no special handling for multi-dimensional arrays.</p>
*
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([true, false, true], 0, 2) -> [true, false, true]</li>
* <li>ArrayUtils.swap([true, false, true], 0, 0) -> [true, false, true]</li>
* <li>ArrayUtils.swap([true, false, true], 1, 0) -> [false, true, true]</li>
* <li>ArrayUtils.swap([true, false, true], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([true, false, true], 0, 5) -> [true, false, true]</li>
* <li>ArrayUtils.swap([true, false, true], -1, 1) -> [false, true, true]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -1906,24 +1910,25 @@ public static void swap(final long[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -1932,24 +1937,25 @@ public static void swap(final int[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -1958,24 +1964,25 @@ public static void swap(final short[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -1984,24 +1991,25 @@ public static void swap(final char[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -2010,24 +2018,25 @@ public static void swap(final byte[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -2036,24 +2045,25 @@ public static void swap(final double[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -2062,24 +2072,25 @@ public static void swap(final float[] array, int offset1, int offset2) {
/**
* <p>Swaps two elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).</p>
*
* <p>Examples:
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3], 0, 2) -> [3, 2, 1]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 0) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 1, 0) -> [2, 1, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3], 0, 5) -> [1, 2, 3]</li>
* <li>ArrayUtils.swap([1, 2, 3], -1, 1) -> [2, 1, 3]</li>
* </ul>
* </p>
*
* @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) {
if (array == null || array.length == 0) {
return;
}
swap(array, offset1, offset2, 1);
@ -2088,45 +2099,61 @@ public static void swap(final boolean[] array, int offset1, int offset2) {
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 2, 1) -> ["3", "2", "1", "4"]</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 0, 1) -> ["1", "2", "3", "4"]</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 2, 0, 2) -> ["3", "4", "1", "2"]</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* </ul>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </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 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;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([true, false, true, false], 0, 2, 1) -> [true, false, true, false]</li>
* <li>ArrayUtils.swap([true, false, true, false], 0, 0, 1) -> [true, false, true, false]</li>
* <li>ArrayUtils.swap([true, false, true, false], 0, 2, 2) -> [true, false, true, false]</li>
* <li>ArrayUtils.swap([true, false, true, false], 0, 5, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([true, false, true, false], -3, 2, 2) -> [true, false, true, false]</li>
* <li>ArrayUtils.swap([true, false, true, false], 0, 3, 3) -> [false, false, true, true]</li>
* </ul>
* </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 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
*/
public static void swap(final boolean[] array, int offset1, int offset2, int len) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
boolean aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
* </ul>
* </p>
*
@ -2134,31 +2161,42 @@ public static void swap(final boolean[] array, int offset1, int offset2, int le
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
byte aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
byte aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
* </ul>
* </p>
*
@ -2166,30 +2204,41 @@ public static void swap(final byte[] array, int offset1, int offset2, int len)
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
char aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
char aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
* </ul>
* </p>
*
@ -2197,30 +2246,41 @@ public static void swap(final char[] array, int offset1, int offset2, int len)
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
double aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
double aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
* </ul>
* </p>
*
@ -2228,30 +2288,42 @@ public static void swap(final double[] array, int offset1, int offset2, int len
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
float aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
float aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
* </ul>
* </p>
*
@ -2259,30 +2331,41 @@ public static void swap(final float[] array, int offset1, int offset2, int len)
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
int aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
int aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
* </ul>
* </p>
*
@ -2290,30 +2373,41 @@ public static void swap(final int[] array, int offset1, int offset2, int len) {
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
long aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
}
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
long aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 2, 1) -> ["3", "2", "1", "4"]</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 0, 1) -> ["1", "2", "3", "4"]</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 2, 0, 2) -> ["3", "4", "1", "2"]</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], -3, 2, 2) -> ["3", "4", "1", "2"]</li>
* <li>ArrayUtils.swap(["1", "2", "3", "4"], 0, 3, 3) -> ["4", "2", "3", "1"]</li>
* </ul>
* </p>
*
@ -2321,30 +2415,41 @@ public static void swap(final long[] array, int offset1, int offset2, int len)
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
Object aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
Object aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}
/**
* <p>Swaps a series of elements in the given array.</p>
*
* <p>This method does nothing for a {@code null} input array.</p>
* <p>This method does nothing for a {@code null} or empty input array or for overflow indices.
* Negative indices are promoted to 0(zero).
* If any of the sub-arrays to swap falls outside of the given array,
* then the swap is stopped at the end of the array and as many as possible elements are swapped.
* </p>
*
* <p>Examples:
* <ul>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 2, 1) -> [3, 2, 1, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 0, 1) -> [1, 2, 3, 4]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 2, 0, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> ArrayOutOfBoundsException</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], -3, 2, 2) -> [3, 4, 1, 2]</li>
* <li>ArrayUtils.swap([1, 2, 3, 4], 0, 3, 3) -> [4, 2, 3, 1]</li>
* </ul>
* </p>
*
@ -2352,16 +2457,25 @@ public static void swap(final Object[] array, int offset1, int offset2, int len
* @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) {
if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
for (int i = 0; i < len; i++) {
short aux = array[offset1 + i];
array[offset1 + i] = array[offset2 + i];
array[offset2 + i] = aux;
if (offset1 < 0) {
offset1 = 0;
}
if (offset2 < 0) {
offset2 = 0;
}
if (offset1 == offset2) {
return;
}
len = Math.min(Math.min(len, array.length - offset1), array.length - offset2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
short aux = array[offset1];
array[offset1] = array[offset2];
array[offset2] = aux;
}
}

View File

@ -2142,18 +2142,18 @@ public void testSwapCharRange() {
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};
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};
assertEquals(1, array[0]);
assertEquals(2, array[1]);
assertEquals(3, array[2]);
array = new char[] {1, 2, 3};
ArrayUtils.swap(array, 0, 2, 2);
assertEquals(3, array[0]);
assertEquals(2, array[1]);
assertEquals(1, array[2]);
}
@Test
@ -2173,18 +2173,18 @@ public void testSwapFloatRange() {
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};
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};
assertEquals(1, array[0], 0);
assertEquals(2, array[1], 0);
assertEquals(3, array[2], 0);
array = new float[] {1, 2, 3};
ArrayUtils.swap(array, 0, 2, 2);
assertEquals(3, array[0], 0);
assertEquals(2, array[1], 0);
assertEquals(1, array[2], 0);
}
@Test
@ -2204,18 +2204,18 @@ public void testSwapDoubleRange() {
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};
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};
assertEquals(1, array[0], 0);
assertEquals(2, array[1], 0);
assertEquals(3, array[2], 0);
array = new double[] {1, 2, 3};
ArrayUtils.swap(array, 0, 2, 2);
assertEquals(3, array[0], 0);
assertEquals(2, array[1], 0);
assertEquals(1, array[2], 0);
}
@Test
@ -2235,6 +2235,18 @@ public void testSwapIntRange() {
assertEquals(4, array[1]);
assertEquals(1, array[2]);
assertEquals(2, array[3]);
array = new int[] {1, 2, 3};
ArrayUtils.swap(array, 3, 0);
assertEquals(1, array[0]);
assertEquals(2, array[1]);
assertEquals(3, array[2]);
array = new int[] {1, 2, 3};
ArrayUtils.swap(array, 0, 2, 2);
assertEquals(3, array[0]);
assertEquals(2, array[1]);
assertEquals(1, array[2]);
}
@Test
@ -2249,18 +2261,6 @@ public void testSwapIntExchangedOffsets() {
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};
@ -2278,18 +2278,18 @@ public void testSwapLongRange() {
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};
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};
assertEquals(1, array[0]);
assertEquals(2, array[1]);
assertEquals(3, array[2]);
array = new long[] {1, 2, 3};
ArrayUtils.swap(array, 0, 2, 2);
assertEquals(3, array[0]);
assertEquals(2, array[1]);
assertEquals(1, array[2]);
}
@Test
@ -2309,18 +2309,21 @@ public void testSwapObjectRange() {
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);
}
array = new String[] {"1", "2", "3", "4"};
ArrayUtils.swap(array, -1, 2, 3);
assertEquals("3", array[0]);
assertEquals("4", array[1]);
assertEquals("1", array[2]);
assertEquals("2", array[3]);
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testSwapObjectOutOfRangeLen() {
String[] array = new String[] {"1", "2", "3"};
ArrayUtils.swap(array, 0, 2, 2);
array = new String[] {"1", "2", "3", "4", "5"};
ArrayUtils.swap(array, -3, 2, 3);
assertEquals("3", array[0]);
assertEquals("4", array[1]);
assertEquals("5", array[2]);
assertEquals("2", array[3]);
assertEquals("1", array[4]);
}
//-----------------------------------------------------------------------