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
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
58
59
60
61 final class Builder
62 extends AbstractConfigurableMessageConstraintBuilder<Builder, ICardinalityConstraint> {
63 private Integer minOccurs;
64 private Integer maxOccurs;
65
66 private Builder() {
67
68 }
69
70
71
72
73
74
75
76
77 public Builder minOccurs(int value) {
78 this.minOccurs = value;
79 return this;
80 }
81
82
83
84
85
86
87
88
89 public Builder maxOccurs(int value) {
90 this.maxOccurs = value;
91 return this;
92 }
93
94 @Override
95 protected Builder getThis() {
96 return this;
97 }
98
99 @Override
100 protected void validate() {
101 super.validate();
102
103 if (getMinOccurs() == null && getMaxOccurs() == null) {
104 throw new IllegalStateException("At least one of minOccurs or maxOccurs must be provided.");
105 }
106 }
107
108 private Integer getMinOccurs() {
109 return minOccurs;
110 }
111
112 private Integer getMaxOccurs() {
113 return maxOccurs;
114 }
115
116 @Override
117 protected ICardinalityConstraint newInstance() {
118 return new DefaultCardinalityConstraint(
119 getId(),
120 getFormalName(),
121 getDescription(),
122 ObjectUtils.notNull(getSource()),
123 getLevel(),
124 getTarget(),
125 getProperties(),
126 getMinOccurs(),
127 getMaxOccurs(),
128 getMessage(),
129 getRemarks());
130 }
131 }
132 }