com.github.phantomthief.failover.util.RandomListUtils Maven / Gradle / Ivy
package com.github.phantomthief.failover.util;
import static java.lang.Math.min;
import static java.util.Collections.emptyList;
import static java.util.Collections.shuffle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nullable;
/**
* @author w.vela
*/
public class RandomListUtils {
private RandomListUtils() {
throw new UnsupportedOperationException();
}
@Nullable
public static T getRandom(List source) {
if (source == null || source.isEmpty()) {
return null;
}
return source.get(ThreadLocalRandom.current().nextInt(source.size()));
}
public static List getRandom(Collection source, int size) {
if (source == null || source.isEmpty()) {
return emptyList();
}
List newList = new ArrayList<>(source);
shuffle(newList, ThreadLocalRandom.current());
return newList.subList(0, min(newList.size(), size));
}
}