JAL-2808 refine FeatureMatcher interface and matcher toString methods
[jalview.git] / src / jalview / util / matcher / Matcher.java
index 14a8585..353df83 100644 (file)
@@ -14,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)
    */
@@ -44,16 +49,21 @@ public class Matcher implements MatcherI
    */
   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 matches will be non-case-sensitive
-      pattern = compareTo == null ? null : compareTo.toUpperCase();
+      pattern = compareTo;
+      if (pattern != null)
+      {
+        uppercasePattern = pattern.toUpperCase();
+      }
     }
 
     // if we add regex conditions (e.g. matchesPattern), then
@@ -71,10 +81,7 @@ public class Matcher implements MatcherI
    */
   public Matcher(Condition cond, float compareTo)
   {
-    Objects.requireNonNull(cond);
-    condition = cond;
-    value = compareTo;
-    pattern = String.valueOf(compareTo).toUpperCase();
+    this(cond, String.valueOf(compareTo));
   }
 
   /**
@@ -113,16 +120,16 @@ public class Matcher implements MatcherI
     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;
@@ -186,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)
@@ -196,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
@@ -222,7 +236,7 @@ public class Matcher implements MatcherI
   public String toString()
   {
     StringBuilder sb = new StringBuilder();
-    sb.append(condition.name()).append(" ");
+    sb.append(condition.toString()).append(" ");
     if (condition.isNumeric())
     {
       sb.append(pattern);