diff --git a/src/main/java/forge/game/GameFormat.java b/src/main/java/forge/game/GameFormat.java index ec07aad5c60..94272753fe8 100644 --- a/src/main/java/forge/game/GameFormat.java +++ b/src/main/java/forge/game/GameFormat.java @@ -20,6 +20,7 @@ package forge.game; import java.util.Collections; import java.util.List; +import com.beust.jcommander.internal.Lists; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Predicates; @@ -39,6 +40,9 @@ public class GameFormat { protected final List allowedSetCodes; protected final List bannedCardNames; + protected final List allowedSetCodes_ro; + protected final List bannedCardNames_ro; + protected final transient Predicate filterRules; protected final transient Predicate filterPrinted; @@ -54,8 +58,12 @@ public class GameFormat { */ public GameFormat(final String fName, final List sets, final List bannedCards) { this.name = fName; - this.allowedSetCodes = Collections.unmodifiableList(sets); - this.bannedCardNames = Collections.unmodifiableList(bannedCards); + this.allowedSetCodes = Lists.newArrayList(sets); + this.bannedCardNames = Lists.newArrayList(bannedCards); + + this.allowedSetCodes_ro = Collections.unmodifiableList(allowedSetCodes); + this.bannedCardNames_ro = Collections.unmodifiableList(bannedCardNames); + this.filterRules = this.buildFilterRules(); this.filterPrinted = this.buildFilterPritned(); } @@ -89,7 +97,7 @@ public class GameFormat { * @return list of allowed set codes */ public List getAllowedSetCodes() { - return this.allowedSetCodes; + return this.allowedSetCodes_ro; } /** @@ -98,7 +106,7 @@ public class GameFormat { * @return list of banned card names */ public List getBannedCardNames() { - return this.bannedCardNames; + return this.bannedCardNames_ro; } /** diff --git a/src/main/java/forge/quest/data/GameFormatQuest.java b/src/main/java/forge/quest/data/GameFormatQuest.java index 7ff17f065b6..00ab56be056 100644 --- a/src/main/java/forge/quest/data/GameFormatQuest.java +++ b/src/main/java/forge/quest/data/GameFormatQuest.java @@ -21,15 +21,11 @@ import java.util.Collections; import java.util.List; import java.util.ArrayList; - -import com.google.common.base.Function; import com.google.common.base.Predicate; import forge.Singletons; import forge.card.CardEdition; -import forge.card.CardRulesPredicates; import forge.game.GameFormat; -import forge.item.CardPrinted; /** @@ -37,54 +33,8 @@ import forge.item.CardPrinted; * is not immutable. This class is necessary because we may wish to update * its contents in certain circumstances, and it was safer to create a new * class than to make the preset game formats modifiable. - * - * N.B! This class MUST NOT INHERIT GameFormat.java as long as that class - * has its data in immutable lists, otherwise this class WILL NOT WORK! - * The whole purpose of this class is to allow modification of the lists, - * otherwise the plain GameFormat.java would have sufficed! */ -public final class GameFormatQuest { - - private final String name; - // contains allowed sets, when empty allows all sets - private final List allowedSetCodes; - private final List bannedCardNames; - - private Predicate filterRules; - private Predicate filterPrinted; - - /** - * Instantiates a new game format based on an existing format. - * - * @param toCopy - * an existing format - */ - public GameFormatQuest(final GameFormat toCopy) { - this.name = new String(toCopy.getName()); - - this.allowedSetCodes = new ArrayList(); - allowedSetCodes.addAll(toCopy.getAllowedSetCodes()); - - this.bannedCardNames = new ArrayList(); - bannedCardNames.addAll(toCopy.getBannedCardNames()); - - this.filterRules = this.buildFilterRules(); - this.filterPrinted = this.buildFilterPrinted(); - } - - /** - * Instantiates an empty new game format. - * - * @param newName - * String, the name - */ - public GameFormatQuest(final String newName) { - this.name = new String(newName); - - this.allowedSetCodes = new ArrayList(); - this.bannedCardNames = new ArrayList(); - } - +public final class GameFormatQuest extends GameFormat { /** * Instantiates a new game format based on two lists. @@ -97,41 +47,29 @@ public final class GameFormatQuest { * List, these will be the banned cards */ public GameFormatQuest(final String newName, final List setsToAllow, final List cardsToBan) { - this.name = new String(newName); - - this.allowedSetCodes = new ArrayList(); - allowedSetCodes.addAll(setsToAllow); - - this.bannedCardNames = new ArrayList(); - bannedCardNames.addAll(cardsToBan); - - this.filterRules = this.buildFilterRules(); - this.filterPrinted = this.buildFilterPrinted(); + super(newName, setsToAllow, cardsToBan); } - private Predicate buildFilterPrinted() { - final Predicate banNames = CardPrinted.Predicates.namesExcept(this.bannedCardNames); - if (this.allowedSetCodes.isEmpty()) { - return banNames; - } - return com.google.common.base.Predicates.and(banNames, CardPrinted.Predicates.printedInSets(this.allowedSetCodes, true)); + /** + * Instantiates a new game format based on an existing format. + * + * @param toCopy + * an existing format + */ + public GameFormatQuest(final GameFormat toCopy) { + this(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames()); } - private Predicate buildFilterRules() { - final Predicate banNames = CardPrinted.Predicates.namesExcept(this.bannedCardNames); - if (this.allowedSetCodes.isEmpty()) { - return banNames; - } - return com.google.common.base.Predicates.and(banNames, com.google.common.base.Predicates.compose(CardRulesPredicates.wasPrintedInSets(this.allowedSetCodes), CardPrinted.FN_GET_RULES)); - } /** * * Updates the filters based on the current list data. */ public void updateFilters() { - this.filterRules = this.buildFilterRules(); - this.filterPrinted = this.buildFilterPrinted(); + // nothing to do here. + // predicates hold references to lists and thus get auto updated. + + // remove this method after reading. } /** @@ -154,51 +92,6 @@ public final class GameFormatQuest { } } - /** - * @param banCard - * the card to ban - */ - public void addBannedCard(final String banCard) { - if (!bannedCardNames.contains(banCard)) { - bannedCardNames.add(banCard); - } - } - - /** - * Gets the name. - * - * @return the name - */ - public String getName() { - return this.name; - } - - /** - * Gets the filter rules. - * - * @return the filter rules - */ - public Predicate getFilterRules() { - return this.filterRules; - } - - /** - * Gets the filter printed. - * - * @return the filter printed - */ - public Predicate getFilterPrinted() { - return this.filterPrinted; - } - - /** - * Gets the set list. - * - * @return unmodifiable list of allowed set codes - */ - public List getAllowedSetCodes() { - return Collections.unmodifiableList(this.allowedSetCodes); - } /** * Get the list of excluded sets. @@ -235,26 +128,6 @@ public final class GameFormatQuest { updateFilters(); } - /** - * Gets the banned cards. - * - * @return unmodifiable list of banned card names - */ - public List getBannedCardNames() { - return Collections.unmodifiableList(this.bannedCardNames); - } - - /** - * Checks if is sets the legal. - * - * @param setCode - * the set code - * @return true, if is sets the legal - */ - public boolean isSetLegal(final String setCode) { - return this.allowedSetCodes.isEmpty() || this.allowedSetCodes.contains(setCode); - } - /** * Checks if the current format contains sets with snow-land (horrible hack...). * @return boolean, contains snow-land sets. @@ -264,17 +137,6 @@ public final class GameFormatQuest { return (this.isSetLegal("ICE") || this.isSetLegal("CSP")); } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return this.name + " (format)"; - } - - /** * The Class Predicates. */ @@ -303,11 +165,5 @@ public final class GameFormatQuest { } } - public static final Function FN_GET_NAME = new Function() { - @Override - public String apply(GameFormat arg1) { - return arg1.getName(); - } - }; }