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
{
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(" ").append(matcher.getPattern());
return sb.toString();
}
}