1
2
3
4
5
6 package gov.nist.secauto.metaschema.core.model.constraint;
7
8 import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute;
9 import gov.nist.secauto.metaschema.core.model.constraint.impl.DefaultCardinalityConstraint;
10 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
11
12 import edu.umd.cs.findbugs.annotations.NonNull;
13 import edu.umd.cs.findbugs.annotations.Nullable;
14
15
16
17
18
19 public interface ICardinalityConstraint extends IConstraint {
20
21
22
23
24
25
26
27
28 @Nullable
29 Integer getMinOccurs();
30
31
32
33
34
35
36
37
38
39 @Nullable
40 Integer getMaxOccurs();
41
42 @Override
43 default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
44 return visitor.visitCardinalityConstraint(this, state);
45 }
46
47
48
49
50
51
52 @NonNull
53 static Builder builder() {
54 return new Builder();
55 }
56
57 final class Builder
58 extends AbstractConstraintBuilder<Builder, ICardinalityConstraint> {
59 private Integer minOccurs;
60 private Integer maxOccurs;
61
62 private Builder() {
63
64 }
65
66
67
68
69
70
71
72
73 public Builder minOccurs(int value) {
74 this.minOccurs = value;
75 return this;
76 }
77
78
79
80
81
82
83
84
85 public Builder maxOccurs(int value) {
86 this.maxOccurs = value;
87 return this;
88 }
89
90 @Override
91 protected Builder getThis() {
92 return this;
93 }
94
95 @Override
96 protected void validate() {
97 super.validate();
98
99 if (getMinOccurs() == null && getMaxOccurs() == null) {
100 throw new IllegalStateException("At least one of minOccurs or maxOccurs must be provided.");
101 }
102 }
103
104 private Integer getMinOccurs() {
105 return minOccurs;
106 }
107
108 private Integer getMaxOccurs() {
109 return maxOccurs;
110 }
111
112 @Override
113 protected ICardinalityConstraint newInstance() {
114 return new DefaultCardinalityConstraint(
115 getId(),
116 getFormalName(),
117 getDescription(),
118 ObjectUtils.notNull(getSource()),
119 getLevel(),
120 getTarget(),
121 getProperties(),
122 getMinOccurs(),
123 getMaxOccurs(),
124 getRemarks());
125 }
126 }
127 }