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()); opponents.remove(player.getId());
} }
if (!opponents.isEmpty()) { if (!opponents.isEmpty()) {
permanent.attachTo(RandomUtil.randomFromSet(opponents), source, game); permanent.attachTo(RandomUtil.randomFromCollection(opponents), source, game);
} }
return true; return true;
} }

View file

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

View file

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