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
51
52
53
54
55 @NonNull
56 Map<String, ? extends IAllowedValue> getAllowedValues();
57
58
59
60
61
62
63
64
65 @Nullable
66 default IAllowedValue getAllowedValue(String name) {
67 return getAllowedValues().get(name);
68 }
69
70
71
72
73
74
75
76
77
78
79 boolean isAllowedOther();
80
81
82
83
84
85
86
87 @NonNull
88 Extensible getExtensible();
89
90 @Override
91 default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
92 return visitor.visitAllowedValues(this, state);
93 }
94
95
96
97
98
99
100 @NonNull
101 static Builder builder() {
102 return new Builder();
103 }
104
105
106
107
108
109 final class Builder
110 extends AbstractConstraintBuilder<Builder, IAllowedValuesConstraint> {
111 @NonNull
112 private final Map<String, IAllowedValue> allowedValues = new LinkedHashMap<>();
113 private boolean allowedOther = ALLOW_OTHER_DEFAULT;
114 @NonNull
115 private Extensible extensible = EXTENSIBLE_DEFAULT;
116
117 private Builder() {
118
119 }
120
121
122
123
124
125
126
127
128 @NonNull
129 public Builder allowedValue(@NonNull IAllowedValue allowedValue) {
130 this.allowedValues.put(allowedValue.getValue(), allowedValue);
131 return this;
132 }
133
134
135
136
137
138
139
140
141 @NonNull
142 public Builder allowedValues(@NonNull Map<String, IAllowedValue> allowedValues) {
143 this.allowedValues.putAll(allowedValues);
144 return this;
145 }
146
147
148
149
150
151
152
153
154
155 @NonNull
156 public Builder allowsOther(boolean bool) {
157 this.allowedOther = bool;
158 return this;
159 }
160
161
162
163
164
165
166
167
168
169 @NonNull
170 public Builder extensible(@NonNull Extensible extensible) {
171 this.extensible = extensible;
172 return this;
173 }
174
175 @Override
176 protected Builder getThis() {
177 return this;
178 }
179
180 @NonNull
181 private Map<String, IAllowedValue> getAllowedValues() {
182 return allowedValues;
183 }
184
185 private boolean isAllowedOther() {
186 return allowedOther;
187 }
188
189 @NonNull
190 private Extensible getExtensible() {
191 return extensible;
192 }
193
194 @Override
195 protected IAllowedValuesConstraint newInstance() {
196 return new DefaultAllowedValuesConstraint(
197 getId(),
198 getFormalName(),
199 getDescription(),
200 ObjectUtils.notNull(getSource()),
201 getLevel(),
202 getTarget(),
203 getProperties(),
204 getAllowedValues(),
205 isAllowedOther(),
206 getExtensible(),
207 getRemarks());
208 }
209 }
210 }