44import de .vill .model .building .VariableReference ;
55import de .vill .util .ConstantSymbols ;
66
7+ import de .vill .exception .ParseError ;
8+
79import java .util .ArrayList ;
810import java .util .Arrays ;
911import java .util .List ;
1012import java .util .Objects ;
13+ import java .util .stream .Collectors ;
1114
1215public class AndConstraint extends Constraint {
13- private Constraint left ;
14- private Constraint right ;
16+
17+ private final List <Constraint > children = new ArrayList <>();
18+
19+ public AndConstraint (Constraint ... constraints ) {
20+ for (Constraint c : constraints ) {
21+ if (c != null ) {
22+ children .add (c );
23+ }
24+ }
25+ }
1526
1627 public AndConstraint (Constraint left , Constraint right ) {
17- this .left = left ;
18- this .right = right ;
28+ this .children . add ( left ) ;
29+ this .children . add ( right ) ;
1930 }
2031
2132 public Constraint getLeft () {
22- return left ;
33+ if (children .isEmpty ()) {
34+ throw new ParseError ("Left child can not be returned because there are no children." );
35+ } else {
36+ return children .get (0 );
37+ }
2338 }
2439
2540 public Constraint getRight () {
26- return right ;
41+ if (children .isEmpty () || children .size () < 2 ) {
42+ throw new ParseError ("Right child can not be returned because there are less than two children." );
43+ } else {
44+ return children .get (children .size () - 1 );
45+ }
46+ }
47+
48+ public List <Constraint > getChildren () {
49+ return children ;
2750 }
2851
2952 public void setLeft (Constraint left ) {
30- this .left = left ;
53+ if (children .isEmpty ()) {
54+ children .add (left );
55+ } else {
56+ children .set (0 , left );
57+ }
3158 }
3259
33- public void setRight (Constraint right ){
34- this .right = right ;
60+ public void setRight (Constraint right ) {
61+ if (children .size () < 2 ) {
62+ if (children .size () < 1 ) {
63+ children .add (null );
64+ }
65+ children .add (right );
66+ } else {
67+ children .set (children .size () - 1 , right );
68+ }
3569 }
3670
3771 @ Override
3872 public String toString (boolean withSubmodels , String currentAlias ) {
39- return AutomaticBrackets . enforceConstraintBracketsIfNecessary ( this , left , withSubmodels , currentAlias ) +
40- " " + ConstantSymbols . AND + " " +
41- AutomaticBrackets . enforceConstraintBracketsIfNecessary ( this , right , withSubmodels , currentAlias );
73+ return children . stream ()
74+ . map ( c -> AutomaticBrackets . enforceConstraintBracketsIfNecessary ( this , c , withSubmodels , currentAlias ))
75+ . collect ( Collectors . joining ( " " + ConstantSymbols . AND + " " ) );
4276 }
4377
4478 @ Override
4579 public List <Constraint > getConstraintSubParts () {
46- return Arrays . asList ( left , right ) ;
80+ return children ;
4781 }
4882
4983 @ Override
5084 public void replaceConstraintSubPart (Constraint oldSubConstraint , Constraint newSubConstraint ) {
51- if ( left == oldSubConstraint ) {
52- left = newSubConstraint ;
53- } else if ( right == oldSubConstraint ) {
54- right = newSubConstraint ;
85+ for ( int i = 0 ; i < children . size (); i ++ ) {
86+ if ( children . get ( i ) == oldSubConstraint ) {
87+ children . set ( i , newSubConstraint );
88+ }
5589 }
5690 }
5791
5892 @ Override
5993 public Constraint clone () {
60- return new AndConstraint (left .clone (), right .clone ());
94+ AndConstraint clone = new AndConstraint ();
95+ for (Constraint c : children ) {
96+ clone .addChild (c .clone ());
97+ }
98+ return clone ;
99+ }
100+
101+ public void addChild (Constraint constraint ) {
102+ if (constraint != null ) {
103+ children .add (constraint );
104+ }
61105 }
62106
63107 @ Override
64108 public int hashCode (int level ) {
65109 final int prime = 31 ;
66- int result = prime * level + (left == null ? 0 : left .hashCode (1 + level ));
67- result = prime * result + (right == null ? 0 : right .hashCode (1 + level ));
110+ int result = prime * level ;
111+ for (Constraint c : children ) {
112+ result = prime * result + (c == null ? 0 : c .hashCode (1 + level ));
113+ }
68114 return result ;
69115 }
70116
@@ -77,14 +123,15 @@ public boolean equals(Object obj) {
77123 return false ;
78124 }
79125 AndConstraint other = (AndConstraint ) obj ;
80- return Objects .equals (left , other .left ) && Objects . equals ( right , other . right );
126+ return Objects .equals (children , other .children );
81127 }
82128
83129 @ Override
84130 public List <VariableReference > getReferences () {
85131 List <VariableReference > references = new ArrayList <>();
86- references .addAll (left .getReferences ());
87- references .addAll (right .getReferences ());
132+ for (Constraint c : children ) {
133+ references .addAll (c .getReferences ());
134+ }
88135 return references ;
89136 }
90137}
0 commit comments