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/05/07 20:32:32 UTC
[1/5] [lang] consistent treatment for negative indices for swap method
Repository: commons-lang
Updated Branches:
refs/heads/master 48d142dc3 -> 13d7ce9fb
consistent treatment for negative indices for swap method
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/5b7608d1
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/5b7608d1
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/5b7608d1
Branch: refs/heads/master
Commit: 5b7608d1549989d92dd159392c19d9ba8ce0e62e
Parents: f431270
Author: beradrian <be...@yahoo.com>
Authored: Thu May 7 09:50:41 2015 +0300
Committer: beradrian <be...@yahoo.com>
Committed: Thu May 7 09:50:41 2015 +0300
----------------------------------------------------------------------
.../org/apache/commons/lang3/ArrayUtils.java | 354 ++++++++++++-------
.../apache/commons/lang3/ArrayUtilsTest.java | 121 +++----
2 files changed, 296 insertions(+), 179 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/5b7608d1/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 0afe0e4..177a0ba 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -1854,24 +1854,25 @@ public class ArrayUtils {
*
* <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 class ArrayUtils {
/**
* <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 class ArrayUtils {
/**
* <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 class ArrayUtils {
/**
* <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 class ArrayUtils {
/**
* <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 class ArrayUtils {
/**
* <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 class ArrayUtils {
/**
* <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 class ArrayUtils {
/**
* <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 class ArrayUtils {
/**
* <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,14 +2099,19 @@ public class ArrayUtils {
/**
* <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([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], -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>
*
@@ -2103,30 +2119,41 @@ public class ArrayUtils {
* @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) {
+ if (array == null || array.length == 0 || offset1 >= array.length || offset2 >= array.length) {
return;
}
- for (int i = 0; i < len; i++) {
- boolean 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++) {
+ 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} 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([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([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 class ArrayUtils {
* @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 class ArrayUtils {
* @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 class ArrayUtils {
* @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 class ArrayUtils {
* @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 class ArrayUtils {
* @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 class ArrayUtils {
* @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 class ArrayUtils {
* @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 class ArrayUtils {
* @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;
+ }
+ if (offset1 < 0) {
+ offset1 = 0;
+ }
+ if (offset2 < 0) {
+ offset2 = 0;
+ }
+ if (offset1 == offset2) {
return;
}
- for (int i = 0; i < len; i++) {
- short aux = array[offset1 + i];
- array[offset1 + i] = array[offset2 + i];
- array[offset2 + i] = aux;
+ 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;
}
}
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/5b7608d1/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 be3e119..b021719 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
@@ -2142,18 +2142,18 @@ public class ArrayUtilsTest {
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 class ArrayUtilsTest {
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 class ArrayUtilsTest {
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 class ArrayUtilsTest {
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 class ArrayUtilsTest {
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 class ArrayUtilsTest {
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 class ArrayUtilsTest {
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]);
}
//-----------------------------------------------------------------------
[3/5] [lang] Add LANG-1122 to changes.xml
Posted by br...@apache.org.
Add LANG-1122 to changes.xml
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/c885a32e
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/c885a32e
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/c885a32e
Branch: refs/heads/master
Commit: c885a32e24662cbbe9fb6b8a0bc3c8dacf3288b5
Parents: e79a590
Author: Benedikt Ritter <br...@apache.org>
Authored: Thu May 7 20:29:53 2015 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Thu May 7 20:29:53 2015 +0200
----------------------------------------------------------------------
src/changes/changes.xml | 1 +
1 file changed, 1 insertion(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/c885a32e/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 27a7f5c..5e9efaf 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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-1114" type="fix" dev="britter" due-to="Andy Coates">TypeUtils.ParameterizedType#equals doesn't work with wildcard types</action>
<action issue="LANG-1119" type="add" dev="britter" due-to="Loic Guibert">Add rotate(string, int) method to StringUtils</action>
<action issue="LANG-1118" type="fix" dev="britter" due-to="Loic Guibert">StringUtils.repeat('z', -1) throws NegativeArraySizeException</action>
[2/5] [lang] new test case explanations about algorithm
Posted by br...@apache.org.
new test case
explanations about algorithm
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/e79a590e
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/e79a590e
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/e79a590e
Branch: refs/heads/master
Commit: e79a590e0cdabd93883aca964db2a862ff3f8490
Parents: 5b7608d
Author: beradrian <be...@yahoo.com>
Authored: Thu May 7 11:31:28 2015 +0300
Committer: beradrian <be...@yahoo.com>
Committed: Thu May 7 11:31:28 2015 +0300
----------------------------------------------------------------------
.../java/org/apache/commons/lang3/ArrayUtils.java | 18 ++++++++++++++++++
.../org/apache/commons/lang3/ArrayUtilsTest.java | 7 +++++++
2 files changed, 25 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e79a590e/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 177a0ba..20ff85d 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -2650,6 +2650,8 @@ public class ArrayUtils {
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;
@@ -2708,6 +2710,8 @@ public class ArrayUtils {
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;
@@ -2766,6 +2770,8 @@ public class ArrayUtils {
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;
@@ -2824,6 +2830,8 @@ public class ArrayUtils {
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 +2890,8 @@ public class ArrayUtils {
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;
@@ -2940,6 +2950,8 @@ public class ArrayUtils {
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 +3009,8 @@ public class ArrayUtils {
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;
@@ -3054,6 +3068,8 @@ public class ArrayUtils {
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;
@@ -3112,6 +3128,8 @@ public class ArrayUtils {
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;
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/e79a590e/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 b021719..cdd21fd 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
@@ -2484,6 +2484,13 @@ public class ArrayUtilsTest {
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
[5/5] [lang] Merge branch 'LANG-1122'
Posted by br...@apache.org.
Merge branch 'LANG-1122'
LANG-1122: Inconsistent behavior of swap for malformed inputs. Thanks
to Adrian Ber.
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/13d7ce9f
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/13d7ce9f
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/13d7ce9f
Branch: refs/heads/master
Commit: 13d7ce9fb65e8f05014daf4ee6cebc76f4781e6c
Parents: 48d142d 35096be
Author: Benedikt Ritter <br...@apache.org>
Authored: Thu May 7 20:31:49 2015 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Thu May 7 20:31:49 2015 +0200
----------------------------------------------------------------------
pom.xml | 3 +
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/ArrayUtils.java | 372 +++++++++++++------
.../apache/commons/lang3/ArrayUtilsTest.java | 128 ++++---
4 files changed, 325 insertions(+), 179 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/13d7ce9f/pom.xml
----------------------------------------------------------------------
diff --cc pom.xml
index 4f8c2e4,e8af806..78f3c07
--- a/pom.xml
+++ b/pom.xml
@@@ -479,8 -479,8 +479,11 @@@
<name>Michał Kordas</name>
</contributor>
<contributor>
+ <name>Felipe Adorno</name>
+ </contributor>
++ <contributor>
+ <name>Adrian Ber</name>
+ </contributor>
</contributors>
<!-- Lang should depend on very little -->
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/13d7ce9f/src/changes/changes.xml
----------------------------------------------------------------------
diff --cc src/changes/changes.xml
index ab6245e,5e9efaf..56f4401
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@@ -22,18 -22,7 +22,19 @@@
<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>
+ <action issue="LANG-1105" type="add" dev="britter" due-to="Hendrik Saly">Add ThreadUtils - A utility class which provides helper methods related to java.lang.Thread</action>
+ <action issue="LANG-1031" type="add" dev="britter" due-to="Felipe Adorno">Add annotations to exclude fields from ReflectionEqualsBuilder, ReflectionToStringBuilder and ReflectionHashCodeBuilder</action>
+ <action issue="LANG-1127" type="add" dev="chas">Unit test helpers which set and reset default Locale and TimeZone</action>
+ <action issue="LANG-1128" type="fix" dev="britter" due-to="jacktan1991">JsonToStringStyle doesn't handle chars and objects correctly</action>
+ <action issue="LANG-456" type="fix" dev="britter" due-to="Bob Fields, Woosan Ko, Bruno P. Kinoshita">HashCodeBuilder throws StackOverflowError in bidirectional navigable association</action>
+ <action issue="LANG-1126" type="fix" dev="britter">DateFormatUtilsTest.testSMTP depends on the default Locale</action>
+ <action issue="LANG-1123" type="fix" dev="chas" due-to="Christian P. Momon">Unit test FastDatePrinterTimeZonesTest needs a timezone set</action>
+ <action issue="LANG-916" type="fix" dev="chas" due-to="Christian P. Momon">CLONE - DateFormatUtils.format does not correctly change Calendar TimeZone in certain situations</action>
+ <action issue="LANG-1116" type="fix" dev="chas" due-to="Aaron Sheldon">DateUtilsTest.testLang530 fails for some timezones</action>
<action issue="LANG-1114" type="fix" dev="britter" due-to="Andy Coates">TypeUtils.ParameterizedType#equals doesn't work with wildcard types</action>
<action issue="LANG-1119" type="add" dev="britter" due-to="Loic Guibert">Add rotate(string, int) method to StringUtils</action>
<action issue="LANG-1118" type="fix" dev="britter" due-to="Loic Guibert">StringUtils.repeat('z', -1) throws NegativeArraySizeException</action>
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/13d7ce9f/src/main/java/org/apache/commons/lang3/ArrayUtils.java
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/13d7ce9f/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
----------------------------------------------------------------------
[4/5] [lang] Add Adrian Ber to the list of contributors
Posted by br...@apache.org.
Add Adrian Ber to the list of contributors
Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/35096bee
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/35096bee
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/35096bee
Branch: refs/heads/master
Commit: 35096beed02221413948ed840bdd33c8e3c921d1
Parents: c885a32
Author: Benedikt Ritter <br...@apache.org>
Authored: Thu May 7 20:30:09 2015 +0200
Committer: Benedikt Ritter <br...@apache.org>
Committed: Thu May 7 20:30:09 2015 +0200
----------------------------------------------------------------------
pom.xml | 3 +++
1 file changed, 3 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-lang/blob/35096bee/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 3d80909..e8af806 100644
--- a/pom.xml
+++ b/pom.xml
@@ -478,6 +478,9 @@
<contributor>
<name>Michał Kordas</name>
</contributor>
+ <contributor>
+ <name>Adrian Ber</name>
+ </contributor>
</contributors>
<!-- Lang should depend on very little -->