From c07d860f1d90dc3961b31c87dbe637dbcd87da76 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 11 Aug 2016 23:59:10 +0200 Subject: Refactored the Card data structure to be able to do filtering properly --- .../equalparts/cardbase/cardfield/CardField.java | 31 ++++++++++++++++++++++ .../cardbase/cardfield/IntegerCardField.java | 23 ++++++++++++++++ .../cardbase/cardfield/StringCardField.java | 23 ++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/eu/equalparts/cardbase/cardfield/CardField.java create mode 100644 src/eu/equalparts/cardbase/cardfield/IntegerCardField.java create mode 100644 src/eu/equalparts/cardbase/cardfield/StringCardField.java (limited to 'src/eu/equalparts/cardbase/cardfield') diff --git a/src/eu/equalparts/cardbase/cardfield/CardField.java b/src/eu/equalparts/cardbase/cardfield/CardField.java new file mode 100644 index 0000000..9af775b --- /dev/null +++ b/src/eu/equalparts/cardbase/cardfield/CardField.java @@ -0,0 +1,31 @@ +package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.filtering.CardFiltering.Filter; + +public abstract class CardField> implements Comparable> { + private String name; + private T value; + + public CardField(String name, T value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public T get() { + return value; + } + + public void set(T newValue) { + this.value = newValue; + } + + @Override + public int compareTo(CardField o) { + return value.compareTo(o.get()); + } + + public abstract boolean filter(Filter filter, String s); +} diff --git a/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java new file mode 100644 index 0000000..4016252 --- /dev/null +++ b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java @@ -0,0 +1,23 @@ +package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.filtering.CardFiltering.Filter; + +public class IntegerCardField extends CardField { + + public IntegerCardField(String name, Integer value) { + super(name, value); + } + + @Override + public boolean filter(Filter filter, String s) { + switch (filter) { + case CONTAINS: + return get().toString().contains(s); + case EQUALS: + return get().toString().equalsIgnoreCase(s); + case REGEX: + return get().toString().matches(s); + default: + return false; + } + } +} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/cardfield/StringCardField.java b/src/eu/equalparts/cardbase/cardfield/StringCardField.java new file mode 100644 index 0000000..059a45d --- /dev/null +++ b/src/eu/equalparts/cardbase/cardfield/StringCardField.java @@ -0,0 +1,23 @@ +package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.filtering.CardFiltering.Filter; + +public class StringCardField extends CardField { + + public StringCardField(String name, String value) { + super(name, value); + } + + @Override + public boolean filter(Filter filter, String s) { + switch (filter) { + case CONTAINS: + return get().toLowerCase().contains(s.toLowerCase()); + case EQUALS: + return get().equalsIgnoreCase(s); + case REGEX: + return get().matches(s); + default: + return false; + } + } +} -- cgit v1.2.3