diff options
Diffstat (limited to 'src/eu/equalparts/cardbase')
-rw-r--r-- | src/eu/equalparts/cardbase/Cardbase.java | 27 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/comparators/CardComparator.java | 102 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/comparators/CardComparators.java | 76 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/data/Card.java | 2 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/standalone/CardbaseCLI.java | 11 |
5 files changed, 86 insertions, 132 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 14afc10..d842031 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -1,7 +1,6 @@ package eu.equalparts.cardbase; import java.io.File; import java.io.IOException; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -11,7 +10,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; -import eu.equalparts.cardbase.comparators.CardComparator; +import eu.equalparts.cardbase.comparators.CardComparators; import eu.equalparts.cardbase.data.Card; import eu.equalparts.cardbase.utils.JSON; @@ -119,7 +118,7 @@ public class Cardbase { * This method is intended to allow iteration directly on the list of cards, * while at the same time retaining control over the insert and remove procedures. * The returned {@code List} is a read-only; trying to modify its structure will - * result in a {@code UnsupportedOperationException}. + * result in an {@code UnsupportedOperationException}. * * @return an unmodifiable list of all the cards in the cardbase. */ @@ -127,22 +126,8 @@ public class Cardbase { return Collections.unmodifiableList(cards); } - /** - * Sorts the cardbase by the specified field. The field must be specified exactly - * as it is defined in {@code Card}, case-sensitive. It must also be comparable to - * itself, as {@code String} and {@code Integer} are. - * - * @param fieldName the declared name of the field to be used for sorting. - * @return true if the sort was successful, false if no such field was found. - */ - public boolean sortBy(String fieldName) { - for (Field field : Card.class.getDeclaredFields()) { - if (field.getName().equals(fieldName)) { - cards.sort(new CardComparator(field)); - return true; - } - } - return false; + public void sortByName() { + cards.sort(new CardComparators.NameComparator()); } /** @@ -161,4 +146,8 @@ public class Cardbase { return null; } + + private String makeHash(String setCode, String number) { + return setCode + number; + } } diff --git a/src/eu/equalparts/cardbase/comparators/CardComparator.java b/src/eu/equalparts/cardbase/comparators/CardComparator.java deleted file mode 100644 index 3640c8b..0000000 --- a/src/eu/equalparts/cardbase/comparators/CardComparator.java +++ /dev/null @@ -1,102 +0,0 @@ -package eu.equalparts.cardbase.comparators; - -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.util.Comparator; - -import eu.equalparts.cardbase.Cardbase; -import eu.equalparts.cardbase.data.Card; - -/** - * I'm new to this reflection business, so bear with me. - * <br><br> - * The idea here is to avoid having to write one class - * for each comparable field in {@code Card}. The program - * can dynamically instantiate them as cards are compared - * by different fields. - * <br><br> - * This class uses reflection to determine if the specified - * field is comparable with itself upon construction, and throws - * an {@code IllegalArgumentException} if that is not the case. - * - * @author Eduardo Pedroni - * - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public class CardComparator implements Comparator<Card> { - - /** - * The field being compared. - */ - private Field fieldToCompare; - - /** - * Creates a new comparator for the specified field only. This class - * will only be constructed successfully if the field comes from - * {@code Card} and can be compared to itself (i.e. implements - * {@code Comparable<T>} where T is its own type. - * <br> - * For reference, {@code String} and {@code Integer} are both self comparable. - * - * @param fieldToCompare the field this comparator will use to compare cards, as declared in {@code Card}. - */ - public CardComparator(Field fieldToCompare) { - if (fieldToCompare.getDeclaringClass().equals(Card.class) && - isSelfComparable(fieldToCompare.getType())) { - - this.fieldToCompare = fieldToCompare; - } else { - System.out.println(fieldToCompare.isAccessible()); - System.out.println(fieldToCompare.getDeclaringClass().equals(Card.class)); - System.out.println(isSelfComparable(fieldToCompare.getType())); - throw new IllegalArgumentException("The field provided is not valid."); - } - } - - @Override - public int compare(Card o1, Card o2) { - try { - /* - * we've already checked that the field is self comparable, - * so we are now free to cast to whatever type it is and compare. - */ - Comparable field1 = (Comparable) fieldToCompare.get(o1); - Comparable field2 = (Comparable) fieldToCompare.get(o2); - - return field1.compareTo(field2); - - } catch (IllegalArgumentException e) { - System.out.println("Error: class Card does not define field" + fieldToCompare.getName() + "."); - if (Cardbase.DEBUG) e.printStackTrace(); - } catch (IllegalAccessException e) { - System.out.println("Error: field " + fieldToCompare.getName() + " in Card is not visible."); - if (Cardbase.DEBUG) e.printStackTrace(); - } - - // not comparable, this should never happen - return 0; - } - - /** - * Use reflection to determine if the specified class can be compared with itself. - * - * @param type the type to analyse. - * @return true if the type can be compared to itself using {@code compareTo()}, false otherwise. - */ - private boolean isSelfComparable(Class<?> type) { - - // go through all interfaces implemented by this class - for (Type implementedInterface : type.getGenericInterfaces()) { - // check if any parameterised interface found is "Comparable" - if (implementedInterface instanceof ParameterizedType) { - ParameterizedType genericInterface = (ParameterizedType) implementedInterface; - if (genericInterface.getRawType().equals(Comparable.class)) { - // check that the type argument of comparable is the same as the field type itself - return genericInterface.getActualTypeArguments()[0].equals(type); - } - } - } - return false; - } -} diff --git a/src/eu/equalparts/cardbase/comparators/CardComparators.java b/src/eu/equalparts/cardbase/comparators/CardComparators.java new file mode 100644 index 0000000..99f7ded --- /dev/null +++ b/src/eu/equalparts/cardbase/comparators/CardComparators.java @@ -0,0 +1,76 @@ +package eu.equalparts.cardbase.comparators; + +import java.util.Comparator; + +import eu.equalparts.cardbase.data.Card; + +public final class CardComparators { + + private CardComparators() {} + + public enum Order { + NATURAL, REVERSE; + } + + public static class NameComparator implements Comparator<Card> { + + private Order order = Order.NATURAL; + + public NameComparator() {} + + public NameComparator(Order order) { + this.order = order; + } + + @Override + public int compare(Card o1, Card o2) { + if (order == Order.NATURAL) { + return o1.name.compareTo(o2.name); + } else { + return o2.name.compareTo(o1.name); + } + + } + } + + public static class LayoutComparator implements Comparator<Card> { + + private Order order = Order.NATURAL; + + public LayoutComparator() {} + + public LayoutComparator(Order order) { + this.order = order; + } + + @Override + public int compare(Card o1, Card o2) { + if (order == Order.NATURAL) { + return o1.layout.compareTo(o2.layout); + } else { + return o2.layout.compareTo(o1.layout); + } + } + } + + public static class ManaCostComparator implements Comparator<Card> { + + private Order order = Order.NATURAL; + + public ManaCostComparator() {} + + public ManaCostComparator(Order order) { + this.order = order; + } + + @Override + public int compare(Card o1, Card o2) { + if (order == Order.NATURAL) { + return o1.manaCost.compareTo(o2.manaCost); + } else { + return o2.manaCost.compareTo(o1.manaCost); + } + } + } + +} diff --git a/src/eu/equalparts/cardbase/data/Card.java b/src/eu/equalparts/cardbase/data/Card.java index 03e7dea..3820223 100644 --- a/src/eu/equalparts/cardbase/data/Card.java +++ b/src/eu/equalparts/cardbase/data/Card.java @@ -4,8 +4,8 @@ import java.util.List; public class Card { - public String layout = ""; public String name = ""; + public String layout = ""; public List<String> names; public String manaCost = ""; public Integer cmc = 0; diff --git a/src/eu/equalparts/cardbase/standalone/CardbaseCLI.java b/src/eu/equalparts/cardbase/standalone/CardbaseCLI.java index 108b753..f491f6c 100644 --- a/src/eu/equalparts/cardbase/standalone/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/standalone/CardbaseCLI.java @@ -416,16 +416,7 @@ public class CardbaseCLI { * @param args the ordering to sort by. */ public void sort(String[] args) { - if (args.length > 0) { - String fieldName = args[0]; - if (cardbase.sortBy(fieldName)) { - System.out.println("Cardbase sorted by " + fieldName); - } else { - System.out.println("Unknown field: " + fieldName); - } - } else { - System.out.println("Please enter the field to use for sorting."); - } + cardbase.sortByName(); } /** |