Skip to content

Commit d5155b7

Browse files
author
Julia Pham
committed
fix: implemented OrConstraint like AndConstraint allowing multiple children
1 parent a7daa3a commit d5155b7

File tree

1 file changed

+57
-20
lines changed

1 file changed

+57
-20
lines changed

src/main/java/de/vill/model/constraint/OrConstraint.java

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,92 @@
66
import java.util.Arrays;
77
import java.util.List;
88
import java.util.Objects;
9+
import java.util.stream.Collectors;
910

1011
public class OrConstraint extends Constraint {
1112

12-
private Constraint left;
13-
private Constraint right;
13+
private final List<Constraint> children = new ArrayList<>();
14+
15+
public OrConstraint(Constraint... constraints) {
16+
for (Constraint c : constraints) {
17+
if (c != null) {
18+
children.add(c);
19+
}
20+
}
21+
}
1422

1523
public OrConstraint(Constraint left, Constraint right) {
16-
this.left = left;
17-
this.right = right;
24+
this.children.add(left);
25+
this.children.add(right);
1826
}
1927

2028
public Constraint getLeft() {
21-
return left;
29+
if (children.isEmpty()){
30+
return null;
31+
}
32+
else{
33+
return children.get(0);
34+
}
2235
}
2336

2437
public Constraint getRight() {
25-
return right;
38+
if (children.isEmpty() || children.size() < 2){
39+
return null;
40+
}
41+
else{
42+
return children.get(children.size() - 1);
43+
}
44+
}
45+
46+
public List<Constraint> getChildren() {
47+
return children;
2648
}
2749

2850
@Override
2951
public String toString(boolean withSubmodels, String currentAlias) {
30-
return left.toString(withSubmodels, currentAlias) +
31-
" | " +
32-
right.toString(withSubmodels, currentAlias);
52+
return
53+
// Constraint-Stream - jeder constraint in einen String umgewandelt und mit einem & verknüpft
54+
children.stream()
55+
.map(c -> c.toString(withSubmodels, currentAlias))
56+
.collect(Collectors.joining(" | "));
3357
}
3458

3559
@Override
3660
public List<Constraint> getConstraintSubParts() {
37-
return Arrays.asList(left, right);
61+
return new ArrayList<>(children);
3862
}
3963

4064
@Override
4165
public void replaceConstraintSubPart(Constraint oldSubConstraint, Constraint newSubConstraint) {
42-
if (left == oldSubConstraint) {
43-
left = newSubConstraint;
44-
} else if (right == oldSubConstraint) {
45-
right = newSubConstraint;
66+
for (int i = 0; i< children.size(); i++) {
67+
if (children.get(i) == oldSubConstraint){
68+
children.set(i, newSubConstraint);
69+
}
4670
}
4771
}
4872

4973
@Override
5074
public Constraint clone() {
51-
return new OrConstraint(left.clone(), right.clone());
75+
OrConstraint clone = new OrConstraint();
76+
for (Constraint c : children) {
77+
clone.addChild(c.clone());
78+
}
79+
return clone;
80+
}
81+
82+
public void addChild(Constraint constraint){
83+
if (constraint != null) {
84+
children.add(constraint);
85+
}
5286
}
5387

5488
@Override
5589
public int hashCode(int level) {
5690
final int prime = 31;
57-
int result = prime * level + (left == null ? 0 : left.hashCode(1 + level));
58-
result = prime * result + (right == null ? 0 : right.hashCode(1 + level));
91+
int result = prime * level;
92+
for(Constraint c: children) {
93+
result = prime * result + (c == null ? 0 : c.hashCode(1 + level));
94+
}
5995
return result;
6096
}
6197

@@ -68,14 +104,15 @@ public boolean equals(Object obj) {
68104
return false;
69105
}
70106
OrConstraint other = (OrConstraint) obj;
71-
return Objects.equals(left, other.left) && Objects.equals(right, other.right);
107+
return Objects.equals(children, other.children);
72108
}
73109

74110
@Override
75111
public List<VariableReference> getReferences() {
76112
List<VariableReference> references = new ArrayList<>();
77-
references.addAll(left.getReferences());
78-
references.addAll(right.getReferences());
113+
for(Constraint c: children){
114+
references.addAll(c.getReferences());
115+
}
79116
return references;
80117
}
81118
}

0 commit comments

Comments
 (0)