diff --git a/src/changes/changes.xml b/src/changes/changes.xml index caddf69f0..0d633988c 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -62,6 +62,7 @@ The type attribute can be add,update,fix,remove. Add StopWatch.getStopTime(). More test coverage for CharSequenceUtils. #631. ArrayUtils.toPrimitive(Object) does not support boolean and other types #607. + Add fluent-style ArrayUtils.sort(Object[]). Enable Dependabot #587. Bump junit-jupiter from 5.6.2 to 5.7.0. diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index f620fd930..8aeafa702 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -973,7 +973,7 @@ public class ArrayUtils { System.arraycopy(array2, 0, joinedArray, array1.length, array2.length); return joinedArray; } - + /** *

Adds all the elements of the given arrays into a new array. *

The new array contains all of the element of {@code array1} followed @@ -2524,50 +2524,50 @@ public class ArrayUtils { return INDEX_NOT_FOUND; } - // int IndexOf -//----------------------------------------------------------------------- -/** - *

Finds the index of the given value in the array. - * - *

This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. - * - * @param array the array to search through for the object, may be {@code null} - * @param valueToFind the value to find - * @return the index of the value within the array, - * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input - */ -public static int indexOf(final int[] array, final int valueToFind) { - return indexOf(array, valueToFind, 0); -} - + // int IndexOf + //----------------------------------------------------------------------- /** - *

Finds the index of the given value in the array starting at the given index. + *

Finds the index of the given value in the array. * *

This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. * - *

A negative startIndex is treated as zero. A startIndex larger than the array - * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). - * * @param array the array to search through for the object, may be {@code null} * @param valueToFind the value to find - * @param startIndex the index to start searching at * @return the index of the value within the array, * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input */ - public static int indexOf(final int[] array, final int valueToFind, int startIndex) { - if (array == null) { - return INDEX_NOT_FOUND; - } - if (startIndex < 0) { - startIndex = 0; - } - for (int i = startIndex; i < array.length; i++) { - if (valueToFind == array[i]) { - return i; - } - } + public static int indexOf(final int[] array, final int valueToFind) { + return indexOf(array, valueToFind, 0); + } + + /** + *

Finds the index of the given value in the array starting at the given index. + * + *

This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array. + * + *

A negative startIndex is treated as zero. A startIndex larger than the array + * length will return {@link #INDEX_NOT_FOUND} ({@code -1}). + * + * @param array the array to search through for the object, may be {@code null} + * @param valueToFind the value to find + * @param startIndex the index to start searching at + * @return the index of the value within the array, + * {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input + */ +public static int indexOf(final int[] array, final int valueToFind, int startIndex) { + if (array == null) { return INDEX_NOT_FOUND; } + if (startIndex < 0) { + startIndex = 0; + } + for (int i = startIndex; i < array.length; i++) { + if (valueToFind == array[i]) { + return i; + } + } + return INDEX_NOT_FOUND; +} // long IndexOf //----------------------------------------------------------------------- @@ -3133,9 +3133,6 @@ public static int indexOf(final int[] array, final int valueToFind) { return getLength(array) == 0; } - // IndexOf search - // ---------------------------------------------------------------------- - /** *

Checks if an array of primitive bytes is empty or {@code null}. * @@ -3147,6 +3144,9 @@ public static int indexOf(final int[] array, final int valueToFind) { return getLength(array) == 0; } + // IndexOf search + // ---------------------------------------------------------------------- + /** *

Checks if an array of primitive chars is empty or {@code null}. * @@ -3180,8 +3180,6 @@ public static int indexOf(final int[] array, final int valueToFind) { return getLength(array) == 0; } - - /** *

Checks if an array of primitive ints is empty or {@code null}. * @@ -3193,6 +3191,8 @@ public static int indexOf(final int[] array, final int valueToFind) { return getLength(array) == 0; } + + /** *

Checks if an array of primitive longs is empty or {@code null}. * @@ -3436,7 +3436,6 @@ public static int indexOf(final int[] array, final int valueToFind) { return getLength(array1) == getLength(array2); } - /** *

Checks whether two arrays are the same length, treating * {@code null} arrays as length {@code 0}. @@ -3453,6 +3452,7 @@ public static int indexOf(final int[] array, final int valueToFind) { return getLength(array1) == getLength(array2); } + /** *

Checks whether two arrays are the same length, treating * {@code null} arrays as length {@code 0}. @@ -3978,7 +3978,6 @@ public static int indexOf(final int[] array, final int valueToFind) { return INDEX_NOT_FOUND; } - /** *

Finds the last index of the given value within the array. * @@ -3993,6 +3992,7 @@ public static int indexOf(final int[] array, final int valueToFind) { return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); } + /** *

Finds the last index of the given value in the array starting at the given index. * @@ -4452,9 +4452,6 @@ public static int indexOf(final int[] array, final int valueToFind) { return array; } - // Primitive/Object array converters - // ---------------------------------------------------------------------- - /** *

Defensive programming technique to change a {@code null} * reference to an empty one. @@ -4475,6 +4472,9 @@ public static int indexOf(final int[] array, final int valueToFind) { return array; } + // Primitive/Object array converters + // ---------------------------------------------------------------------- + /** *

Defensive programming technique to change a {@code null} * reference to an empty one. @@ -7856,6 +7856,20 @@ public static int indexOf(final int[] array, final int valueToFind) { } } + /** + * Sorts and returns the given array. + * + * @param the array type. + * @param array the array to sort. + * @return the given array. + * @see Arrays#sort(Object[]) + * @since 3.12 + */ + public static T[] sort(T[] array) { + Arrays.sort(array); + return array; + } + /** *

Produces a new {@code boolean} array containing the elements * between the start and end indices. diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 46bb1042e..2c5a1f607 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -78,7 +78,6 @@ public class ArrayUtilsTest { assertEquals("foo", array[0]); assertEquals("bar", array[1]); } - /** * Tests generic array creation with parameters of common base type. */ @@ -4529,7 +4528,6 @@ public class ArrayUtilsTest { assertEquals(2, array[3]); } - @Test public void testShiftFloat() { final float[] array = new float[]{1, 2, 3, 4}; @@ -4555,6 +4553,7 @@ public class ArrayUtilsTest { assertEquals(2, array[3]); } + @Test public void testShiftInt() { final int[] array = new int[]{1, 2, 3, 4}; @@ -5097,6 +5096,13 @@ public class ArrayUtilsTest { } } + public void testSort() { + final String[] array1 = ArrayUtils.toArray("foo", "bar"); + final String[] array2 = array1.clone(); + Arrays.sort(array1); + assertEquals(array1, ArrayUtils.sort(array2)); + } + @Test public void testSubarrayBoolean() { final boolean[] nullArray = null; diff --git a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java index 6eb6d1cef..26250d6e9 100644 --- a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.lang3; +import static org.apache.commons.lang3.ArrayUtils.sort; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -27,7 +28,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; -import java.util.Arrays; import org.junit.jupiter.api.Test; @@ -39,8 +39,7 @@ public class BooleanUtilsTest { @Test public void test_booleanValues() { final Boolean[] expected = new Boolean[] {false, true}; - Arrays.sort(expected); - assertArrayEquals(expected, BooleanUtils.booleanValues()); + assertArrayEquals(sort(expected), BooleanUtils.booleanValues()); } @Test