made a util function more generic because why not

This commit is contained in:
Evan Kranzler 2021-08-21 08:56:51 -04:00
parent 14838e670a
commit b9550a7387
3 changed files with 7 additions and 8 deletions

View file

@ -132,7 +132,7 @@ class MaddeningHexEffect extends OneShotEffect {
opponents.remove(player.getId());
}
if (!opponents.isEmpty()) {
permanent.attachTo(RandomUtil.randomFromSet(opponents), source, game);
permanent.attachTo(RandomUtil.randomFromCollection(opponents), source, game);
}
return true;
}

View file

@ -2716,7 +2716,7 @@ public abstract class PlayerImpl implements Player, Serializable {
.stream()
.filter(card -> filter.match(card, source.getSourceId(), getId(), game))
.collect(Collectors.toSet());
Card card = RandomUtil.randomFromSet(cards);
Card card = RandomUtil.randomFromCollection(cards);
if (card == null) {
return false;
}

View file

@ -1,16 +1,15 @@
package mage.util;
import java.awt.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.Random;
import java.util.Set;
/**
* Created by IGOUDT on 5-9-2016.
*/
public final class RandomUtil {
private static Random random = new Random(); // thread safe with seed support
private static final Random random = new Random(); // thread safe with seed support
private RandomUtil() {
}
@ -43,15 +42,15 @@ public final class RandomUtil {
random.setSeed(newSeed);
}
public static <T extends Serializable> T randomFromSet(Set<T> collection) {
public static <T> T randomFromCollection(Collection<T> collection) {
if (collection.size() < 2) {
return collection.stream().findFirst().orElse(null);
}
int rand = nextInt(collection.size());
int count = 0;
for (T currentId : collection) {
for (T current : collection) {
if (count == rand) {
return currentId;
return current;
}
count++;
}