X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Futil%2Fmatcher%2FMatcher.java;h=07925094479b1eda2f8676f62d2d6784e93a3013;hb=5574cf40bdcaf7ca99ba0611ed76824e7fb12cd9;hp=b162d5d98b1346cf5e22d6a02c3b29c466ad499f;hpb=67dbd0072044a077ac43079ea12c8c4803636ee1;p=jalview.git diff --git a/src/jalview/util/matcher/Matcher.java b/src/jalview/util/matcher/Matcher.java index b162d5d..0792509 100644 --- a/src/jalview/util/matcher/Matcher.java +++ b/src/jalview/util/matcher/Matcher.java @@ -1,5 +1,6 @@ package jalview.util.matcher; +import java.util.Objects; import java.util.regex.Pattern; /** @@ -13,12 +14,17 @@ public class Matcher implements MatcherI Condition condition; /* - * the string value (upper-cased), or the regex, to compare to + * the string pattern as entered, or the regex, to compare to * also holds the string form of float value if a numeric condition */ String pattern; /* + * the pattern in upper case, for non-case-sensitive matching + */ + String uppercasePattern; + + /* * the compiled regex if using a pattern match condition * (reserved for possible future enhancement) */ @@ -37,19 +43,29 @@ public class Matcher implements MatcherI * @return * @throws NumberFormatException * if a numerical condition is specified with a non-numeric - * comparision value + * comparison value * @throws NullPointerException - * if a null comparison string is specified + * if a null condition or comparison string is specified */ public Matcher(Condition cond, String compareTo) { + Objects.requireNonNull(cond); condition = cond; if (cond.isNumeric()) { value = Float.valueOf(compareTo); + pattern = String.valueOf(value); + uppercasePattern = pattern; + } + else + { + pattern = compareTo; + if (pattern != null) + { + uppercasePattern = pattern.toUpperCase(); + } } - // pattern matches will be non-case-sensitive - pattern = compareTo.toUpperCase(); + // if we add regex conditions (e.g. matchesPattern), then // pattern should hold the raw regex, and // regexPattern = Pattern.compile(compareTo); @@ -65,9 +81,7 @@ public class Matcher implements MatcherI */ public Matcher(Condition cond, float compareTo) { - condition = cond; - value = compareTo; - pattern = String.valueOf(compareTo); + this(cond, String.valueOf(compareTo)); } /** @@ -98,23 +112,29 @@ public class Matcher implements MatcherI if (val == null) { return condition == Condition.NotContains - || condition == Condition.NotMatches; + || condition == Condition.NotMatches + || condition == Condition.NotPresent; } String upper = val.toUpperCase().trim(); boolean matched = false; switch(condition) { case Matches: - matched = upper.equals(pattern); + matched = upper.equals(uppercasePattern); break; case NotMatches: - matched = !upper.equals(pattern); + matched = !upper.equals(uppercasePattern); break; case Contains: - matched = upper.indexOf(pattern) > -1; + matched = upper.indexOf(uppercasePattern) > -1; break; case NotContains: - matched = upper.indexOf(pattern) == -1; + matched = upper.indexOf(uppercasePattern) == -1; + break; + case Present: + matched = true; + break; + default: break; } return matched; @@ -154,6 +174,8 @@ public class Matcher implements MatcherI case GE: matched = f >= value; break; + default: + break; } return matched; @@ -171,7 +193,7 @@ public class Matcher implements MatcherI /** * equals is overridden so that we can safely remove Matcher objects from - * collections (e.g. delete an attribut match condition for a feature colour) + * collections (e.g. delete an attribute match condition for a feature colour) */ @Override public boolean equals(Object obj) @@ -181,8 +203,15 @@ public class Matcher implements MatcherI return false; } Matcher m = (Matcher) obj; - return condition != m.condition || value != m.value - || !pattern.equals(m.pattern); + if (condition != m.condition || value != m.value) + { + return false; + } + if (pattern == null) + { + return m.pattern == null; + } + return uppercasePattern.equals(m.uppercasePattern); } @Override @@ -202,4 +231,21 @@ public class Matcher implements MatcherI { return value; } + + @Override + public String toString() + { + StringBuilder sb = new StringBuilder(); + sb.append(condition.toString()).append(" "); + if (condition.isNumeric()) + { + sb.append(pattern); + } + else + { + sb.append("'").append(pattern).append("'"); + } + + return sb.toString(); + } }