1
2
3
4
5
6 package gov.nist.secauto.metaschema.core.model.constraint;
7
8 import gov.nist.secauto.metaschema.core.model.constraint.impl.DefaultAllowedValuesConstraint;
9 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10
11 import java.util.LinkedHashMap;
12 import java.util.Map;
13
14 import edu.umd.cs.findbugs.annotations.NonNull;
15 import edu.umd.cs.findbugs.annotations.Nullable;
16
17
18
19
20
21 public interface IAllowedValuesConstraint extends IConstraint {
22
23
24
25 boolean ALLOW_OTHER_DEFAULT = false;
26
27
28
29 @NonNull
30 Extensible EXTENSIBLE_DEFAULT = Extensible.EXTERNAL;
31
32
33
34
35 enum Extensible {
36
37
38
39 EXTERNAL,
40
41
42
43 MODEL,
44
45
46
47 NONE;
48 }
49
50 @Override
51 default Type getType() {
52 return Type.ALLOWED_VALUES;
53 }
54
55
56
57
58
59
60 @NonNull
61 Map<String, ? extends IAllowedValue> getAllowedValues();
62
63
64
65
66
67
68
69
70 @Nullable
71 default IAllowedValue getAllowedValue(String name) {
72 return getAllowedValues().get(name);
73 }
74
75
76
77
78
79
80
81
82
83
84 boolean isAllowedOther();
85
86
87
88
89
90
91
92 @NonNull
93 Extensible getExtensible();
94
95 @Override
96 default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
97 return visitor.visitAllowedValues(this, state);
98 }
99
100
101
102
103
104
105 @NonNull
106 static Builder builder() {
107 return new Builder();
108 }
109
110
111
112
113
114 final class Builder
115 extends AbstractConstraintBuilder<Builder, IAllowedValuesConstraint> {
116 @NonNull
117 private final Map<String, IAllowedValue> allowedValues = new LinkedHashMap<>();
118 private boolean allowedOther = ALLOW_OTHER_DEFAULT;
119 @NonNull
120 private Extensible extensible = EXTENSIBLE_DEFAULT;
121
122 private Builder() {
123
124 }
125
126
127
128
129
130
131
132
133 @NonNull
134 public Builder allowedValue(@NonNull IAllowedValue allowedValue) {
135 this.allowedValues.put(allowedValue.getValue(), allowedValue);
136 return this;
137 }
138
139
140
141
142
143
144
145
146 @NonNull
147 public Builder allowedValues(@NonNull Map<String, IAllowedValue> allowedValues) {
148 this.allowedValues.putAll(allowedValues);
149 return this;
150 }
151
152
153
154
155
156
157
158
159
160 @NonNull
161 public Builder allowsOther(boolean bool) {
162 this.allowedOther = bool;
163 return this;
164 }
165
166
167
168
169
170
171
172
173
174 @NonNull
175 public Builder extensible(@NonNull Extensible extensible) {
176 this.extensible = extensible;
177 return this;
178 }
179
180 @Override
181 protected Builder getThis() {
182 return this;
183 }
184
185 @NonNull
186 private Map<String, IAllowedValue> getAllowedValues() {
187 return allowedValues;
188 }
189
190 private boolean isAllowedOther() {
191 return allowedOther;
192 }
193
194 @NonNull
195 private Extensible getExtensible() {
196 return extensible;
197 }
198
199 @Override
200 protected IAllowedValuesConstraint newInstance() {
201 return new DefaultAllowedValuesConstraint(
202 getId(),
203 getFormalName(),
204 getDescription(),
205 ObjectUtils.notNull(getSource()),
206 getLevel(),
207 getTarget(),
208 getProperties(),
209 getAllowedValues(),
210 isAllowedOther(),
211 getExtensible(),
212 getRemarks());
213 }
214 }
215 }