From 45e3fd686d0437f63f280e50b851166b6df7266e Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Sun, 17 Feb 2013 15:02:48 +0000 Subject: [PATCH] Formats Comparable sorts formats according to number of sets they include --- .../java/forge/card/FormatCollection.java | 30 +++++++++---------- src/main/java/forge/game/GameFormat.java | 19 +++++++----- src/main/java/forge/gui/GuiChoose.java | 6 ++-- .../forge/quest/data/GameFormatQuest.java | 2 +- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/main/java/forge/card/FormatCollection.java b/src/main/java/forge/card/FormatCollection.java index 2c3233cb209..3e05222e83c 100644 --- a/src/main/java/forge/card/FormatCollection.java +++ b/src/main/java/forge/card/FormatCollection.java @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import forge.game.GameFormat; +import forge.util.FileSection; import forge.util.StorageView; import forge.util.StorageReaderFile; @@ -93,30 +94,27 @@ public final class FormatCollection extends StorageView { */ @Override protected GameFormat read(String line) { - String name = null; - final List sets = new ArrayList(); // default: all - // sets - // allowed + final List sets = new ArrayList(); // default: all sets allowed final List bannedCards = new ArrayList(); // default: // nothing // banned - final String[] sParts = line.trim().split("\\|"); - for (final String sPart : sParts) { - final String[] kv = sPart.split(":", 2); - final String key = kv[0].toLowerCase(); - if ("name".equals(key)) { - name = kv[1]; - } else if ("sets".equals(key)) { - sets.addAll(Arrays.asList(kv[1].split(", "))); - } else if ("banned".equals(key)) { - bannedCards.addAll(Arrays.asList(kv[1].split("; "))); - } + FileSection section = FileSection.parse(line, ":", "|"); + String name = section.get("name"); + int index = section.getInt("index", 0); + String strSets = section.get("sets"); + if ( null != strSets ) { + sets.addAll(Arrays.asList(strSets.split(", "))); } + String strCars = section.get("banned"); + if ( strCars != null ) { + bannedCards.addAll(Arrays.asList(strCars.split("; "))); + } + if (name == null) { throw new RuntimeException("Format must have a name! Check formats.txt file"); } - return new GameFormat(name, sets, bannedCards); + return new GameFormat(name, sets, bannedCards, index); } diff --git a/src/main/java/forge/game/GameFormat.java b/src/main/java/forge/game/GameFormat.java index 6efc126ec95..9451d5a76fc 100644 --- a/src/main/java/forge/game/GameFormat.java +++ b/src/main/java/forge/game/GameFormat.java @@ -47,6 +47,8 @@ public class GameFormat implements Comparable { protected final transient Predicate filterRules; protected final transient Predicate filterPrinted; + private final int index; + /** * Instantiates a new game format. * @@ -58,6 +60,11 @@ public class GameFormat implements Comparable { * the banned cards */ public GameFormat(final String fName, final Iterable sets, final List bannedCards) { + this(fName, sets, bannedCards, 0); + } + + public GameFormat(final String fName, final Iterable sets, final List bannedCards, int compareIdx) { + this.index = compareIdx; this.name = fName; this.allowedSetCodes = Lists.newArrayList(sets); this.bannedCardNames = bannedCards == null ? new ArrayList() : Lists.newArrayList(bannedCards); @@ -167,13 +174,11 @@ public class GameFormat implements Comparable { if (null == other) { return 1; } - if (name == other.name) { - return 0; - } - if (null == name) { - return -1; - } - return name.compareTo(other.name); + return index - other.index; + } + + public int getIndex() { + return index; } } diff --git a/src/main/java/forge/gui/GuiChoose.java b/src/main/java/forge/gui/GuiChoose.java index 1b712230be7..cb9784894da 100644 --- a/src/main/java/forge/gui/GuiChoose.java +++ b/src/main/java/forge/gui/GuiChoose.java @@ -235,16 +235,14 @@ public class GuiChoose { public static List sortedGetChoices(final String message, final int min, final int max, final T[] choices, Comparator comparer) { // You may create a copy of source array if callers expect the collection to be unchanged Arrays.sort(choices, comparer); - final ListChooser c = new ListChooser(message, min, max, choices); - return getChoices(c); + return getChoices(message, min, max, choices); } // If comparer is NULL, T has to be comparable. Otherwise you'll get an exception from inside the Arrays.sort() routine public static List sortedGetChoices(final String message, final int min, final int max, final List choices, Comparator comparer) { // You may create a copy of source list if callers expect the collection to be unchanged Collections.sort(choices, comparer); - final ListChooser c = new ListChooser(message, min, max, choices); - return getChoices(c); + return getChoices(message, min, max, choices); } } diff --git a/src/main/java/forge/quest/data/GameFormatQuest.java b/src/main/java/forge/quest/data/GameFormatQuest.java index 5cd847e25e1..92cb2e71e24 100644 --- a/src/main/java/forge/quest/data/GameFormatQuest.java +++ b/src/main/java/forge/quest/data/GameFormatQuest.java @@ -65,7 +65,7 @@ public final class GameFormatQuest extends GameFormat { * @param allowSetUnlocks */ public GameFormatQuest(final GameFormat toCopy, boolean allowSetUnlocks) { - super(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames()); + super(toCopy.getName(), toCopy.getAllowedSetCodes(), toCopy.getBannedCardNames(), toCopy.getIndex()); allowUnlocks = allowSetUnlocks; }