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.databind.model;
28
29 import gov.nist.secauto.metaschema.core.metapath.MetapathException;
30 import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValuesConstraint;
31 import gov.nist.secauto.metaschema.core.model.constraint.IConstraint;
32 import gov.nist.secauto.metaschema.core.model.constraint.IConstraint.ISource;
33 import gov.nist.secauto.metaschema.core.model.constraint.IExpectConstraint;
34 import gov.nist.secauto.metaschema.core.model.constraint.IIndexHasKeyConstraint;
35 import gov.nist.secauto.metaschema.core.model.constraint.IMatchesConstraint;
36 import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;
37 import gov.nist.secauto.metaschema.databind.model.annotations.BoundFlag;
38 import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints;
39
40 import java.util.Arrays;
41 import java.util.LinkedList;
42 import java.util.List;
43 import java.util.stream.Collectors;
44
45 import edu.umd.cs.findbugs.annotations.NonNull;
46 import edu.umd.cs.findbugs.annotations.Nullable;
47
48
49
50
51 class ValueConstraintSupport implements IValueConstrained {
52 @NonNull
53 private final List<IConstraint> constraints;
54 @NonNull
55 private final List<IAllowedValuesConstraint> allowedValuesConstraints;
56 @NonNull
57 private final List<IMatchesConstraint> matchesConstraints;
58 @NonNull
59 private final List<IIndexHasKeyConstraint> indexHasKeyConstraints;
60 @NonNull
61 private final List<IExpectConstraint> expectConstraints;
62
63 public ValueConstraintSupport() {
64 this.constraints = new LinkedList<>();
65 this.allowedValuesConstraints = new LinkedList<>();
66 this.matchesConstraints = new LinkedList<>();
67 this.indexHasKeyConstraints = new LinkedList<>();
68 this.expectConstraints = new LinkedList<>();
69 }
70
71
72
73
74
75
76
77
78
79 @SuppressWarnings("null")
80 public ValueConstraintSupport(
81 @Nullable ValueConstraints valueAnnotation,
82 @NonNull ISource source) {
83 if (valueAnnotation == null) {
84 this.allowedValuesConstraints = new LinkedList<>();
85 this.matchesConstraints = new LinkedList<>();
86 this.indexHasKeyConstraints = new LinkedList<>();
87 this.expectConstraints = new LinkedList<>();
88 } else {
89 try {
90 allowedValuesConstraints = Arrays.stream(valueAnnotation.allowedValues())
91 .map(annotation -> ConstraintFactory.newAllowedValuesConstraint(annotation, source))
92 .collect(Collectors.toCollection(LinkedList::new));
93
94 matchesConstraints = Arrays.stream(valueAnnotation.matches())
95 .map(annotation -> ConstraintFactory.newMatchesConstraint(annotation, source))
96 .collect(Collectors.toCollection(LinkedList::new));
97
98 indexHasKeyConstraints = Arrays.stream(valueAnnotation.indexHasKey())
99 .map(annotation -> ConstraintFactory.newIndexHasKeyConstraint(annotation, source))
100 .collect(Collectors.toCollection(LinkedList::new));
101
102 expectConstraints = Arrays.stream(valueAnnotation.expect())
103 .map(annotation -> ConstraintFactory.newExpectConstraint(annotation, source))
104 .collect(Collectors.toCollection(LinkedList::new));
105 } catch (MetapathException ex) {
106 throw new MetapathException(
107 String.format("Unable to compile a Metapath in '%s'. %s", source.getSource(), ex.getLocalizedMessage()),
108 ex);
109 }
110 }
111 constraints = new LinkedList<>();
112 constraints.addAll(allowedValuesConstraints);
113 constraints.addAll(matchesConstraints);
114 constraints.addAll(indexHasKeyConstraints);
115 constraints.addAll(expectConstraints);
116 }
117
118 @Override
119 public List<IConstraint> getConstraints() {
120 return constraints;
121 }
122
123 @Override
124 public List<IAllowedValuesConstraint> getAllowedValuesConstraints() {
125 return allowedValuesConstraints;
126 }
127
128 @Override
129 public List<IMatchesConstraint> getMatchesConstraints() {
130 return matchesConstraints;
131 }
132
133 @Override
134 public List<IIndexHasKeyConstraint> getIndexHasKeyConstraints() {
135 return indexHasKeyConstraints;
136 }
137
138 @Override
139 public List<IExpectConstraint> getExpectConstraints() {
140 return expectConstraints;
141 }
142
143 @Override
144 public void addConstraint(@NonNull IAllowedValuesConstraint constraint) {
145 constraints.add(constraint);
146 allowedValuesConstraints.add(constraint);
147 }
148
149 @Override
150 public void addConstraint(@NonNull IMatchesConstraint constraint) {
151 constraints.add(constraint);
152 matchesConstraints.add(constraint);
153 }
154
155 @Override
156 public void addConstraint(@NonNull IIndexHasKeyConstraint constraint) {
157 constraints.add(constraint);
158 indexHasKeyConstraints.add(constraint);
159 }
160
161 @Override
162 public void addConstraint(@NonNull IExpectConstraint constraint) {
163 constraints.add(constraint);
164 expectConstraints.add(constraint);
165 }
166 }