diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2016-08-15 15:30:27 +0200 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2016-08-15 15:30:27 +0200 |
commit | 7f80ab416a708e5c31116b9af7295af673d116d7 (patch) | |
tree | 27e0974b2b583b750d461aaac075190ebe9f3a39 /src/eu | |
parent | 8cb35b358563e5a81ad9e4aceb123b85222cf4cc (diff) |
Added more filtering tests, almost done
Diffstat (limited to 'src/eu')
-rw-r--r-- | src/eu/equalparts/cardbase/cardfield/IntegerCardField.java | 28 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/cardfield/StringCardField.java | 47 |
2 files changed, 53 insertions, 22 deletions
diff --git a/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java index 8526640..604880a 100644 --- a/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java +++ b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java @@ -9,18 +9,22 @@ public class IntegerCardField extends CardField<Integer> { @Override public boolean filter(Filter filter) throws NumberFormatException { - switch (filter.type) { - case CONTAINS: - return get().toString().contains(filter.value); - case EQUALS: - return get().toString().equalsIgnoreCase(filter.value); - case REGEX: - return get().toString().matches(filter.value); - case GREATER_THAN: - return get() > Integer.parseInt(filter.value); - case SMALLER_THAN: - return get() < Integer.parseInt(filter.value); - default: + if (get() != null) { + switch (filter.type) { + case CONTAINS: + return get().toString().contains(filter.value); + case EQUALS: + return get().toString().equalsIgnoreCase(filter.value); + case REGEX: + return get().toString().matches(filter.value); + case GREATER_THAN: + return get() > Integer.parseInt(filter.value); + case SMALLER_THAN: + return get() < Integer.parseInt(filter.value); + default: + return false; + } + } else { return false; } } diff --git a/src/eu/equalparts/cardbase/cardfield/StringCardField.java b/src/eu/equalparts/cardbase/cardfield/StringCardField.java index 8239138..d33cb53 100644 --- a/src/eu/equalparts/cardbase/cardfield/StringCardField.java +++ b/src/eu/equalparts/cardbase/cardfield/StringCardField.java @@ -1,23 +1,50 @@ package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; import eu.equalparts.cardbase.filtering.Filter; +import eu.equalparts.cardbase.utils.Utils; public class StringCardField extends CardField<String> { - + public StringCardField(String name, String value) { super(name, value); } @Override public boolean filter(Filter filter) { - switch (filter.type) { - case CONTAINS: - return get().toLowerCase().contains(filter.value.toLowerCase()); - case EQUALS: - return get().equalsIgnoreCase(filter.value); - case REGEX: - return get().matches(filter.value); - default: - throw new IllegalArgumentException(); + if (get() != null) { + switch (filter.type) { + case CONTAINS: + return get().toLowerCase().contains(filter.value.toLowerCase()); + case EQUALS: + return get().equalsIgnoreCase(filter.value); + case REGEX: + return get().matches(filter.value); + case GREATER_THAN: + try { + if (Utils.hasAnnotation(Card.class.getField(filter.fieldName), DirtyNumber.class)) { + return Integer.parseInt(get().replaceAll("[^0-9]+", "")) > Integer.parseInt(filter.value); + } else { + throw new IllegalArgumentException(); + } + } catch (NumberFormatException | NoSuchFieldException | SecurityException e) { + return false; + } + case SMALLER_THAN: + try { + if (Utils.hasAnnotation(Card.class.getField(filter.fieldName), DirtyNumber.class)) { + return Integer.parseInt(get().replaceAll("[^0-9]+", "")) < Integer.parseInt(filter.value); + } else { + throw new IllegalArgumentException(); + } + } catch (NumberFormatException | NoSuchFieldException | SecurityException e) { + return false; + } + default: + throw new IllegalArgumentException(); + } + } else { + return false; } } } |