1 package jalview.util.matcher;
3 import java.util.ArrayList;
4 import java.util.Iterator;
6 import java.util.function.Function;
8 public class KeyedMatcherSet implements KeyedMatcherSetI
10 List<KeyedMatcherI> matchConditions;
12 boolean andConditions;
17 public KeyedMatcherSet()
19 matchConditions = new ArrayList<>();
23 public boolean matches(Function<String, String> valueProvider)
26 * no conditions matches anything
28 if (matchConditions.isEmpty())
38 for (KeyedMatcherI m : matchConditions)
40 if (!m.matches(valueProvider))
51 for (KeyedMatcherI m : matchConditions)
53 if (m.matches(valueProvider))
62 public KeyedMatcherSetI and(KeyedMatcherI m)
64 if (!andConditions && matchConditions.size() > 1)
66 throw new IllegalStateException("Can't add an AND to OR conditions");
68 matchConditions.add(m);
75 public KeyedMatcherSetI or(KeyedMatcherI m)
77 if (andConditions && matchConditions.size() > 1)
79 throw new IllegalStateException("Can't add an OR to AND conditions");
81 matchConditions.add(m);
82 andConditions = false;
88 public boolean isAnded()
94 public Iterator<KeyedMatcherI> getMatchers()
96 return matchConditions.iterator();
100 public String toString()
102 StringBuilder sb = new StringBuilder();
103 boolean first = true;
104 for (KeyedMatcherI matcher : matchConditions)
108 sb.append(andConditions ? " AND " : " OR ");
111 sb.append("(").append(matcher.toString()).append(")");
113 return sb.toString();
117 public boolean isEmpty()
119 return matchConditions == null || matchConditions.isEmpty();