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 IConfigurableMessageConstraint {
20 @Override
21 default Type getType() {
22 return Type.CARDINALITY;
23 }
24
25
26
27
28
29
30
31
32
33 @Nullable
34 Integer getMinOccurs();
35
36
37
38
39
40
41
42
43
44 @Nullable
45 Integer getMaxOccurs();
46
47 @Override
48 default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
49 return visitor.visitCardinalityConstraint(this, state);
50 }
51
52
53
54
55
56
57 @NonNull
58 static Builder builder() {
59 return new Builder();
60 }
61
62
63
64
65
66 final class Builder
67 extends AbstractConfigurableMessageConstraintBuilder<Builder, ICardinalityConstraint> {
68 private Integer minOccurs;
69 private Integer maxOccurs;
70
71 private Builder() {
72
73 }
74
75
76
77
78
79
80
81
82 public Builder minOccurs(int value) {
83 this.minOccurs = value;
84 return this;
85 }
86
87
88
89
90
91
92
93
94 public Builder maxOccurs(int value) {
95 this.maxOccurs = value;
96 return this;
97 }
98
99 @Override
100 protected Builder getThis() {
101 return this;
102 }
103
104 @Override
105 protected void validate() {
106 super.validate();
107
108 if (getMinOccurs() == null && getMaxOccurs() == null) {
109 throw new ConstraintInitializationException("At least one of minOccurs or maxOccurs must be provided.");
110 }
111 }
112
113 private Integer getMinOccurs() {
114 return minOccurs;
115 }
116
117 private Integer getMaxOccurs() {
118 return maxOccurs;
119 }
120
121 @Override
122 protected ICardinalityConstraint newInstance() {
123 return new DefaultCardinalityConstraint(
124 getId(),
125 getFormalName(),
126 getDescription(),
127 ObjectUtils.notNull(getSource()),
128 getLevel(),
129 getTarget(),
130 getProperties(),
131 getMinOccurs(),
132 getMaxOccurs(),
133 getMessage(),
134 getRemarks());
135 }
136 }
137 }