aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/cardfield/StringCardField.java
blob: d33cb5347b8090007234b9aeeb8e81ba7b44c96d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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) {
		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;
		}
	}
}