1 package jalview.util.matcher;
3 import java.util.Objects;
4 import java.util.regex.Pattern;
7 * A bean to describe one attribute-based filter
9 public class Matcher implements MatcherI
12 * the comparison condition
17 * the string value (upper-cased), or the regex, to compare to
18 * also holds the string form of float value if a numeric condition
23 * the compiled regex if using a pattern match condition
24 * (reserved for possible future enhancement)
29 * the value to compare to for a numerical condition
39 * @throws NumberFormatException
40 * if a numerical condition is specified with a non-numeric
42 * @throws NullPointerException
43 * if a null condition or comparison string is specified
45 public Matcher(Condition cond, String compareTo)
50 value = Float.valueOf(compareTo);
51 pattern = String.valueOf(value);
55 // pattern matches will be non-case-sensitive
56 pattern = compareTo.toUpperCase();
59 // if we add regex conditions (e.g. matchesPattern), then
60 // pattern should hold the raw regex, and
61 // regexPattern = Pattern.compile(compareTo);
65 * Constructor for a numerical match condition. Note that if a string
66 * comparison condition is specified, this will be converted to a comparison
67 * with the float value as string
72 public Matcher(Condition cond, float compareTo)
74 Objects.requireNonNull(cond);
77 pattern = String.valueOf(compareTo).toUpperCase();
83 @SuppressWarnings("incomplete-switch")
85 public boolean matches(String val)
87 if (condition.isNumeric())
92 * treat a null value (no such attribute) as
93 * failing any numerical filter condition
95 return val == null ? false : matches(Float.valueOf(val));
96 } catch (NumberFormatException e)
103 * a null value matches a negative condition, fails a positive test
107 return condition == Condition.NotContains
108 || condition == Condition.NotMatches;
111 String upper = val.toUpperCase().trim();
112 boolean matched = false;
115 matched = upper.equals(pattern);
118 matched = !upper.equals(pattern);
121 matched = upper.indexOf(pattern) > -1;
124 matched = upper.indexOf(pattern) == -1;
131 * Applies a numerical comparison match condition
136 @SuppressWarnings("incomplete-switch")
137 boolean matches(float f)
139 if (!condition.isNumeric())
141 return matches(String.valueOf(f));
144 boolean matched = false;
150 matched = f <= value;
153 matched = f == value;
156 matched = f != value;
162 matched = f >= value;
170 * A simple hash function that guarantees that when two objects are equal,
171 * they have the same hashcode
174 public int hashCode()
176 return pattern.hashCode() + condition.hashCode() + (int) value;
180 * equals is overridden so that we can safely remove Matcher objects from
181 * collections (e.g. delete an attribut match condition for a feature colour)
184 public boolean equals(Object obj)
186 if (obj == null || !(obj instanceof Matcher))
190 Matcher m = (Matcher) obj;
191 return condition == m.condition && value == m.value
192 && pattern.equals(m.pattern);
196 public Condition getCondition()
202 public String getPattern()
208 public float getFloatValue()
214 public String toString()
216 return condition.name() + " " + pattern;