-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAndConstraint.java
More file actions
137 lines (117 loc) · 3.72 KB
/
AndConstraint.java
File metadata and controls
137 lines (117 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package de.vill.model.constraint;
import de.vill.model.building.AutomaticBrackets;
import de.vill.model.building.VariableReference;
import de.vill.util.ConstantSymbols;
import de.vill.exception.ParseError;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class AndConstraint extends Constraint {
private final List<Constraint> children = new ArrayList<>();
public AndConstraint(Constraint... constraints) {
for (Constraint c : constraints) {
if (c != null) {
children.add(c);
}
}
}
public AndConstraint(Constraint left, Constraint right) {
this.children.add(left);
this.children.add(right);
}
public Constraint getLeft() {
if (children.isEmpty()) {
throw new ParseError("Left child can not be returned because there are no children.");
} else {
return children.get(0);
}
}
public Constraint getRight() {
if (children.isEmpty() || children.size() < 2) {
throw new ParseError("Right child can not be returned because there are less than two children.");
} else {
return children.get(children.size() - 1);
}
}
public List<Constraint> getChildren() {
return children;
}
public void setLeft(Constraint left) {
if (children.isEmpty()) {
children.add(left);
} else {
children.set(0, left);
}
}
public void setRight(Constraint right) {
if (children.size() < 2) {
if (children.size() < 1) {
children.add(null);
}
children.add(right);
} else {
children.set(children.size() - 1, right);
}
}
@Override
public String toString(boolean withSubmodels, String currentAlias) {
return children.stream()
.map(c -> AutomaticBrackets.enforceConstraintBracketsIfNecessary(this, c, withSubmodels, currentAlias))
.collect(Collectors.joining(" " + ConstantSymbols.AND + " "));
}
@Override
public List<Constraint> getConstraintSubParts() {
return children;
}
@Override
public void replaceConstraintSubPart(Constraint oldSubConstraint, Constraint newSubConstraint) {
for (int i = 0; i < children.size(); i++) {
if (children.get(i) == oldSubConstraint) {
children.set(i, newSubConstraint);
}
}
}
@Override
public Constraint clone() {
AndConstraint clone = new AndConstraint();
for (Constraint c : children) {
clone.addChild(c.clone());
}
return clone;
}
public void addChild(Constraint constraint) {
if (constraint != null) {
children.add(constraint);
}
}
@Override
public int hashCode(int level) {
final int prime = 31;
int result = prime * level;
for (Constraint c : children) {
result = prime * result + (c == null ? 0 : c.hashCode(1 + level));
}
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
AndConstraint other = (AndConstraint) obj;
return Objects.equals(children, other.children);
}
@Override
public List<VariableReference> getReferences() {
List<VariableReference> references = new ArrayList<>();
for (Constraint c : children) {
references.addAll(c.getReferences());
}
return references;
}
}