1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.metaschema.core.model.constraint;
28
29 import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
30 import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
31 import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
32 import gov.nist.secauto.metaschema.core.util.CollectionUtil;
33 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
34
35 import java.util.LinkedHashMap;
36 import java.util.LinkedHashSet;
37 import java.util.Map;
38 import java.util.Objects;
39 import java.util.Set;
40
41 import javax.xml.namespace.QName;
42
43 import edu.umd.cs.findbugs.annotations.NonNull;
44 import edu.umd.cs.findbugs.annotations.Nullable;
45
46 public abstract class AbstractConstraint implements IConstraint {
47 @Nullable
48 private final String id;
49 @Nullable
50 private final String formalName;
51 @Nullable
52 private final MarkupLine description;
53 @NonNull
54 private final ISource source;
55 @NonNull
56 private final Level level;
57 @NonNull
58 private final MetapathExpression target;
59 @Nullable
60 private final MarkupMultiline remarks;
61 @NonNull
62 private final Map<QName, Set<String>> properties;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 protected AbstractConstraint(
86 @Nullable String id,
87 @Nullable String formalName,
88 @Nullable MarkupLine description,
89 @NonNull ISource source,
90 @NonNull Level level,
91 @NonNull MetapathExpression target,
92 @NonNull Map<QName, Set<String>> properties,
93 @Nullable MarkupMultiline remarks) {
94 Objects.requireNonNull(target);
95 this.id = id;
96 this.formalName = formalName;
97 this.description = description;
98 this.source = source;
99 this.level = ObjectUtils.requireNonNull(level, "level");
100 this.target = ObjectUtils.requireNonNull(target, "target");
101 this.properties = properties;
102 this.remarks = remarks;
103 }
104
105 @Override
106 public String getId() {
107 return id;
108 }
109
110 @Override
111 public MarkupLine getDescription() {
112 return description;
113 }
114
115 @Override
116 public String getFormalName() {
117 return formalName;
118 }
119
120 @Override
121 public ISource getSource() {
122 return source;
123 }
124
125 @Override
126 @NonNull
127 public Level getLevel() {
128 return level;
129 }
130
131 @Override
132 public MetapathExpression getTarget() {
133 return target;
134 }
135
136 @Override
137 public Map<QName, Set<String>> getProperties() {
138 return CollectionUtil.unmodifiableMap(properties);
139 }
140
141 @Override
142 public MarkupMultiline getRemarks() {
143 return remarks;
144 }
145
146 public abstract static class AbstractConstraintBuilder<
147 T extends AbstractConstraintBuilder<T,
148 R>,
149 R extends AbstractConstraint> {
150 private String id;
151 private String formalName;
152 private MarkupLine description;
153 private ISource source;
154 @NonNull
155 private Level level = IConstraint.DEFAULT_LEVEL;
156 @NonNull
157 private MetapathExpression target = IConstraint.DEFAULT_TARGET;
158 @NonNull
159 private Map<QName, Set<String>> properties = new LinkedHashMap<>();
160 private MarkupMultiline remarks;
161
162 protected abstract T getThis();
163
164 public T identifier(@NonNull String id) {
165 this.id = id;
166 return getThis();
167 }
168
169 public T formalName(@NonNull String name) {
170 this.formalName = name;
171 return getThis();
172 }
173
174 public T description(@NonNull MarkupLine description) {
175 this.description = description;
176 return getThis();
177 }
178
179 public T source(@NonNull ISource source) {
180 this.source = source;
181 return getThis();
182 }
183
184 public T level(@NonNull Level level) {
185 this.level = level;
186 return getThis();
187 }
188
189 public T target(@NonNull MetapathExpression target) {
190 this.target = target;
191 return getThis();
192 }
193
194 public T properties(@NonNull Map<QName, Set<String>> properties) {
195 this.properties = properties;
196 return getThis();
197 }
198
199 public T property(@NonNull QName name, @NonNull String value) {
200 return property(name, CollectionUtil.singleton(value));
201 }
202
203 public T property(@NonNull QName name, @NonNull Set<String> newValues) {
204 Set<String> existingValues = properties.get(name);
205 if (existingValues == null) {
206 existingValues = new LinkedHashSet<>();
207 properties.put(name, existingValues);
208 }
209
210 existingValues.addAll(newValues);
211 return getThis();
212 }
213
214 public T remarks(@NonNull MarkupMultiline remarks) {
215 this.remarks = remarks;
216 return getThis();
217 }
218
219 protected void validate() {
220 ObjectUtils.requireNonNull(getSource());
221 }
222
223 @NonNull
224 protected abstract R newInstance();
225
226 @NonNull
227 public R build() {
228 validate();
229 return newInstance();
230 }
231
232 protected String getId() {
233 return id;
234 }
235
236 protected String getFormalName() {
237 return formalName;
238 }
239
240 protected MarkupLine getDescription() {
241 return description;
242 }
243
244 protected ISource getSource() {
245 return source;
246 }
247
248 @NonNull
249 protected Level getLevel() {
250 return level;
251 }
252
253 @NonNull
254 protected MetapathExpression getTarget() {
255 return target;
256 }
257
258 @NonNull
259 protected Map<QName, Set<String>> getProperties() {
260 return properties;
261 }
262
263 protected MarkupMultiline getRemarks() {
264 return remarks;
265 }
266 }
267 }