aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/comparator
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/equalparts/cardbase/comparator')
-rw-r--r--src/eu/equalparts/cardbase/comparator/CardComparator.java45
-rw-r--r--src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java13
2 files changed, 19 insertions, 39 deletions
diff --git a/src/eu/equalparts/cardbase/comparator/CardComparator.java b/src/eu/equalparts/cardbase/comparator/CardComparator.java
index c3ff736..d2cd0c1 100644
--- a/src/eu/equalparts/cardbase/comparator/CardComparator.java
+++ b/src/eu/equalparts/cardbase/comparator/CardComparator.java
@@ -2,12 +2,11 @@ package eu.equalparts.cardbase.comparator;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
import java.util.Comparator;
import java.util.function.BiFunction;
-import eu.equalparts.cardbase.cards.Card;
+import eu.equalparts.cardbase.card.Card;
+import eu.equalparts.cardbase.cardfield.CardField;
import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber;
import eu.equalparts.cardbase.comparator.SpecialFields.Rarity;
@@ -26,7 +25,7 @@ import eu.equalparts.cardbase.comparator.SpecialFields.Rarity;
* @author Eduardo Pedroni
*
*/
-@SuppressWarnings({"rawtypes", "unchecked"})
+//@SuppressWarnings({"rawtypes", "unchecked"})
public class CardComparator implements Comparator<Card> {
/**
@@ -36,7 +35,7 @@ public class CardComparator implements Comparator<Card> {
/**
* The comparison delegate to use for the specified field.
*/
- private BiFunction<Comparable, Comparable, Integer> comparisonDelegate = (field1, field2) -> field1.compareTo(field2);
+ private BiFunction<CardField, CardField, Integer> comparisonDelegate = (field1, field2) -> field1.compareTo(field2);
/**
* Creates a new comparator for the specified field only. This class
@@ -46,11 +45,13 @@ public class CardComparator implements Comparator<Card> {
* <br>
* For reference, {@code String} and {@code Integer} are both self comparable.
*
+ * TODO comment
+ *
* @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())) {
+ CardField.class.isAssignableFrom(fieldToCompare.getType())) {
this.fieldToCompare = fieldToCompare;
@@ -74,17 +75,17 @@ public class CardComparator implements Comparator<Card> {
* so we are now free to cast to comparable
*/
try {
- Comparable field1 = (Comparable) fieldToCompare.get(o1);
- Comparable field2 = (Comparable) fieldToCompare.get(o2);
+ CardField field1 = (CardField) fieldToCompare.get(o1);
+ CardField field2 = (CardField) fieldToCompare.get(o2);
// if either or both fields' values are null, skip delegation altogether since delegates are not required to deal with null values
- if (field1 == null) {
- if (field2 == null) {
+ if (field1.get() == null) {
+ if (field2.get() == null) {
return 0;
} else {
return -1;
}
- } else if (field2 == null) {
+ } else if (field2.get() == null) {
return 1;
} else {
return comparisonDelegate.apply(field1, field2);
@@ -101,26 +102,4 @@ public class CardComparator implements Comparator<Card> {
// fallback, this shouldn't 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/comparator/ComparatorDelegates.java b/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java
index cede956..292cd24 100644
--- a/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java
+++ b/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java
@@ -1,5 +1,6 @@
package eu.equalparts.cardbase.comparator;
+import eu.equalparts.cardbase.cardfield.CardField;
final class ComparatorDelegates {
@@ -19,10 +20,10 @@ final class ComparatorDelegates {
* @param field2 the second field to compare.
* @return a value smaller than, equal to or greater than 0 according to the standard comparison convention.
*/
- public static Integer compareDirtyNumber(Comparable<String> field1, Comparable<String> field2) {
+ public static Integer compareDirtyNumber(CardField<String> field1, CardField<String> field2) {
// this assumes that the format is uninterrupted numbers and letters
- String number1 = ((String) field1).replaceAll("[^0-9]+", "");
- String number2 = ((String) field2).replaceAll("[^0-9]+", "");
+ String number1 = field1.get().replaceAll("[^0-9]+", "");
+ String number2 = field2.get().replaceAll("[^0-9]+", "");
Integer int1 = number1.matches("[0-9]+") ? Integer.parseInt(number1) : null;
Integer int2 = number2.matches("[0-9]+") ? Integer.parseInt(number2) : null;
@@ -48,10 +49,10 @@ final class ComparatorDelegates {
return letter1.compareTo(letter2);
}
- public static Integer compareRarity(Comparable<String> field1, Comparable<String> field2) {
+ public static Integer compareRarity(CardField<String> field1, CardField<String> field2) {
// assign numerical values based on the different possibilities and compare those instead
- Integer value1 = getRarityValue((String) field1);
- Integer value2 = getRarityValue((String) field2);
+ Integer value1 = getRarityValue(field1.get());
+ Integer value2 = getRarityValue(field2.get());
return value1.compareTo(value2);
}