1 package jalview.util.matcher;
3 import java.util.ArrayList;
5 import java.util.function.Function;
7 public class KeyedMatcherSet implements KeyedMatcherSetI
9 List<KeyedMatcherI> matchConditions;
11 boolean andConditions;
16 public KeyedMatcherSet()
18 matchConditions = new ArrayList<>();
22 public boolean matches(Function<String[], String> valueProvider)
25 * no conditions matches anything
27 if (matchConditions.isEmpty())
37 for (KeyedMatcherI m : matchConditions)
39 if (!m.matches(valueProvider))
50 for (KeyedMatcherI m : matchConditions)
52 if (m.matches(valueProvider))
61 public KeyedMatcherSetI and(KeyedMatcherI m)
63 if (!andConditions && matchConditions.size() > 1)
65 throw new IllegalStateException("Can't add an AND to OR conditions");
67 matchConditions.add(m);
74 public KeyedMatcherSetI or(KeyedMatcherI m)
76 if (andConditions && matchConditions.size() > 1)
78 throw new IllegalStateException("Can't add an OR to AND conditions");
80 matchConditions.add(m);
81 andConditions = false;
87 public boolean isAnded()
93 public Iterable<KeyedMatcherI> getMatchers()
95 return matchConditions;
99 public String toString()
101 StringBuilder sb = new StringBuilder();
102 boolean first = true;
103 for (KeyedMatcherI matcher : matchConditions)
107 sb.append(andConditions ? " AND " : " OR ");
110 sb.append("(").append(matcher.toString()).append(")");
112 return sb.toString();
116 public boolean isEmpty()
118 return matchConditions == null || matchConditions.isEmpty();