View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.parsers.bool;
20  
21  import java.util.Collection;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Locale;
25  
26  import org.jdom2.Element;
27  
28  /**
29   * @author Frank Lützenkirchen
30   */
31  public abstract class MCRSetCondition<T> implements MCRCondition<T> {
32  
33      public static final String AND = "and";
34  
35      public static final String OR = "or";
36  
37      protected String operator;
38  
39      protected List<MCRCondition<T>> children = new LinkedList<>();
40  
41      protected MCRSetCondition(String operator) {
42          this.operator = operator;
43      }
44  
45      public String getOperator() {
46          return operator;
47      }
48  
49      public MCRSetCondition<T> addChild(MCRCondition<T> condition) {
50          children.add(condition);
51          return this;
52      }
53  
54      public MCRSetCondition<T> addAll(Collection<MCRCondition<T>> conditions) {
55          children.addAll(conditions);
56          return this;
57      }
58  
59      public List<MCRCondition<T>> getChildren() {
60          return children;
61      }
62  
63      @Override
64      public String toString() {
65          StringBuilder sb = new StringBuilder();
66          for (int i = 0; i < children.size(); i++) {
67              sb.append("(").append(children.get(i)).append(")");
68              if (i < children.size() - 1) {
69                  sb.append(' ').append(operator.toUpperCase(Locale.ROOT)).append(' ');
70              }
71          }
72          return sb.toString();
73      }
74  
75      public Element toXML() {
76          Element cond = new Element("boolean").setAttribute("operator", operator);
77          for (MCRCondition<T> child : children) {
78              cond.addContent(child.toXML());
79          }
80          return cond;
81      }
82  
83      public abstract boolean evaluate(T o);
84  }