2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.datamodel.features;
23 import jalview.datamodel.SequenceFeature;
24 import jalview.util.MessageManager;
26 import java.util.ArrayList;
27 import java.util.List;
30 * A class that models one or more match conditions, which may be combined with
31 * AND or OR (but not a mixture)
35 public class FeatureMatcherSet implements FeatureMatcherSetI
37 private static final String OR = "OR";
39 private static final String AND = "AND";
41 private static final String SPACE = " ";
43 private static final String CLOSE_BRACKET = ")";
45 private static final String OPEN_BRACKET = "(";
47 private static final String OR_I18N = MessageManager
48 .getString("label.or");
50 private static final String AND_18N = MessageManager
51 .getString("label.and");
53 List<FeatureMatcherI> matchConditions;
55 boolean andConditions;
58 * A factory constructor that converts a stringified object (as output by
59 * toStableString) to an object instance.
63 * <li>(condition1) AND (condition2) AND (condition3)</li>
65 * <li>(condition1) OR (condition2) OR (condition3)</li>
67 * where OR and AND are not case-sensitive, and may not be mixed. Brackets are
68 * optional if there is only one condition.
72 * @see FeatureMatcher#fromString(String)
74 public static FeatureMatcherSet fromString(final String descriptor)
76 String invalid = "Invalid descriptor: " + descriptor;
77 boolean firstCondition = true;
78 FeatureMatcherSet result = new FeatureMatcherSet();
80 String leftToParse = descriptor.trim();
82 while (leftToParse.length() > 0)
85 * inspect AND or OR condition, check not mixed
90 int spacePos = leftToParse.indexOf(SPACE);
93 // trailing junk after a match condition
94 System.err.println(invalid);
97 String conjunction = leftToParse.substring(0, spacePos);
98 leftToParse = leftToParse.substring(spacePos + 1).trim();
99 if (conjunction.equalsIgnoreCase(AND))
103 else if (conjunction.equalsIgnoreCase(OR))
109 // not an AND or an OR - invalid
110 System.err.println(invalid);
116 * now extract the next condition and AND or OR it
118 String nextCondition = leftToParse;
119 if (leftToParse.startsWith(OPEN_BRACKET))
121 int closePos = leftToParse.indexOf(CLOSE_BRACKET);
124 System.err.println(invalid);
127 nextCondition = leftToParse.substring(1, closePos);
128 leftToParse = leftToParse.substring(closePos + 1).trim();
135 FeatureMatcher fm = FeatureMatcher.fromString(nextCondition);
138 System.err.println(invalid);
151 firstCondition = false;
152 } catch (IllegalStateException e)
154 // thrown if OR and AND are mixed
155 System.err.println(invalid);
166 public FeatureMatcherSet()
168 matchConditions = new ArrayList<>();
172 public boolean matches(SequenceFeature feature)
175 * no conditions matches anything
177 if (matchConditions.isEmpty())
187 for (FeatureMatcherI m : matchConditions)
189 if (!m.matches(feature))
200 for (FeatureMatcherI m : matchConditions)
202 if (m.matches(feature))
211 public void and(FeatureMatcherI m)
213 if (!andConditions && matchConditions.size() > 1)
215 throw new IllegalStateException("Can't add an AND to OR conditions");
217 matchConditions.add(m);
218 andConditions = true;
222 public void or(FeatureMatcherI m)
224 if (andConditions && matchConditions.size() > 1)
226 throw new IllegalStateException("Can't add an OR to AND conditions");
228 matchConditions.add(m);
229 andConditions = false;
233 public boolean isAnded()
235 return andConditions;
239 public Iterable<FeatureMatcherI> getMatchers()
241 return matchConditions;
245 * Answers a string representation of this object suitable for display, and
246 * possibly internationalized. The format is not guaranteed stable and may
250 public String toString()
252 StringBuilder sb = new StringBuilder();
253 boolean first = true;
254 boolean multiple = matchConditions.size() > 1;
255 for (FeatureMatcherI matcher : matchConditions)
259 String joiner = andConditions ? AND_18N : OR_I18N;
260 sb.append(SPACE).append(joiner.toLowerCase()).append(SPACE);
265 sb.append(OPEN_BRACKET).append(matcher.toString())
266 .append(CLOSE_BRACKET);
270 sb.append(matcher.toString());
273 return sb.toString();
277 public boolean isEmpty()
279 return matchConditions == null || matchConditions.isEmpty();
283 * {@inheritDoc} The output of this method should be parseable by method
284 * <code>fromString<code> to restore the original object.
287 public String toStableString()
289 StringBuilder sb = new StringBuilder();
290 boolean moreThanOne = matchConditions.size() > 1;
291 boolean first = true;
293 for (FeatureMatcherI matcher : matchConditions)
297 String joiner = andConditions ? AND : OR;
298 sb.append(SPACE).append(joiner).append(SPACE);
303 sb.append(OPEN_BRACKET).append(matcher.toStableString())
304 .append(CLOSE_BRACKET);
308 sb.append(matcher.toStableString());
311 return sb.toString();