Skip to content

Commit 623235e

Browse files
committed
[LANG-1811] ArrayUtils.shuffle() throws NullPointerException for null
array input. - Null test assertions are from PR #1553. - null inputs are now no-ops - This follows the class Javadoc and other method implementations in this class - Javadoc @param updates
1 parent 48d6e75 commit 623235e

File tree

3 files changed

+145
-36
lines changed

3 files changed

+145
-36
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ The <action> type attribute can be add,update,fix,remove.
9191
<action issue="LANG-1805" type="fix" dev="ggregory" due-to="Gary Gregory, jher235">Speedup StringUtils.join(AllPrimitiveTypes[], char, int, int), see StringUtilsJoinBenchmark, based on #1532. Also better StringBuilder allocation</action>
9292
<action issue="LANG-1749" type="fix" dev="ggregory" due-to="jinwoo choi, Gary Gregory, YuMing Ma, Ivan Šarić">TypeUtils.isAssignable returns a wrong result for ParameterizedType when raw class is "Class" #1548.</action>
9393
<action issue="LANG-1700" type="fix" dev="ggregory" due-to="seokhyeon moon, Gary Gregory, Ivan Šarić">Improve handling of parameterized types and variable unrolling.</action>
94+
<action issue="LANG-1811" type="fix" dev="ggregory" due-to="Raju Gupta, Gary Gregory">ArrayUtils.shuffle() throws NullPointerException for null array input.</action>
9495
<!-- ADD -->
9596
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_27.</action>
9697
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_27.</action>

src/main/java/org/apache/commons/lang3/ArrayUtils.java

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7428,14 +7428,16 @@ public static void shuffle(final boolean[] array) {
74287428
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
74297429
* algorithm</a>.
74307430
*
7431-
* @param array the array to shuffle.
7432-
* @param random the source of randomness used to permute the elements.
7431+
* @param array the array to shuffle, no-op if {@code null}.
7432+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
74337433
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
74347434
* @since 3.6
74357435
*/
74367436
public static void shuffle(final boolean[] array, final Random random) {
7437-
for (int i = array.length; i > 1; i--) {
7438-
swap(array, i - 1, random.nextInt(i), 1);
7437+
if (array != null && random != null) {
7438+
for (int i = array.length; i > 1; i--) {
7439+
swap(array, i - 1, random.nextInt(i), 1);
7440+
}
74397441
}
74407442
}
74417443

@@ -7462,14 +7464,16 @@ public static void shuffle(final byte[] array) {
74627464
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
74637465
* algorithm</a>.
74647466
*
7465-
* @param array the array to shuffle.
7466-
* @param random the source of randomness used to permute the elements.
7467+
* @param array the array to shuffle, no-op if {@code null}.
7468+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
74677469
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
74687470
* @since 3.6
74697471
*/
74707472
public static void shuffle(final byte[] array, final Random random) {
7471-
for (int i = array.length; i > 1; i--) {
7472-
swap(array, i - 1, random.nextInt(i), 1);
7473+
if (array != null && random != null) {
7474+
for (int i = array.length; i > 1; i--) {
7475+
swap(array, i - 1, random.nextInt(i), 1);
7476+
}
74737477
}
74747478
}
74757479

@@ -7496,14 +7500,16 @@ public static void shuffle(final char[] array) {
74967500
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
74977501
* algorithm</a>.
74987502
*
7499-
* @param array the array to shuffle.
7500-
* @param random the source of randomness used to permute the elements.
7503+
* @param array the array to shuffle, no-op if {@code null}.
7504+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
75017505
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
75027506
* @since 3.6
75037507
*/
75047508
public static void shuffle(final char[] array, final Random random) {
7505-
for (int i = array.length; i > 1; i--) {
7506-
swap(array, i - 1, random.nextInt(i), 1);
7509+
if (array != null && random != null) {
7510+
for (int i = array.length; i > 1; i--) {
7511+
swap(array, i - 1, random.nextInt(i), 1);
7512+
}
75077513
}
75087514
}
75097515

@@ -7530,14 +7536,16 @@ public static void shuffle(final double[] array) {
75307536
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
75317537
* algorithm</a>.
75327538
*
7533-
* @param array the array to shuffle.
7534-
* @param random the source of randomness used to permute the elements.
7539+
* @param array the array to shuffle, no-op if {@code null}.
7540+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
75357541
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
75367542
* @since 3.6
75377543
*/
75387544
public static void shuffle(final double[] array, final Random random) {
7539-
for (int i = array.length; i > 1; i--) {
7540-
swap(array, i - 1, random.nextInt(i), 1);
7545+
if (array != null && random != null) {
7546+
for (int i = array.length; i > 1; i--) {
7547+
swap(array, i - 1, random.nextInt(i), 1);
7548+
}
75417549
}
75427550
}
75437551

@@ -7564,14 +7572,16 @@ public static void shuffle(final float[] array) {
75647572
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
75657573
* algorithm</a>.
75667574
*
7567-
* @param array the array to shuffle.
7568-
* @param random the source of randomness used to permute the elements.
7575+
* @param array the array to shuffle, no-op if {@code null}.
7576+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
75697577
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
75707578
* @since 3.6
75717579
*/
75727580
public static void shuffle(final float[] array, final Random random) {
7573-
for (int i = array.length; i > 1; i--) {
7574-
swap(array, i - 1, random.nextInt(i), 1);
7581+
if (array != null && random != null) {
7582+
for (int i = array.length; i > 1; i--) {
7583+
swap(array, i - 1, random.nextInt(i), 1);
7584+
}
75757585
}
75767586
}
75777587

@@ -7598,14 +7608,16 @@ public static void shuffle(final int[] array) {
75987608
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
75997609
* algorithm</a>.
76007610
*
7601-
* @param array the array to shuffle.
7602-
* @param random the source of randomness used to permute the elements.
7611+
* @param array the array to shuffle, no-op if {@code null}.
7612+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
76037613
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
76047614
* @since 3.6
76057615
*/
76067616
public static void shuffle(final int[] array, final Random random) {
7607-
for (int i = array.length; i > 1; i--) {
7608-
swap(array, i - 1, random.nextInt(i), 1);
7617+
if (array != null && random != null) {
7618+
for (int i = array.length; i > 1; i--) {
7619+
swap(array, i - 1, random.nextInt(i), 1);
7620+
}
76097621
}
76107622
}
76117623

@@ -7632,14 +7644,16 @@ public static void shuffle(final long[] array) {
76327644
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
76337645
* algorithm</a>.
76347646
*
7635-
* @param array the array to shuffle.
7636-
* @param random the source of randomness used to permute the elements.
7647+
* @param array the array to shuffle, no-op if {@code null}.
7648+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
76377649
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
76387650
* @since 3.6
76397651
*/
76407652
public static void shuffle(final long[] array, final Random random) {
7641-
for (int i = array.length; i > 1; i--) {
7642-
swap(array, i - 1, random.nextInt(i), 1);
7653+
if (array != null && random != null) {
7654+
for (int i = array.length; i > 1; i--) {
7655+
swap(array, i - 1, random.nextInt(i), 1);
7656+
}
76437657
}
76447658
}
76457659

@@ -7666,14 +7680,16 @@ public static void shuffle(final Object[] array) {
76667680
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
76677681
* algorithm</a>.
76687682
*
7669-
* @param array the array to shuffle.
7670-
* @param random the source of randomness used to permute the elements.
7683+
* @param array the array to shuffle, no-op if {@code null}.
7684+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
76717685
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
76727686
* @since 3.6
76737687
*/
76747688
public static void shuffle(final Object[] array, final Random random) {
7675-
for (int i = array.length; i > 1; i--) {
7676-
swap(array, i - 1, random.nextInt(i), 1);
7689+
if (array != null && random != null) {
7690+
for (int i = array.length; i > 1; i--) {
7691+
swap(array, i - 1, random.nextInt(i), 1);
7692+
}
76777693
}
76787694
}
76797695

@@ -7700,14 +7716,16 @@ public static void shuffle(final short[] array) {
77007716
* Shuffles randomly the elements of the specified array using the <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle
77017717
* algorithm</a>.
77027718
*
7703-
* @param array the array to shuffle.
7704-
* @param random the source of randomness used to permute the elements.
7719+
* @param array the array to shuffle, no-op if {@code null}.
7720+
* @param random the source of randomness used to permute the elements, no-op if {@code null}.
77057721
* @see <a href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher-Yates shuffle algorithm</a>
77067722
* @since 3.6
77077723
*/
77087724
public static void shuffle(final short[] array, final Random random) {
7709-
for (int i = array.length; i > 1; i--) {
7710-
swap(array, i - 1, random.nextInt(i), 1);
7725+
if (array != null && random != null) {
7726+
for (int i = array.length; i > 1; i--) {
7727+
swap(array, i - 1, random.nextInt(i), 1);
7728+
}
77117729
}
77127730
}
77137731

0 commit comments

Comments
 (0)