Add fluent-style ArrayUtils.sort(Object[]).

This commit is contained in:
Gary Gregory 2020-11-14 11:41:46 -05:00
parent 14b60bbb9f
commit 09c662429b
4 changed files with 69 additions and 49 deletions

View File

@ -62,6 +62,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1359" type="add" dev="ggregory" due-to="Gary Gregory, Keegan Witt">Add StopWatch.getStopTime().</action>
<action type="add" dev="ggregory" due-to="Edgar Asatryan">More test coverage for CharSequenceUtils. #631.</action>
<action issue="LANG-1596" type="update" dev="aherbert" due-to="Richard Eckart de Castilho">ArrayUtils.toPrimitive(Object) does not support boolean and other types #607.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add fluent-style ArrayUtils.sort(Object[]).</action>
<!-- UPDATES -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Enable Dependabot #587.</action>
<action type="update" dev="chtompki">Bump junit-jupiter from 5.6.2 to 5.7.0.</action>

View File

@ -973,7 +973,7 @@ public class ArrayUtils {
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
return joinedArray;
}
/**
* <p>Adds all the elements of the given arrays into a new array.
* <p>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
//-----------------------------------------------------------------------
/**
* <p>Finds the index of the given value in the array.
*
* <p>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
//-----------------------------------------------------------------------
/**
* <p>Finds the index of the given value in the array starting at the given index.
* <p>Finds the index of the given value in the array.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
* <p>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);
}
/**
* <p>Finds the index of the given value in the array starting at the given index.
*
* <p>This method returns {@link #INDEX_NOT_FOUND} ({@code -1}) for a {@code null} input array.
*
* <p>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
// ----------------------------------------------------------------------
/**
* <p>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
// ----------------------------------------------------------------------
/**
* <p>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;
}
/**
* <p>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;
}
/**
* <p>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);
}
/**
* <p>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);
}
/**
* <p>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;
}
/**
* <p>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);
}
/**
* <p>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
// ----------------------------------------------------------------------
/**
* <p>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
// ----------------------------------------------------------------------
/**
* <p>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 <T> the array type.
* @param array the array to sort.
* @return the given array.
* @see Arrays#sort(Object[])
* @since 3.12
*/
public static <T> T[] sort(T[] array) {
Arrays.sort(array);
return array;
}
/**
* <p>Produces a new {@code boolean} array containing the elements
* between the start and end indices.

View File

@ -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;

View File

@ -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