Skip to content

Commit 32108d6

Browse files
committed
ArrayUtils
1 parent a889fe8 commit 32108d6

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/util/ArrayUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.datastax.oss.driver.internal.core.util;
1919

2020
import edu.umd.cs.findbugs.annotations.NonNull;
21+
import java.util.Random;
2122
import java.util.concurrent.ThreadLocalRandom;
2223

2324
public class ArrayUtils {
@@ -70,14 +71,13 @@ public static <ElementT> void shuffleHead(@NonNull ElementT[] elements, int n) {
7071
*
7172
* @param elements the array to shuffle.
7273
* @param n the number of elements to shuffle; must be {@code <= elements.length}.
73-
* @param random the {@link ThreadLocalRandom} instance to use. This is mainly intended to
74-
* facilitate tests.
74+
* @param random the {@link Random} instance to use. This is mainly intended to facilitate tests.
7575
* @see <a
7676
* href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm">Modern
7777
* Fisher-Yates shuffle</a>
7878
*/
7979
public static <ElementT> void shuffleHead(
80-
@NonNull ElementT[] elements, int n, @NonNull ThreadLocalRandom random) {
80+
@NonNull ElementT[] elements, int n, @NonNull Random random) {
8181
if (n > elements.length) {
8282
throw new ArrayIndexOutOfBoundsException(
8383
String.format(

core/src/test/java/com/datastax/oss/driver/internal/core/util/ArrayUtilsTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@
1818
package com.datastax.oss.driver.internal.core.util;
1919

2020
import static com.datastax.oss.driver.Assertions.assertThat;
21-
import static org.mockito.Mockito.anyInt;
22-
import static org.mockito.Mockito.mock;
23-
import static org.mockito.Mockito.when;
2421

25-
import java.util.concurrent.ThreadLocalRandom;
22+
import java.util.Random;
2623
import org.junit.Test;
2724

2825
public class ArrayUtilsTest {
@@ -86,15 +83,15 @@ public void should_not_bubble_down_when_target_index_lower() {
8683
@Test
8784
public void should_shuffle_head() {
8885
String[] array = {"a", "b", "c", "d", "e"};
89-
ThreadLocalRandom random = mock(ThreadLocalRandom.class);
90-
when(random.nextInt(anyInt()))
91-
.thenAnswer(
92-
(invocation) -> {
93-
int i = invocation.getArgument(0);
94-
// shifts elements by 1 to the right
95-
return i - 2;
96-
});
97-
ArrayUtils.shuffleHead(array, 3, random);
86+
Random deterministicRandom =
87+
new Random() {
88+
@Override
89+
public int nextInt(int bound) {
90+
// shifts elements by 1 to the right
91+
return bound - 2;
92+
}
93+
};
94+
ArrayUtils.shuffleHead(array, 3, deterministicRandom);
9895
assertThat(array[0]).isEqualTo("c");
9996
assertThat(array[1]).isEqualTo("a");
10097
assertThat(array[2]).isEqualTo("b");

0 commit comments

Comments
 (0)