View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
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   * Support for constraints on valued objects (i.e., fields and flags).
50   */
51  class ValueConstraintSupport implements IValueConstrained { // NOPMD - intentional data class
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     * Generate constraints from a {@link BoundFlag} annotation.
73     *
74     * @param valueAnnotation
75     *          the annotation where the constraints are defined
76     * @param source
77     *          information about the source of the constraint
78     */
79    @SuppressWarnings("null")
80    public ValueConstraintSupport( // NOPMD - intentional
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 }