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 comparison
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 == null ? null : 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
109 || condition == Condition.NotPresent;
112 String upper = val.toUpperCase().trim();
113 boolean matched = false;
116 matched = upper.equals(pattern);
119 matched = !upper.equals(pattern);
122 matched = upper.indexOf(pattern) > -1;
125 matched = upper.indexOf(pattern) == -1;
137 * Applies a numerical comparison match condition
142 @SuppressWarnings("incomplete-switch")
143 boolean matches(float f)
145 if (!condition.isNumeric())
147 return matches(String.valueOf(f));
150 boolean matched = false;
156 matched = f <= value;
159 matched = f == value;
162 matched = f != value;
168 matched = f >= value;
178 * A simple hash function that guarantees that when two objects are equal,
179 * they have the same hashcode
182 public int hashCode()
184 return pattern.hashCode() + condition.hashCode() + (int) value;
188 * equals is overridden so that we can safely remove Matcher objects from
189 * collections (e.g. delete an attribut match condition for a feature colour)
192 public boolean equals(Object obj)
194 if (obj == null || !(obj instanceof Matcher))
198 Matcher m = (Matcher) obj;
199 return condition == m.condition && value == m.value
200 && pattern.equals(m.pattern);
204 public Condition getCondition()
210 public String getPattern()
216 public float getFloatValue()
222 public String toString()
224 StringBuilder sb = new StringBuilder();
225 sb.append(condition.name()).append(" ");
226 if (condition.isNumeric())
232 sb.append("'").append(pattern).append("'");
235 return sb.toString();