Skip to content

Commit 7c2ec8b

Browse files
author
Julia Pham
committed
feat(and-constraint): allow more than two children
1 parent 660f453 commit 7c2ec8b

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

.DS_Store

6 KB
Binary file not shown.

src/.DS_Store

6 KB
Binary file not shown.

src/main/.DS_Store

6 KB
Binary file not shown.

src/main/java/.DS_Store

6 KB
Binary file not shown.

src/main/java/de/.DS_Store

6 KB
Binary file not shown.

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

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,74 @@
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 AndConstraint extends Constraint {
11-
private Constraint left;
12-
private Constraint right;
1312

14-
public AndConstraint(Constraint left, Constraint right) {
15-
this.left = left;
16-
this.right = right;
13+
//List of children
14+
private final List<Constraint> children = new ArrayList<>();
15+
16+
public AndConstraint(Constraint... constraints) {
17+
for (Constraint c : constraints) {
18+
if (c != null) {
19+
children.add(c);
20+
}
21+
}
1722
}
1823

19-
public Constraint getLeft() {
20-
return left;
24+
public AndConstraint(Constraint left, Constraint right) {
25+
this(new Constraint[]{left, right});
2126
}
2227

23-
public Constraint getRight() {
24-
return right;
28+
public List<Constraint> getChildren() {
29+
return children;
2530
}
2631

2732
@Override
2833
public String toString(boolean withSubmodels, String currentAlias) {
29-
return left.toString(withSubmodels, currentAlias) +
30-
" & " +
31-
right.toString(withSubmodels, currentAlias);
34+
return
35+
// Constraint-Stream - jeder constraint in einen String umgewandelt und mit einem & verknüpft
36+
children.stream()
37+
.map(c -> c.toString(withSubmodels, currentAlias))
38+
.collect(Collectors.joining(" & "));
3239
}
3340

3441
@Override
3542
public List<Constraint> getConstraintSubParts() {
36-
return Arrays.asList(left, right);
43+
return new ArrayList<>(children);
3744
}
3845

3946
@Override
4047
public void replaceConstraintSubPart(Constraint oldSubConstraint, Constraint newSubConstraint) {
41-
if (left == oldSubConstraint) {
42-
left = newSubConstraint;
43-
} else if (right == oldSubConstraint) {
44-
right = newSubConstraint;
48+
for (int i = 0; i< children.size(); i++) {
49+
if (children.get(i) == oldSubConstraint){
50+
children.set(i, newSubConstraint);
51+
}
4552
}
4653
}
4754

4855
@Override
4956
public Constraint clone() {
50-
return new AndConstraint(left.clone(), right.clone());
57+
AndConstraint clone = new AndConstraint();
58+
for (Constraint c : children) {
59+
clone.addChild(c.clone());
60+
}
61+
return clone;
62+
}
63+
64+
public void addChild(Constraint constraint){
65+
if (constraint != null) {
66+
children.add(constraint);
67+
}
5168
}
5269

5370
@Override
5471
public int hashCode(int level) {
5572
final int prime = 31;
56-
int result = prime * level + (left == null ? 0 : left.hashCode(1 + level));
57-
result = prime * result + (right == null ? 0 : right.hashCode(1 + level));
73+
int result = prime * level;
74+
for(Constraint c: children) {
75+
result = prime * result + (c == null ? 0 : c.hashCode(1 + level));
76+
}
5877
return result;
5978
}
6079

@@ -67,14 +86,15 @@ public boolean equals(Object obj) {
6786
return false;
6887
}
6988
AndConstraint other = (AndConstraint) obj;
70-
return Objects.equals(left, other.left) && Objects.equals(right, other.right);
89+
return Objects.equals(children, other.children);
7190
}
7291

7392
@Override
7493
public List<VariableReference> getReferences() {
7594
List<VariableReference> references = new ArrayList<>();
76-
references.addAll(left.getReferences());
77-
references.addAll(right.getReferences());
95+
for(Constraint c: children){
96+
references.addAll(c.getReferences());
97+
}
7898
return references;
7999
}
80100
}

0 commit comments

Comments
 (0)