Formats Comparable sorts formats according to number of sets they include

This commit is contained in:
Maxmtg
2013-02-17 15:02:48 +00:00
parent 7ee73e9b61
commit 45e3fd686d
4 changed files with 29 additions and 28 deletions

View File

@@ -21,6 +21,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import forge.game.GameFormat; import forge.game.GameFormat;
import forge.util.FileSection;
import forge.util.StorageView; import forge.util.StorageView;
import forge.util.StorageReaderFile; import forge.util.StorageReaderFile;
@@ -93,30 +94,27 @@ public final class FormatCollection extends StorageView<GameFormat> {
*/ */
@Override @Override
protected GameFormat read(String line) { protected GameFormat read(String line) {
String name = null; final List<String> sets = new ArrayList<String>(); // default: all sets allowed
final List<String> sets = new ArrayList<String>(); // default: all
// sets
// allowed
final List<String> bannedCards = new ArrayList<String>(); // default: final List<String> bannedCards = new ArrayList<String>(); // default:
// nothing // nothing
// banned // banned
final String[] sParts = line.trim().split("\\|"); FileSection section = FileSection.parse(line, ":", "|");
for (final String sPart : sParts) { String name = section.get("name");
final String[] kv = sPart.split(":", 2); int index = section.getInt("index", 0);
final String key = kv[0].toLowerCase(); String strSets = section.get("sets");
if ("name".equals(key)) { if ( null != strSets ) {
name = kv[1]; sets.addAll(Arrays.asList(strSets.split(", ")));
} else if ("sets".equals(key)) {
sets.addAll(Arrays.asList(kv[1].split(", ")));
} else if ("banned".equals(key)) {
bannedCards.addAll(Arrays.asList(kv[1].split("; ")));
}
} }
String strCars = section.get("banned");
if ( strCars != null ) {
bannedCards.addAll(Arrays.asList(strCars.split("; ")));
}
if (name == null) { if (name == null) {
throw new RuntimeException("Format must have a name! Check formats.txt file"); 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);
} }

View File

@@ -47,6 +47,8 @@ public class GameFormat implements Comparable<GameFormat> {
protected final transient Predicate<CardPrinted> filterRules; protected final transient Predicate<CardPrinted> filterRules;
protected final transient Predicate<CardPrinted> filterPrinted; protected final transient Predicate<CardPrinted> filterPrinted;
private final int index;
/** /**
* Instantiates a new game format. * Instantiates a new game format.
* *
@@ -58,6 +60,11 @@ public class GameFormat implements Comparable<GameFormat> {
* the banned cards * the banned cards
*/ */
public GameFormat(final String fName, final Iterable<String> sets, final List<String> bannedCards) { public GameFormat(final String fName, final Iterable<String> sets, final List<String> bannedCards) {
this(fName, sets, bannedCards, 0);
}
public GameFormat(final String fName, final Iterable<String> sets, final List<String> bannedCards, int compareIdx) {
this.index = compareIdx;
this.name = fName; this.name = fName;
this.allowedSetCodes = Lists.newArrayList(sets); this.allowedSetCodes = Lists.newArrayList(sets);
this.bannedCardNames = bannedCards == null ? new ArrayList<String>() : Lists.newArrayList(bannedCards); this.bannedCardNames = bannedCards == null ? new ArrayList<String>() : Lists.newArrayList(bannedCards);
@@ -167,13 +174,11 @@ public class GameFormat implements Comparable<GameFormat> {
if (null == other) { if (null == other) {
return 1; return 1;
} }
if (name == other.name) { return index - other.index;
return 0; }
}
if (null == name) { public int getIndex() {
return -1; return index;
}
return name.compareTo(other.name);
} }
} }

View File

@@ -235,16 +235,14 @@ public class GuiChoose {
public static <T> List<T> sortedGetChoices(final String message, final int min, final int max, final T[] choices, Comparator<T> comparer) { public static <T> List<T> sortedGetChoices(final String message, final int min, final int max, final T[] choices, Comparator<T> comparer) {
// You may create a copy of source array if callers expect the collection to be unchanged // You may create a copy of source array if callers expect the collection to be unchanged
Arrays.sort(choices, comparer); Arrays.sort(choices, comparer);
final ListChooser<T> c = new ListChooser<T>(message, min, max, choices); return getChoices(message, min, max, choices);
return getChoices(c);
} }
// If comparer is NULL, T has to be comparable. Otherwise you'll get an exception from inside the Arrays.sort() routine // If comparer is NULL, T has to be comparable. Otherwise you'll get an exception from inside the Arrays.sort() routine
public static <T> List<T> sortedGetChoices(final String message, final int min, final int max, final List<T> choices, Comparator<T> comparer) { public static <T> List<T> sortedGetChoices(final String message, final int min, final int max, final List<T> choices, Comparator<T> comparer) {
// You may create a copy of source list if callers expect the collection to be unchanged // You may create a copy of source list if callers expect the collection to be unchanged
Collections.sort(choices, comparer); Collections.sort(choices, comparer);
final ListChooser<T> c = new ListChooser<T>(message, min, max, choices); return getChoices(message, min, max, choices);
return getChoices(c);
} }
} }

View File

@@ -65,7 +65,7 @@ public final class GameFormatQuest extends GameFormat {
* @param allowSetUnlocks * @param allowSetUnlocks
*/ */
public GameFormatQuest(final GameFormat toCopy, boolean 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; allowUnlocks = allowSetUnlocks;
} }