From 7f80ab416a708e5c31116b9af7295af673d116d7 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 15 Aug 2016 15:30:27 +0200 Subject: Added more filtering tests, almost done --- .../cardbase/cardfield/IntegerCardField.java | 28 +++++++------ .../cardbase/cardfield/StringCardField.java | 47 +++++++++++++++++----- 2 files changed, 53 insertions(+), 22 deletions(-) (limited to 'src/eu/equalparts') 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 { @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 { - + 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; } } } -- cgit v1.2.3