package jalview.util.matcher; import java.util.function.Function; /** * An immutable class that models one or more match conditions, each of which is * applied to the value obtained by lookup given the match key. *

* For example, the value provider could be a SequenceFeature's attributes map, * and the conditions might be *

* * @author gmcarstairs * */ public class KeyedMatcher implements KeyedMatcherI { final private String key; final private MatcherI matcher; /** * Constructor given a key, a test condition and a match pattern * * @param theKey * @param cond * @param pattern */ public KeyedMatcher(String theKey, Condition cond, String pattern) { key = theKey; matcher = new Matcher(cond, pattern); } /** * Constructor given a key, a test condition and a numerical value to compare * to. Note that if a non-numerical condition is specified, the float will be * converted to a string. * * @param theKey * @param cond * @param value */ public KeyedMatcher(String theKey, Condition cond, float value) { key = theKey; matcher = new Matcher(cond, value); } @Override public boolean matches(Function valueProvider) { String value = valueProvider.apply(key); return matcher.matches(value); } @Override public String getKey() { return key; } @Override public MatcherI getMatcher() { return matcher; } /** * Answers a string description of this matcher, suitable for debugging or * logging. The format may change in future. */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(key).append(" ").append(matcher.getCondition().toString()) .append(" "); if (matcher.getCondition().isNumeric()) { sb.append(matcher.getPattern()); } else { sb.append("'").append(matcher.getPattern()).append("'"); } return sb.toString(); } }