1 package jalview.util.matcher;
3 import java.util.function.Function;
5 public class KeyedMatcher implements KeyedMatcherI
9 private MatcherI matcher;
12 * an optional second condition
14 KeyedMatcherI combineWith;
17 * if true, any second condition is AND-ed with this one
18 * if false,any second condition is OR-ed with this one
23 * Constructor given a match condition
27 public KeyedMatcher(String theKey, MatcherI m)
34 public boolean matches(Function<String, String> valueProvider)
36 String value = valueProvider.apply(key);
37 boolean matched = matcher.matches(value);
40 * apply a second condition if there is one, using
41 * lazy evalution of AND and OR combinations
43 if (combineWith != null)
45 if (combineAnd && matched)
47 matched = combineWith.matches(valueProvider);
49 if (!combineAnd && !matched)
51 matched = combineWith.matches(valueProvider);
59 public KeyedMatcherI and(String key2, MatcherI m)
61 return combineWith(key2, m, true);
65 public KeyedMatcherI or(String key2, MatcherI m)
67 return combineWith(key2, m, false);
71 * Answers a Matcher that is the logical combination of this one with the
72 * given argument. The two matchers are AND-ed if and is true, else OR-ed.
79 KeyedMatcher combineWith(String key2, MatcherI condition2,
82 if (condition2 == null)
87 KeyedMatcher combined = new KeyedMatcher(key2, condition2);
88 combined.combineWith = this;
89 combined.combineAnd = and;
95 public String getKey()
101 public MatcherI getMatcher()
107 public KeyedMatcherI getSecondMatcher()
113 public boolean isAnded()
119 * Answers a string description of this matcher, suitable for debugging or
120 * logging. The format may change in future.
123 public String toString()
125 StringBuilder sb = new StringBuilder();
126 sb.append(key).append(" ").append(matcher.getCondition().name())
127 .append(" ").append(matcher.getPattern());
129 if (combineWith != null)
131 sb.append(" ").append(combineAnd ? "AND (" : "OR (")
132 .append(combineWith.toString()).append(")");
134 return sb.toString();