1 package jalview.util.matcher;
3 import java.util.regex.Pattern;
6 * A bean to describe one attribute-based filter
8 public class Matcher implements MatcherI
11 * the comparison condition
16 * the string value (upper-cased), or the regex, to compare to
17 * also holds the string form of float value if a numeric condition
22 * the compiled regex if using a pattern match condition
23 * (reserved for possible future enhancement)
28 * the value to compare to for a numerical condition
38 * @throws NumberFormatException
39 * if a numerical condition is specified with a non-numeric
41 * @throws NullPointerException
42 * if a null comparison string is specified
44 public Matcher(Condition cond, String compareTo)
49 value = Float.valueOf(compareTo);
51 // pattern matches will be non-case-sensitive
52 pattern = compareTo.toUpperCase();
53 // if we add regex conditions (e.g. matchesPattern), then
54 // pattern should hold the raw regex, and
55 // regexPattern = Pattern.compile(compareTo);
59 * Constructor for a numerical match condition. Note that if a string
60 * comparison condition is specified, this will be converted to a comparison
61 * with the float value as string
66 public Matcher(Condition cond, float compareTo)
70 pattern = String.valueOf(compareTo);
76 @SuppressWarnings("incomplete-switch")
78 public boolean matches(String val)
80 if (condition.isNumeric())
85 * treat a null value (no such attribute) as
86 * failing any numerical filter condition
88 return val == null ? false : matches(Float.valueOf(val));
89 } catch (NumberFormatException e)
96 * a null value matches a negative condition, fails a positive test
100 return condition == Condition.NotContains
101 || condition == Condition.NotMatches;
104 String upper = val.toUpperCase().trim();
105 boolean matched = false;
108 matched = upper.equals(pattern);
111 matched = !upper.equals(pattern);
114 matched = upper.indexOf(pattern) > -1;
117 matched = upper.indexOf(pattern) == -1;
124 * Applies a numerical comparison match condition
129 @SuppressWarnings("incomplete-switch")
130 boolean matches(float f)
132 if (!condition.isNumeric())
134 return matches(String.valueOf(f));
137 boolean matched = false;
143 matched = f <= value;
146 matched = f == value;
149 matched = f != value;
155 matched = f >= value;
163 * A simple hash function that guarantees that when two objects are equal,
164 * they have the same hashcode
167 public int hashCode()
169 return pattern.hashCode() + condition.hashCode() + (int) value;
173 * equals is overridden so that we can safely remove Matcher objects from
174 * collections (e.g. delete an attribut match condition for a feature colour)
177 public boolean equals(Object obj)
179 if (obj == null || !(obj instanceof Matcher))
183 Matcher m = (Matcher) obj;
184 return condition != m.condition || value != m.value
185 || !pattern.equals(m.pattern);
189 public Condition getCondition()
195 public String getPattern()
201 public float getFloatValue()