Skip to content

Commit

Permalink
Make ArraySorter null-safe
Browse files Browse the repository at this point in the history
Make ArrayUtils.removeAll() null-safe
  • Loading branch information
garydgregory committed Feb 24, 2024
1 parent 7bb1950 commit aeca68f
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate ThreadUtils 0-argument constructor.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Deprecate TypeUtils 0-argument constructor.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make ArrayFill null-safe.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make ArraySorter null-safe.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make ArrayUtils.removeAll() null-safe.</action>
<!-- UPDATE -->
<action type="update" dev="sebb" due-to="Dependabot">Bump commons-parent from 64 to 66.</action>
<action type="update" dev="ggregory" due-to="Dependabot">Bump org.codehaus.mojo:exec-maven-plugin from 3.1.1 to 3.2.0 #1175.</action>
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/org/apache/commons/lang3/ArraySorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class ArraySorter {
* @see Arrays#sort(byte[])
*/
public static byte[] sort(final byte[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -47,7 +49,9 @@ public static byte[] sort(final byte[] array) {
* @see Arrays#sort(char[])
*/
public static char[] sort(final char[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -59,7 +63,9 @@ public static char[] sort(final char[] array) {
* @see Arrays#sort(double[])
*/
public static double[] sort(final double[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -71,7 +77,9 @@ public static double[] sort(final double[] array) {
* @see Arrays#sort(float[])
*/
public static float[] sort(final float[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -83,7 +91,9 @@ public static float[] sort(final float[] array) {
* @see Arrays#sort(int[])
*/
public static int[] sort(final int[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -95,7 +105,9 @@ public static int[] sort(final int[] array) {
* @see Arrays#sort(long[])
*/
public static long[] sort(final long[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -107,7 +119,9 @@ public static long[] sort(final long[] array) {
* @see Arrays#sort(short[])
*/
public static short[] sort(final short[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -120,7 +134,9 @@ public static short[] sort(final short[] array) {
* @see Arrays#sort(Object[])
*/
public static <T> T[] sort(final T[] array) {
Arrays.sort(array);
if (array != null) {
Arrays.sort(array);
}
return array;
}

Expand All @@ -135,7 +151,9 @@ public static <T> T[] sort(final T[] array) {
* @see Arrays#sort(Object[])
*/
public static <T> T[] sort(final T[] array, final Comparator<? super T> comparator) {
Arrays.sort(array, comparator);
if (array != null) {
Arrays.sort(array, comparator);
}
return array;
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/apache/commons/lang3/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5296,6 +5296,9 @@ static Object removeAll(final Object array, final BitSet indices) {
*/
// package protected for access by unit tests
static Object removeAll(final Object array, final int... indices) {
if (array == null) {
return null;
}
final int length = getLength(array);
int diff = 0; // number of distinct indexes, i.e. number of entries that will be removed
final int[] clonedIndices = ArraySorter.sort(clone(indices));
Expand All @@ -5319,7 +5322,7 @@ static Object removeAll(final Object array, final int... indices) {

// create result array
final Object result = Array.newInstance(array.getClass().getComponentType(), length - diff);
if (diff < length) {
if (diff < length && clonedIndices != null) {
int end = length; // index just after last copy
int dest = length - diff; // number of entries so far not copied
for (int i = clonedIndices.length - 1; i >= 0; i--) {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/apache/commons/lang3/ArraySorterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
package org.apache.commons.lang3;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Arrays;

import org.junit.jupiter.api.Test;

/**
* Tests {@link ArraySorter}.
*/
public class ArraySorterTest extends AbstractLangTest {

@Test
Expand All @@ -31,6 +35,7 @@ public void testSortByteArray() {
final byte[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((byte[]) null));
}

@Test
Expand All @@ -39,6 +44,7 @@ public void testSortCharArray() {
final char[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((char[]) null));
}

@Test
Expand All @@ -47,6 +53,7 @@ public void testSortComparable() {
final String[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2, String::compareTo));
assertNull(ArraySorter.sort((String[]) null));
}

@Test
Expand All @@ -55,6 +62,7 @@ public void testSortDoubleArray() {
final double[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((double[]) null));
}

@Test
Expand All @@ -63,6 +71,7 @@ public void testSortFloatArray() {
final float[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((float[]) null));
}

@Test
Expand All @@ -71,6 +80,7 @@ public void testSortIntArray() {
final int[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((int[]) null));
}

@Test
Expand All @@ -79,6 +89,7 @@ public void testSortLongArray() {
final long[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((long[]) null));
}

@Test
Expand All @@ -87,6 +98,7 @@ public void testSortObjects() {
final String[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((String[]) null));
}

@Test
Expand All @@ -95,6 +107,7 @@ public void testSortShortArray() {
final short[] array2 = array1.clone();
Arrays.sort(array1);
assertArrayEquals(array1, ArraySorter.sort(array2));
assertNull(ArraySorter.sort((short[]) null));
}

}
Loading

0 comments on commit aeca68f

Please sign in to comment.