From 56dc0db523156c6ff77d9212983c51b531250733 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 11 Jun 2015 14:31:39 +0200 Subject: Moved away from reflection for sorting, now looking into using a hashmap instead of a list --- .../cardbase/comparators/CardComparator.java | 102 --------------------- .../cardbase/comparators/CardComparators.java | 76 +++++++++++++++ 2 files changed, 76 insertions(+), 102 deletions(-) delete mode 100644 src/eu/equalparts/cardbase/comparators/CardComparator.java create mode 100644 src/eu/equalparts/cardbase/comparators/CardComparators.java (limited to 'src/eu/equalparts/cardbase/comparators') 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. - *

- * 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. - *

- * 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 { - - /** - * 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} where T is its own type. - *
- * 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 { + + 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 { + + 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 { + + 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); + } + } + } + +} -- cgit v1.2.3