From 8cb35b358563e5a81ad9e4aceb123b85222cf4cc Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 14 Aug 2016 18:09:22 +0200 Subject: Implemented some more filtering tests, sorted out validation and exceptions --- src/eu/equalparts/cardbase/card/Card.java | 1 - .../equalparts/cardbase/cardfield/CardField.java | 4 +- .../cardbase/cardfield/IntegerCardField.java | 21 ++++----- .../cardbase/cardfield/StringCardField.java | 14 +++--- src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 14 +++--- .../cardbase/comparator/CardComparator.java | 12 ++--- .../cardbase/filtering/CardFiltering.java | 53 +++++++++++++++++----- src/eu/equalparts/cardbase/filtering/Filter.java | 16 +++++++ .../equalparts/cardbase/json/CardDeserializer.java | 1 - .../equalparts/cardbase/json/CardSerializer.java | 6 --- src/eu/equalparts/cardbase/utils/InputSanity.java | 27 ----------- src/eu/equalparts/cardbase/utils/Utils.java | 15 ++++++ 12 files changed, 100 insertions(+), 84 deletions(-) create mode 100644 src/eu/equalparts/cardbase/filtering/Filter.java delete mode 100644 src/eu/equalparts/cardbase/utils/InputSanity.java create mode 100644 src/eu/equalparts/cardbase/utils/Utils.java (limited to 'src/eu') diff --git a/src/eu/equalparts/cardbase/card/Card.java b/src/eu/equalparts/cardbase/card/Card.java index 11cfb77..2c896e8 100644 --- a/src/eu/equalparts/cardbase/card/Card.java +++ b/src/eu/equalparts/cardbase/card/Card.java @@ -25,7 +25,6 @@ public class Card { public IntegerCardField loyalty; public IntegerCardField multiverseid; public StringCardField imageName; - public StringCardField watermark; // Not part of upstream JSON public StringCardField setCode; diff --git a/src/eu/equalparts/cardbase/cardfield/CardField.java b/src/eu/equalparts/cardbase/cardfield/CardField.java index 9af775b..e088577 100644 --- a/src/eu/equalparts/cardbase/cardfield/CardField.java +++ b/src/eu/equalparts/cardbase/cardfield/CardField.java @@ -1,5 +1,5 @@ package eu.equalparts.cardbase.cardfield; -import eu.equalparts.cardbase.filtering.CardFiltering.Filter; +import eu.equalparts.cardbase.filtering.Filter; public abstract class CardField> implements Comparable> { private String name; @@ -27,5 +27,5 @@ public abstract class CardField> implements Comparable { @@ -9,22 +8,18 @@ public class IntegerCardField extends CardField { } @Override - public boolean filter(Filter filter, String s) { - switch (filter) { + public boolean filter(Filter filter) throws NumberFormatException { + switch (filter.type) { case CONTAINS: - return get().toString().contains(s); + return get().toString().contains(filter.value); case EQUALS: - return get().toString().equalsIgnoreCase(s); + return get().toString().equalsIgnoreCase(filter.value); case REGEX: - return get().toString().matches(s); + return get().toString().matches(filter.value); case GREATER_THAN: - if (InputSanity.isInteger(s)) { - return get() > Integer.valueOf(s); - } + return get() > Integer.parseInt(filter.value); case SMALLER_THAN: - if (InputSanity.isInteger(s)) { - return get() < Integer.valueOf(s); - } + return get() < Integer.parseInt(filter.value); default: return false; } diff --git a/src/eu/equalparts/cardbase/cardfield/StringCardField.java b/src/eu/equalparts/cardbase/cardfield/StringCardField.java index 059a45d..8239138 100644 --- a/src/eu/equalparts/cardbase/cardfield/StringCardField.java +++ b/src/eu/equalparts/cardbase/cardfield/StringCardField.java @@ -1,5 +1,5 @@ package eu.equalparts.cardbase.cardfield; -import eu.equalparts.cardbase.filtering.CardFiltering.Filter; +import eu.equalparts.cardbase.filtering.Filter; public class StringCardField extends CardField { @@ -8,16 +8,16 @@ public class StringCardField extends CardField { } @Override - public boolean filter(Filter filter, String s) { - switch (filter) { + public boolean filter(Filter filter) { + switch (filter.type) { case CONTAINS: - return get().toLowerCase().contains(s.toLowerCase()); + return get().toLowerCase().contains(filter.value.toLowerCase()); case EQUALS: - return get().equalsIgnoreCase(s); + return get().equalsIgnoreCase(filter.value); case REGEX: - return get().matches(s); + return get().matches(filter.value); default: - return false; + throw new IllegalArgumentException(); } } } diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index f86365d..5738f5d 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -389,14 +389,14 @@ public final class CardbaseCLI { if (cardToRemove != null) { String count = args.length > 1 ? args[1] : "1"; - if (count.matches("[-]?[0-9]+")) { - Integer intCount = Integer.valueOf(count); + try { + Integer intCount = Integer.parseInt(count); if (intCount > 0) { removeCard(cardToRemove, intCount); } else { System.out.println("Cannot remove " + count + " cards."); } - } else { + } catch (NumberFormatException e) { System.out.println(count + " is not a valid number of cards."); } } else { @@ -430,16 +430,14 @@ public final class CardbaseCLI { if (cardToAdd != null) { String count = args != null && args.length > 0 ? args[0] : "1"; - if (count.matches("[-]?[0-9]+")) { - - Integer intCount = Integer.valueOf(count); - + try { + Integer intCount = Integer.parseInt(count); if (intCount > 0) { addCard(cardToAdd, intCount); } else { System.out.println("Cannot add " + intCount + " cards."); } - } else { + } catch (NumberFormatException e) { System.out.println(count + " is not a valid number of cards."); } } else { diff --git a/src/eu/equalparts/cardbase/comparator/CardComparator.java b/src/eu/equalparts/cardbase/comparator/CardComparator.java index d2cd0c1..31d42ad 100644 --- a/src/eu/equalparts/cardbase/comparator/CardComparator.java +++ b/src/eu/equalparts/cardbase/comparator/CardComparator.java @@ -1,6 +1,5 @@ package eu.equalparts.cardbase.comparator; -import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Comparator; import java.util.function.BiFunction; @@ -9,6 +8,7 @@ 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; +import eu.equalparts.cardbase.utils.Utils; /** * I'm new to this reflection business, so bear with me. @@ -56,12 +56,10 @@ public class CardComparator implements Comparator { this.fieldToCompare = fieldToCompare; // if annotated with a special comparator, set the comparison delegate here - for (Annotation annotation : fieldToCompare.getAnnotations()) { - if (annotation.annotationType() == DirtyNumber.class) { - this.comparisonDelegate = ComparatorDelegates::compareDirtyNumber; - } else if (annotation.annotationType() == Rarity.class) { - this.comparisonDelegate = ComparatorDelegates::compareRarity; - } + if (Utils.hasAnnotation(fieldToCompare, DirtyNumber.class)) { + this.comparisonDelegate = ComparatorDelegates::compareDirtyNumber; + } else if (Utils.hasAnnotation(fieldToCompare, Rarity.class)) { + this.comparisonDelegate = ComparatorDelegates::compareRarity; } } else { throw new IllegalArgumentException("The field provided is not valid."); diff --git a/src/eu/equalparts/cardbase/filtering/CardFiltering.java b/src/eu/equalparts/cardbase/filtering/CardFiltering.java index 081951f..8da9912 100644 --- a/src/eu/equalparts/cardbase/filtering/CardFiltering.java +++ b/src/eu/equalparts/cardbase/filtering/CardFiltering.java @@ -6,6 +6,10 @@ import java.util.List; import eu.equalparts.cardbase.card.Card; import eu.equalparts.cardbase.cardfield.CardField; +import eu.equalparts.cardbase.cardfield.IntegerCardField; +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; +import eu.equalparts.cardbase.filtering.Filter.FilterType; +import eu.equalparts.cardbase.utils.Utils; /** * Contains and equals are not case sensitive. Regex is. @@ -14,26 +18,51 @@ import eu.equalparts.cardbase.cardfield.CardField; * */ public class CardFiltering { - - public enum Filter { - EQUALS, CONTAINS, REGEX, GREATER_THAN, SMALLER_THAN; - } - - public static void filterByField(List cards, String fieldName, Filter filterType, String filterValue) throws NoSuchFieldException { - Field fieldToFilter = Card.class.getDeclaredField(fieldName); + + public static void filterByField(List cards, Filter filter) throws NoSuchFieldException { + Field fieldToFilter = Card.class.getDeclaredField(filter.fieldName); try { for (Iterator iterator = cards.iterator(); iterator.hasNext();) { - if (!((CardField) fieldToFilter.get(iterator.next())).filter(filterType, filterValue)) { + if (!((CardField) fieldToFilter.get(iterator.next())).filter(filter)) { iterator.remove(); } } - } catch (IllegalArgumentException e) { - System.out.println("Error: class Card does not define field \"" + fieldToFilter.getName() + "\"."); - e.printStackTrace(); } catch (IllegalAccessException e) { System.out.println("Error: field " + fieldToFilter.getName() + " in Card is not visible."); e.printStackTrace(); } } - + + public static boolean isFilterValid(Filter filter) { + // Check the integrity of the filter object + if (filter == null || filter.fieldName == null || filter.value == null) { + return false; + } + + // Check that the specified field exists + Field fieldToFilter; + try { + fieldToFilter = Card.class.getDeclaredField(filter.fieldName); + } catch (NoSuchFieldException | SecurityException e) { + return false; + } + + // If filter is numeric, check that the field and value are valid + if (filter.type == FilterType.GREATER_THAN || filter.type == FilterType.SMALLER_THAN) { + // Check that the field is of type integer + if (!fieldToFilter.getType().equals(IntegerCardField.class) + && !Utils.hasAnnotation(fieldToFilter, DirtyNumber.class)) { + return false; + } + + // Check that the value is an integer + try { + Integer.parseInt(filter.value); + } catch (NumberFormatException e) { + return false; + } + } + + return true; + } } diff --git a/src/eu/equalparts/cardbase/filtering/Filter.java b/src/eu/equalparts/cardbase/filtering/Filter.java new file mode 100644 index 0000000..7beaa09 --- /dev/null +++ b/src/eu/equalparts/cardbase/filtering/Filter.java @@ -0,0 +1,16 @@ +package eu.equalparts.cardbase.filtering; + +public class Filter { + + public enum FilterType { EQUALS, CONTAINS, REGEX, GREATER_THAN, SMALLER_THAN } + + public FilterType type; + public String fieldName, value; + + public Filter(FilterType type, String fieldName, String value) { + this.type = type; + this.fieldName = fieldName; + this.value = value; + } + +} diff --git a/src/eu/equalparts/cardbase/json/CardDeserializer.java b/src/eu/equalparts/cardbase/json/CardDeserializer.java index 7193c53..e140a5f 100644 --- a/src/eu/equalparts/cardbase/json/CardDeserializer.java +++ b/src/eu/equalparts/cardbase/json/CardDeserializer.java @@ -50,7 +50,6 @@ public class CardDeserializer extends StdDeserializer { newCard.loyalty = new IntegerCardField("loyalty", node.hasNonNull("loyalty") ? node.get("loyalty").asInt() : null); newCard.multiverseid = new IntegerCardField("multiverseid", node.hasNonNull("multiverseid") ? node.get("multiverseid").asInt() : null); newCard.imageName = new StringCardField("imageName", node.hasNonNull("imageName") ? node.get("imageName").asText() : null); - newCard.watermark = new StringCardField("watermark", node.hasNonNull("watermark") ? node.get("watermark").asText() : null); newCard.setCode = new StringCardField("setCode", node.hasNonNull("setCode") ? node.get("setCode").asText() : null); return newCard; diff --git a/src/eu/equalparts/cardbase/json/CardSerializer.java b/src/eu/equalparts/cardbase/json/CardSerializer.java index 5699061..7eaa0cb 100644 --- a/src/eu/equalparts/cardbase/json/CardSerializer.java +++ b/src/eu/equalparts/cardbase/json/CardSerializer.java @@ -103,12 +103,6 @@ public class CardSerializer extends JsonSerializer { jGen.writeNullField("imageName"); } - if (card.watermark.get() != null) { - jGen.writeStringField("watermark", card.watermark.get()); - } else { - jGen.writeNullField("watermark"); - } - if (card.setCode.get() != null) { jGen.writeStringField("setCode", card.setCode.get()); } else { diff --git a/src/eu/equalparts/cardbase/utils/InputSanity.java b/src/eu/equalparts/cardbase/utils/InputSanity.java deleted file mode 100644 index 0571c73..0000000 --- a/src/eu/equalparts/cardbase/utils/InputSanity.java +++ /dev/null @@ -1,27 +0,0 @@ -package eu.equalparts.cardbase.utils; - -public class InputSanity { - public static boolean isInteger(String str) { - if (str == null) { - return false; - } - int length = str.length(); - if (length == 0) { - return false; - } - int i = 0; - if (str.charAt(0) == '-') { - if (length == 1) { - return false; - } - i = 1; - } - for (; i < length; i++) { - char c = str.charAt(i); - if (c < '0' || c > '9') { - return false; - } - } - return true; - } -} diff --git a/src/eu/equalparts/cardbase/utils/Utils.java b/src/eu/equalparts/cardbase/utils/Utils.java new file mode 100644 index 0000000..bb88397 --- /dev/null +++ b/src/eu/equalparts/cardbase/utils/Utils.java @@ -0,0 +1,15 @@ +package eu.equalparts.cardbase.utils; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; + +public class Utils { + public static boolean hasAnnotation(Field fieldToAnalyse, Class annotation) { + for (Annotation a : fieldToAnalyse.getAnnotations()) { + if (a.annotationType().equals(annotation)) { + return true; + } + } + return false; + } +} -- cgit v1.2.3