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