From 06506aecf9e46441d08edab311e19e599d273fab Mon Sep 17 00:00:00 2001 From: beradrian Date: Mon, 23 Mar 2015 13:06:03 +0200 Subject: [PATCH] tabs replaced by spaces new tests added --- .../org/apache/commons/lang3/ArrayUtils.java | 476 +++++++++--------- .../apache/commons/lang3/ArrayUtilsTest.java | 18 +- 2 files changed, 253 insertions(+), 241 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 8f2172671..fb53bf867 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -2043,10 +2043,10 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - byte aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + byte aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } } /** @@ -2065,10 +2065,10 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - char aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + char aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } } /** @@ -2087,10 +2087,10 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - double aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + double aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } } /** @@ -2109,10 +2109,10 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - float aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + float aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } } /** @@ -2131,10 +2131,10 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - int aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + int aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } } /** @@ -2153,9 +2153,9 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - long aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; + long aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; } } @@ -2175,10 +2175,10 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - Object aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + Object aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } } /** @@ -2197,10 +2197,10 @@ public class ArrayUtils { return; } for (int i = 0; i < len; i++) { - short aux = array[offset1 + i]; - array[offset1 + i] = array[offset2 + i]; - array[offset2 + i] = aux; - } + short aux = array[offset1 + i]; + array[offset1 + i] = array[offset2 + i]; + array[offset2 + i] = aux; + } } // Shift @@ -2361,35 +2361,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2419,35 +2419,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2477,35 +2477,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2535,35 +2535,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2593,35 +2593,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2651,35 +2651,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2708,35 +2708,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2765,35 +2765,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } /** @@ -2823,35 +2823,35 @@ public class ArrayUtils { return; } if (startIndexInclusive < 0) { - startIndexInclusive = 0; + startIndexInclusive = 0; } if (endIndexExclusive >= array.length) { - endIndexExclusive = array.length; + endIndexExclusive = array.length; } int n = endIndexExclusive - startIndexInclusive; if (n <= 1) { - return; + return; + } + offset %= n; + if (offset < 0) { + offset += n; + } + while (n > 1 && offset > 0) { + int n_offset = n - offset; + + if (offset > n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); + n = offset; + offset -= n_offset; + } else if (offset < n_offset) { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + startIndexInclusive += offset; + n = n_offset; + } else { + swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); + break; + } } - offset %= n; - if (offset < 0) { - offset += n; - } - while (n > 1 && offset > 0) { - int n_offset = n - offset; - - if (offset > n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n - n_offset, n_offset); - n = offset; - offset -= n_offset; - } else if (offset < n_offset) { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - startIndexInclusive += offset; - n = n_offset; - } else { - swap(array, startIndexInclusive, startIndexInclusive + n_offset, offset); - break; - } - } } // IndexOf search diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 805375a79..5633c9d1d 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -2123,9 +2123,21 @@ public class ArrayUtilsTest { public void testSwapChar() { char[] array = new char[] {1, 2, 3}; ArrayUtils.swap(array, 0, 2); - assertEquals(3, array[0]); - assertEquals(2, array[1]); - assertEquals(1, array[2]); + assertArrayEquals(new char[] {3, 2, 1}, array); + + array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 0); + assertArrayEquals(new char[] {1, 2, 3}, array); + + array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 1, 0); + assertArrayEquals(new char[] {2, 1, 3}, array); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testSwapCharOutOfBounds() { + char[] array = new char[] {1, 2, 3}; + ArrayUtils.swap(array, 0, 5); } @Test