1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }