8294693: Add Collections.shuffle overload that accepts RandomGenerator interface

Reviewed-by: smarks, darcy
This commit is contained in:
Tagir F. Valeev 2023-01-21 18:36:31 +00:00
parent c8dd7583a9
commit 67b1c890b3
2 changed files with 105 additions and 6 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -37,6 +37,7 @@ import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.random.RandomGenerator;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@ -411,8 +412,8 @@ public class Collections {
* portion of the list that runs from the first element to the current
* position, inclusive.
*
* <p>This method runs in linear time. If the specified list does not
* implement the {@link RandomAccess} interface and is large, this
* @implSpec This method runs in linear time. If the specified list does
* not implement the {@link RandomAccess} interface and is large, this
* implementation dumps the specified list into an array before shuffling
* it, and dumps the shuffled array back into the list. This avoids the
* quadratic behavior that would result from shuffling a "sequential
@ -431,6 +432,24 @@ public class Collections {
private static Random r;
/**
* Randomly permute the specified list using the specified source of
* randomness.<p>
*
* This method is equivalent to {@link #shuffle(List, RandomGenerator)}
* and exists for backward compatibility. The {@link #shuffle(List, RandomGenerator)}
* method is preferred, as it is not limited to random generators
* that extend the {@link Random} class.
*
* @param list the list to be shuffled.
* @param rnd the source of randomness to use to shuffle the list.
* @throws UnsupportedOperationException if the specified list or its
* list-iterator does not support the {@code set} operation.
*/
public static void shuffle(List<?> list, Random rnd) {
shuffle(list, (RandomGenerator) rnd);
}
/**
* Randomly permute the specified list using the specified source of
* randomness. All permutations occur with equal likelihood
@ -442,8 +461,8 @@ public class Collections {
* portion of the list that runs from the first element to the current
* position, inclusive.<p>
*
* This method runs in linear time. If the specified list does not
* implement the {@link RandomAccess} interface and is large, this
* @implSpec This method runs in linear time. If the specified list does
* not implement the {@link RandomAccess} interface and is large, this
* implementation dumps the specified list into an array before shuffling
* it, and dumps the shuffled array back into the list. This avoids the
* quadratic behavior that would result from shuffling a "sequential
@ -453,9 +472,10 @@ public class Collections {
* @param rnd the source of randomness to use to shuffle the list.
* @throws UnsupportedOperationException if the specified list or its
* list-iterator does not support the {@code set} operation.
* @since 21
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static void shuffle(List<?> list, Random rnd) {
public static void shuffle(List<?> list, RandomGenerator rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i=size; i>1; i--)