Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.util.matcher

File Matcher.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

20
80
10
1
251
162
34
0.43
8
10
3.4

Classes

Class Line # Actions
Matcher 9 80 34 4
0.9636363496.4%
 

Contributing tests

This file is covered by 35 tests. .

Source view

1    package jalview.util.matcher;
2   
3    import java.util.Objects;
4    import java.util.regex.Pattern;
5   
6    /**
7    * A bean to describe one attribute-based filter
8    */
 
9    public class Matcher implements MatcherI
10    {
11    /*
12    * the comparison condition
13    */
14    Condition condition;
15   
16    /*
17    * the string pattern as entered, or the regex, to compare to
18    * also holds the string form of float value if a numeric condition
19    */
20    String pattern;
21   
22    /*
23    * the pattern in upper case, for non-case-sensitive matching
24    */
25    String uppercasePattern;
26   
27    /*
28    * the compiled regex if using a pattern match condition
29    * (reserved for possible future enhancement)
30    */
31    Pattern regexPattern;
32   
33    /*
34    * the value to compare to for a numerical condition
35    */
36    float value;
37   
38    /**
39    * Constructor
40    *
41    * @param cond
42    * @param compareTo
43    * @return
44    * @throws NumberFormatException
45    * if a numerical condition is specified with a non-numeric
46    * comparison value
47    * @throws NullPointerException
48    * if a null condition or comparison string is specified
49    */
 
50  187 toggle public Matcher(Condition cond, String compareTo)
51    {
52  187 Objects.requireNonNull(cond);
53  186 condition = cond;
54  186 if (cond.isNumeric())
55    {
56  96 value = Float.valueOf(compareTo);
57  93 pattern = String.valueOf(value);
58  93 uppercasePattern = pattern;
59    }
60    else
61    {
62  90 pattern = compareTo;
63  90 if (pattern != null)
64    {
65  85 uppercasePattern = pattern.toUpperCase();
66    }
67    }
68   
69    // if we add regex conditions (e.g. matchesPattern), then
70    // pattern should hold the raw regex, and
71    // regexPattern = Pattern.compile(compareTo);
72    }
73   
74    /**
75    * Constructor for a numerical match condition. Note that if a string
76    * comparison condition is specified, this will be converted to a comparison
77    * with the float value as string
78    *
79    * @param cond
80    * @param compareTo
81    */
 
82  24 toggle public Matcher(Condition cond, float compareTo)
83    {
84  24 this(cond, String.valueOf(compareTo));
85    }
86   
87    /**
88    * {@inheritDoc}
89    */
 
90  153 toggle @SuppressWarnings("incomplete-switch")
91    @Override
92    public boolean matches(String val)
93    {
94  153 if (condition.isNumeric())
95    {
96  73 try
97    {
98    /*
99    * treat a null value (no such attribute) as
100    * failing any numerical filter condition
101    */
102  73 return val == null ? false : matches(Float.valueOf(val));
103    } catch (NumberFormatException e)
104    {
105  18 return false;
106    }
107    }
108   
109    /*
110    * a null value matches a negative condition, fails a positive test
111    */
112  80 if (val == null)
113    {
114  11 return condition == Condition.NotContains
115    || condition == Condition.NotMatches
116    || condition == Condition.NotPresent;
117    }
118   
119  69 String upper = val.toUpperCase().trim();
120  69 boolean matched = false;
121  69 switch(condition) {
122  8 case Matches:
123  8 matched = upper.equals(uppercasePattern);
124  8 break;
125  6 case NotMatches:
126  6 matched = !upper.equals(uppercasePattern);
127  6 break;
128  31 case Contains:
129  31 matched = upper.indexOf(uppercasePattern) > -1;
130  31 break;
131  16 case NotContains:
132  16 matched = upper.indexOf(uppercasePattern) == -1;
133  16 break;
134  4 case Present:
135  4 matched = true;
136  4 break;
137  4 default:
138  4 break;
139    }
140  69 return matched;
141    }
142   
143    /**
144    * Applies a numerical comparison match condition
145    *
146    * @param f
147    * @return
148    */
 
149  47 toggle @SuppressWarnings("incomplete-switch")
150    boolean matches(float f)
151    {
152  47 if (!condition.isNumeric())
153    {
154  2 return matches(String.valueOf(f));
155    }
156   
157  45 boolean matched = false;
158  45 switch (condition) {
159  10 case LT:
160  10 matched = f < value;
161  10 break;
162  6 case LE:
163  6 matched = f <= value;
164  6 break;
165  5 case EQ:
166  5 matched = f == value;
167  5 break;
168  3 case NE:
169  3 matched = f != value;
170  3 break;
171  6 case GT:
172  6 matched = f > value;
173  6 break;
174  15 case GE:
175  15 matched = f >= value;
176  15 break;
177  0 default:
178  0 break;
179    }
180   
181  45 return matched;
182    }
183   
184    /**
185    * A simple hash function that guarantees that when two objects are equal,
186    * they have the same hashcode
187    */
 
188  8 toggle @Override
189    public int hashCode()
190    {
191  8 return pattern.hashCode() + condition.hashCode() + (int) value;
192    }
193   
194    /**
195    * equals is overridden so that we can safely remove Matcher objects from
196    * collections (e.g. delete an attribute match condition for a feature colour)
197    */
 
198  18 toggle @Override
199    public boolean equals(Object obj)
200    {
201  18 if (obj == null || !(obj instanceof Matcher))
202    {
203  4 return false;
204    }
205  14 Matcher m = (Matcher) obj;
206  14 if (condition != m.condition || value != m.value)
207    {
208  6 return false;
209    }
210  8 if (pattern == null)
211    {
212  0 return m.pattern == null;
213    }
214  8 return uppercasePattern.equals(m.uppercasePattern);
215    }
216   
 
217  86 toggle @Override
218    public Condition getCondition()
219    {
220  86 return condition;
221    }
222   
 
223  83 toggle @Override
224    public String getPattern()
225    {
226  83 return pattern;
227    }
228   
 
229  10 toggle @Override
230    public float getFloatValue()
231    {
232  10 return value;
233    }
234   
 
235  3 toggle @Override
236    public String toString()
237    {
238  3 StringBuilder sb = new StringBuilder();
239  3 sb.append(condition.toString()).append(" ");
240  3 if (condition.isNumeric())
241    {
242  1 sb.append(pattern);
243    }
244    else
245    {
246  2 sb.append("'").append(pattern).append("'");
247    }
248   
249  3 return sb.toString();
250    }
251    }