Merge branch 'LANG-1122'

LANG-1122: Inconsistent behavior of swap for malformed inputs. Thanks
to Adrian Ber.
This commit is contained in:
Benedikt Ritter 2015-05-07 20:31:49 +02:00
commit 13d7ce9fb6
4 changed files with 340 additions and 194 deletions

View File

@ -481,6 +481,9 @@
<contributor>
<name>Felipe Adorno</name>
</contributor>
<contributor>
<name>Adrian Ber</name>
</contributor>
</contributors>
<!-- Lang should depend on very little -->

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1122" type="fix" dev="britter" due-to="Adrian Ber">Inconsistent behavior of swap for malformed inputs</action>
<action issue="LANG-701" type="add" dev="britter" due-to="James Sawle">StringUtils join with var args</action>
<action issue="LANG-1130" type="fix" dev="britter">Fix critical issues reported by SonarQube</action>
<action issue="LANG-1131" type="fix" dev="britter">StrBuilder.equals(StrBuilder) doesn't check for null inputs</action>

View File

@ -1853,24 +1853,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);
@ -1879,24 +1880,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);
@ -1905,24 +1909,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);
@ -1931,24 +1936,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);
@ -1957,24 +1963,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);
@ -1983,24 +1990,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);
@ -2009,24 +2017,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);
@ -2035,24 +2044,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);
@ -2061,24 +2071,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);
@ -2087,45 +2098,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>
*
@ -2133,31 +2160,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>
*
@ -2165,30 +2203,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>
*
@ -2196,30 +2245,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>
*
@ -2227,30 +2287,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>
*
@ -2258,30 +2330,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>
*
@ -2289,30 +2372,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>
*
@ -2320,30 +2414,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>
*
@ -2351,16 +2456,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;
}
}
@ -2535,6 +2649,8 @@ public static void shift(final boolean[] array, int startIndexInclusive, int end
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2593,6 +2709,8 @@ public static void shift(final byte[] array, int startIndexInclusive, int endInd
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2651,6 +2769,8 @@ public static void shift(final char[] array, int startIndexInclusive, int endInd
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2709,6 +2829,8 @@ public static void shift(final double[] array, int startIndexInclusive, int endI
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2767,6 +2889,8 @@ public static void shift(final float[] array, int startIndexInclusive, int endIn
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2825,6 +2949,8 @@ public static void shift(final int[] array, int startIndexInclusive, int endInde
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2882,6 +3008,8 @@ public static void shift(final long[] array, int startIndexInclusive, int endInd
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2939,6 +3067,8 @@ public static void shift(final Object[] array, int startIndexInclusive, int endI
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;
@ -2997,6 +3127,8 @@ public static void shift(final short[] array, int startIndexInclusive, int endIn
if (offset < 0) {
offset += n;
}
// For algorithm explanations and proof of O(n) time complexity and O(1) space complexity
// see https://beradrian.wordpress.com/2015/04/07/shift-an-array-in-on-in-place/
while (n > 1 && offset > 0) {
int n_offset = n - offset;

View File

@ -2140,18 +2140,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);
}
assertEquals(1, array[0]);
assertEquals(2, array[1]);
assertEquals(3, array[2]);
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testSwapCharOutOfRangeLen() {
char[] array = new char[] {1, 2, 3};
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
@ -2171,18 +2171,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);
}
assertEquals(1, array[0], 0);
assertEquals(2, array[1], 0);
assertEquals(3, array[2], 0);
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testSwapFloatOutOfRangeLen() {
float[] array = new float[] {1, 2, 3};
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
@ -2202,18 +2202,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);
}
assertEquals(1, array[0], 0);
assertEquals(2, array[1], 0);
assertEquals(3, array[2], 0);
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testSwapDoubleOutOfRangeLen() {
double[] array = new double[] {1, 2, 3};
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
@ -2233,6 +2233,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
@ -2247,18 +2259,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};
@ -2276,18 +2276,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);
}
assertEquals(1, array[0]);
assertEquals(2, array[1]);
assertEquals(3, array[2]);
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void testSwapLongOutOfRangeLen() {
long[] array = new long[] {1, 2, 3};
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
@ -2307,18 +2307,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]);
}
//-----------------------------------------------------------------------
@ -2479,6 +2482,13 @@ public void testShiftShort() {
assertEquals(4, array[1]);
assertEquals(1, array[2]);
assertEquals(2, array[3]);
array = new short[] {1, 2, 3, 4, 5};
ArrayUtils.shift(array, 2);
assertEquals(4, array[0]);
assertEquals(5, array[1]);
assertEquals(1, array[2]);
assertEquals(2, array[3]);
assertEquals(3, array[4]);
}
@Test