aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/comparator
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2015-07-30 10:37:54 +0200
committerEduardo Pedroni <ep625@york.ac.uk>2015-07-30 10:37:54 +0200
commit41981711f0240976c4fffc861995a8f2efebe191 (patch)
treec0bacae01b855c3be0e64a4d06e1945ea7bd0198 /src/eu/equalparts/cardbase/comparator
parent3cb8dad3917cad4df95340253db5f5b743844f58 (diff)
Refactored comparators, did a little bit of CLI testing but will need to look into mocking
Diffstat (limited to 'src/eu/equalparts/cardbase/comparator')
-rw-r--r--src/eu/equalparts/cardbase/comparator/CardComparator.java24
-rw-r--r--src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java11
-rw-r--r--src/eu/equalparts/cardbase/comparator/SpecialFields.java4
3 files changed, 19 insertions, 20 deletions
diff --git a/src/eu/equalparts/cardbase/comparator/CardComparator.java b/src/eu/equalparts/cardbase/comparator/CardComparator.java
index 9f90e00..7173109 100644
--- a/src/eu/equalparts/cardbase/comparator/CardComparator.java
+++ b/src/eu/equalparts/cardbase/comparator/CardComparator.java
@@ -37,7 +37,7 @@ public class CardComparator implements Comparator<Card> {
/**
* The comparison delegate to use for the specified field.
*/
- private BiFunction<Comparable, Comparable, Integer> selectedComparisonDelegate = this::defaultComparison;
+ private BiFunction<Comparable, Comparable, Integer> comparisonDelegate = (field1, field2) -> field1.compareTo(field2);
/**
* Creates a new comparator for the specified field only. This class
@@ -58,9 +58,9 @@ public class CardComparator implements Comparator<Card> {
// if annotated with a special comparator, set the comparison delegate here
for (Annotation annotation : fieldToCompare.getAnnotations()) {
if (annotation.annotationType() == DirtyNumber.class) {
- this.selectedComparisonDelegate = ComparatorDelegates::compareDirtyNumber;
+ this.comparisonDelegate = ComparatorDelegates::compareDirtyNumber;
} else if (annotation.annotationType() == Rarity.class) {
- this.selectedComparisonDelegate = ComparatorDelegates::compareRarity;
+ this.comparisonDelegate = ComparatorDelegates::compareRarity;
}
}
} else {
@@ -72,7 +72,7 @@ public class CardComparator implements Comparator<Card> {
public int compare(Card o1, Card o2) {
/*
* we've already checked that the field is self comparable,
- * so we are now free to cast to whatever type it is and compare.
+ * so we are now free to cast to comparable
*/
try {
Comparable field1 = (Comparable) fieldToCompare.get(o1);
@@ -88,7 +88,7 @@ public class CardComparator implements Comparator<Card> {
} else if (field2 == null) {
return 1;
} else {
- return selectedComparisonDelegate.apply(field1, field2);
+ return comparisonDelegate.apply(field1, field2);
}
} catch (IllegalArgumentException e) {
@@ -102,20 +102,6 @@ public class CardComparator implements Comparator<Card> {
// fallback, this shouldn't happen
return 0;
}
-
- /**
- * The standard comparison operation, which uses the field's own {@code compareTo()}
- * method.
- *
- * @param field1 the first object to be compared.
- * @param field2 the second object to be compared.
- * @return a negative integer, zero, or a positive integer as the
- * first argument is less than, equal to, or greater than the
- * second.
- */
- private int defaultComparison(Comparable field1, Comparable field2) {
- return field1.compareTo(field2);
- }
/**
* Use reflection to determine if the specified class can be compared with itself.
diff --git a/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java b/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java
index 7bf09ac..cede956 100644
--- a/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java
+++ b/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java
@@ -8,6 +8,17 @@ final class ComparatorDelegates {
/*
* Delegates
*/
+ /**
+ * Compares two dirty numbers, that is, strings containing a single number
+ * and, optionally, additional non-numerical characters.
+ * For instance, when comparing "150a" and "36", this comparator delegate
+ * first compares 150 with 36 using the standard integer comparison, and
+ * returns an integer > 1 because 150 > 36.
+ *
+ * @param field1 the first field to compare.
+ * @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) {
// this assumes that the format is uninterrupted numbers and letters
String number1 = ((String) field1).replaceAll("[^0-9]+", "");
diff --git a/src/eu/equalparts/cardbase/comparator/SpecialFields.java b/src/eu/equalparts/cardbase/comparator/SpecialFields.java
index f17ccc6..692f774 100644
--- a/src/eu/equalparts/cardbase/comparator/SpecialFields.java
+++ b/src/eu/equalparts/cardbase/comparator/SpecialFields.java
@@ -4,7 +4,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
-public class SpecialFields {
+public final class SpecialFields {
+
+ private SpecialFields() {}
@Retention(RetentionPolicy.RUNTIME)
public @interface DirtyNumber {}