aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/equalparts/cardbase')
-rw-r--r--src/eu/equalparts/cardbase/cardfield/IntegerCardField.java28
-rw-r--r--src/eu/equalparts/cardbase/cardfield/StringCardField.java47
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;
}
}
}