Guava migration - Preliminary

This commit is contained in:
Jetz
2024-09-02 12:03:59 -04:00
parent 17ffe17e9d
commit b46c1deeb4
5 changed files with 16 additions and 17 deletions

View File

@@ -196,7 +196,7 @@ public final class CardRulesPredicates {
try { try {
return CardRulesPredicates.coreType(isEqual, Enum.valueOf(CardType.CoreType.class, what)); return CardRulesPredicates.coreType(isEqual, Enum.valueOf(CardType.CoreType.class, what));
} catch (final Exception e) { } catch (final Exception e) {
return com.google.common.base.Predicates.alwaysFalse(); return x -> false;
} }
} }
@@ -226,7 +226,7 @@ public final class CardRulesPredicates {
try { try {
return CardRulesPredicates.superType(isEqual, Enum.valueOf(CardType.Supertype.class, what)); return CardRulesPredicates.superType(isEqual, Enum.valueOf(CardType.Supertype.class, what));
} catch (final Exception e) { } catch (final Exception e) {
return com.google.common.base.Predicates.alwaysFalse(); return x -> false;
} }
} }

View File

@@ -123,14 +123,12 @@ public class DeckHints {
for (Pair<Type, String> pair : filters) { for (Pair<Type, String> pair : filters) {
Type type = pair.getLeft(); Type type = pair.getLeft();
String param = pair.getRight(); String param = pair.getRight();
Iterable<PaperCard> cards = getCardsForFilter(cardList, type, param); List<PaperCard> cards = getCardsForFilter(cardList, type, param);
if (cards != null) { // if a type is used more than once intersect respective matches
// if a type is used more than once intersect respective matches if (ret.containsKey(type)) {
if (ret.containsKey(type)) { cards.retainAll(new FCollection<>(ret.get(type)));
Iterables.retainAll(cards, new FCollection<>(ret.get(type)));
}
ret.put(type, cards);
} }
ret.put(type, cards);
} }
return ret; return ret;
} }
@@ -166,7 +164,7 @@ public class DeckHints {
return pair; return pair;
} }
private Iterable<PaperCard> getCardsForFilter(Iterable<PaperCard> cardList, Type type, String param) { private List<PaperCard> getCardsForFilter(Iterable<PaperCard> cardList, Type type, String param) {
List<PaperCard> cards = new ArrayList<>(); List<PaperCard> cards = new ArrayList<>();
// this is case ABILITY, but other types can also use this when the implicit parsing would miss // this is case ABILITY, but other types can also use this when the implicit parsing would miss

View File

@@ -21,6 +21,7 @@ import com.esotericsoftware.minlog.Log;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.collect.*; import com.google.common.collect.*;
import com.google.common.collect.Iterables;
import forge.GameCommand; import forge.GameCommand;
import forge.StaticData; import forge.StaticData;
import forge.card.*; import forge.card.*;

View File

@@ -5,6 +5,7 @@ import com.badlogic.gdx.utils.Json;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import forge.StaticData; import forge.StaticData;
import forge.adventure.data.ConfigData; import forge.adventure.data.ConfigData;
import forge.adventure.data.GeneratedDeckData; import forge.adventure.data.GeneratedDeckData;
@@ -804,15 +805,15 @@ public class CardUtil {
} }
public static PaperCard getCardByName(String cardName) { public static PaperCard getCardByName(String cardName) {
List<PaperCard> validCards = Arrays.asList(Iterables.toArray(Iterables.filter(getFullCardPool(Config.instance().getSettingData().useAllCardVariants), List<PaperCard> validCards = Lists.newArrayList(Iterables.filter(getFullCardPool(Config.instance().getSettingData().useAllCardVariants),
input -> input.getCardName().equals(cardName)), PaperCard.class)); input -> input.getCardName().equals(cardName)));
return validCards.get(Current.world().getRandom().nextInt(validCards.size())); return validCards.get(Current.world().getRandom().nextInt(validCards.size()));
} }
public static PaperCard getCardByNameAndEdition(String cardName, String edition) { public static PaperCard getCardByNameAndEdition(String cardName, String edition) {
List<PaperCard> validCards = Arrays.asList(Iterables.toArray(Iterables.filter(getFullCardPool(Config.instance().getSettingData().useAllCardVariants), List<PaperCard> validCards = Lists.newArrayList(Iterables.filter(getFullCardPool(Config.instance().getSettingData().useAllCardVariants),
input -> input.getCardName().equals(cardName) && input.getEdition().equals(edition)), PaperCard.class)); input -> input.getCardName().equals(cardName) && input.getEdition().equals(edition)));
if (validCards.isEmpty()) { if (validCards.isEmpty()) {
System.err.println("Unexpected behavior: tried to call getCardByNameAndEdition for card " + cardName + " from the edition " + edition + ", but didn't find it in the DB. A random existing instance will be returned."); System.err.println("Unexpected behavior: tried to call getCardByNameAndEdition for card " + cardName + " from the edition " + edition + ", but didn't find it in the DB. A random existing instance will be returned.");

View File

@@ -449,7 +449,7 @@ public final class BoosterUtils {
*/ */
public static Predicate<CardRules> parseRulesLimitation(final String input) { public static Predicate<CardRules> parseRulesLimitation(final String input) {
if (null == input || "random".equalsIgnoreCase(input)) { if (null == input || "random".equalsIgnoreCase(input)) {
return Predicates.alwaysTrue(); return null;
} }
if (input.equalsIgnoreCase("black")) return CardRulesPredicates.Presets.IS_BLACK; if (input.equalsIgnoreCase("black")) return CardRulesPredicates.Presets.IS_BLACK;
@@ -491,8 +491,7 @@ public final class BoosterUtils {
if (temp.length > 2) { if (temp.length > 2) {
Predicate<CardRules> cr = parseRulesLimitation(temp[1]); Predicate<CardRules> cr = parseRulesLimitation(temp[1]);
//noinspection RedundantCast if (cr != null) {
if (Predicates.alwaysTrue() != (Object) cr) { // guava has a single instance for always-const predicates
preds.add(Predicates.compose(cr, PaperCard::getRules)); preds.add(Predicates.compose(cr, PaperCard::getRules));
} }
} }