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
*
* - CSQ contains "pathological"
* - AND
* - AF <= 1.0e-5
*
*
* @author gmcarstairs
*
*/
public class KeyedMatcher implements KeyedMatcherI
{
private static final String COLON = ":";
final private String[] key;
final private MatcherI matcher;
/**
* Constructor given a key, a test condition and a match pattern
*
* @param cond
* @param pattern
* @param theKey
*/
public KeyedMatcher(Condition cond, String pattern, String... theKey)
{
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 cond
* @param value
* @param theKey
*/
public KeyedMatcher(Condition cond, float value, String... theKey)
{
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(String.join(COLON, 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();
}
}