JAL-2808 update spike to latest
[jalview.git] / src / jalview / datamodel / features / FeatureMatcherSet.java
1 package jalview.datamodel.features;
2
3 import jalview.datamodel.SequenceFeature;
4 import jalview.util.MessageManager;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 public class FeatureMatcherSet implements FeatureMatcherSetI
10 {
11   private static final String OR_I18N = MessageManager
12           .getString("label.or");
13
14   private static final String AND_18N = MessageManager
15           .getString("label.and");
16
17   List<FeatureMatcherI> matchConditions;
18
19   boolean andConditions;
20
21   /**
22    * Constructor
23    */
24   public FeatureMatcherSet()
25   {
26     matchConditions = new ArrayList<>();
27   }
28
29   @Override
30   public boolean matches(SequenceFeature feature)
31   {
32     /*
33      * no conditions matches anything
34      */
35     if (matchConditions.isEmpty())
36     {
37       return true;
38     }
39
40     /*
41      * AND until failure
42      */
43     if (andConditions)
44     {
45       for (FeatureMatcherI m : matchConditions)
46       {
47         if (!m.matches(feature))
48         {
49           return false;
50         }
51       }
52       return true;
53     }
54
55     /*
56      * OR until match
57      */
58     for (FeatureMatcherI m : matchConditions)
59     {
60       if (m.matches(feature))
61       {
62         return true;
63       }
64     }
65     return false;
66   }
67
68   @Override
69   public FeatureMatcherSetI and(FeatureMatcherI m)
70   {
71     if (!andConditions && matchConditions.size() > 1)
72     {
73       throw new IllegalStateException("Can't add an AND to OR conditions");
74     }
75     matchConditions.add(m);
76     andConditions = true;
77
78     return this;
79   }
80
81   @Override
82   public FeatureMatcherSetI or(FeatureMatcherI m)
83   {
84     if (andConditions && matchConditions.size() > 1)
85     {
86       throw new IllegalStateException("Can't add an OR to AND conditions");
87     }
88     matchConditions.add(m);
89     andConditions = false;
90
91     return this;
92   }
93
94   @Override
95   public boolean isAnded()
96   {
97     return andConditions;
98   }
99
100   @Override
101   public Iterable<FeatureMatcherI> getMatchers()
102   {
103     return matchConditions;
104   }
105
106   @Override
107   public String toString()
108   {
109     StringBuilder sb = new StringBuilder();
110     boolean first = true;
111     for (FeatureMatcherI matcher : matchConditions)
112     {
113       if (!first)
114       {
115         String joiner = andConditions ? AND_18N : OR_I18N;
116         sb.append(" ").append(joiner.toLowerCase()).append(" ");
117       }
118       first = false;
119       sb.append("(").append(matcher.toString()).append(")");
120     }
121     return sb.toString();
122   }
123
124   @Override
125   public boolean isEmpty()
126   {
127     return matchConditions == null || matchConditions.isEmpty();
128   }
129
130 }