From 41981711f0240976c4fffc861995a8f2efebe191 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 30 Jul 2015 10:37:54 +0200 Subject: Refactored comparators, did a little bit of CLI testing but will need to look into mocking --- .../cardbase/comparator/CardComparator.java | 24 +++++----------------- .../cardbase/comparator/ComparatorDelegates.java | 11 ++++++++++ .../cardbase/comparator/SpecialFields.java | 4 +++- 3 files changed, 19 insertions(+), 20 deletions(-) (limited to 'src/eu/equalparts/cardbase/comparator') 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 { /** * The comparison delegate to use for the specified field. */ - private BiFunction selectedComparisonDelegate = this::defaultComparison; + private BiFunction 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 { // 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 { 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 { } 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 { // 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 field1, Comparable 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 {} -- cgit v1.2.3