1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.model.impl;
7   
8   import gov.nist.secauto.metaschema.core.model.ISource;
9   import gov.nist.secauto.metaschema.core.model.constraint.IModelConstrained;
10  import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained;
11  import gov.nist.secauto.metaschema.databind.model.annotations.AssemblyConstraints;
12  import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints;
13  
14  import java.util.Arrays;
15  
16  import edu.umd.cs.findbugs.annotations.NonNull;
17  import edu.umd.cs.findbugs.annotations.Nullable;
18  
19  public final class ConstraintSupport {
20    private ConstraintSupport() {
21      // disable construction
22    }
23  
24    /**
25     * Generate constraints from a {@link ValueConstraints} annotation on a valued
26     * object (i.e., fields and flags).
27     *
28     * @param valueAnnotation
29     *          the annotation where the constraints are defined
30     * @param source
31     *          information about the source of the constraint
32     * @param set
33     *          the constraint set to parse the constraints into
34     */
35    @SuppressWarnings("null")
36    public static void parse( // NOPMD - intentional
37        @Nullable ValueConstraints valueAnnotation,
38        @NonNull ISource source,
39        @NonNull IValueConstrained set) {
40      if (valueAnnotation != null) {
41        Arrays.stream(valueAnnotation.lets())
42            .map(annotation -> ConstraintFactory.newLetExpression(annotation, source))
43            .forEachOrdered(set::addLetExpression);
44        Arrays.stream(valueAnnotation.allowedValues())
45            .map(annotation -> ConstraintFactory.newAllowedValuesConstraint(annotation, source))
46            .forEachOrdered(set::addConstraint);
47        Arrays.stream(valueAnnotation.matches())
48            .map(annotation -> ConstraintFactory.newMatchesConstraint(annotation, source))
49            .forEachOrdered(set::addConstraint);
50        Arrays.stream(valueAnnotation.indexHasKey())
51            .map(annotation -> ConstraintFactory.newIndexHasKeyConstraint(annotation, source))
52            .forEachOrdered(set::addConstraint);
53        Arrays.stream(valueAnnotation.expect())
54            .map(annotation -> ConstraintFactory.newExpectConstraint(annotation, source))
55            .forEachOrdered(set::addConstraint);
56      }
57    }
58  
59    /**
60     * Generate constraints from a {@link ValueConstraints} annotation on a valued
61     * object (i.e., fields and flags).
62     *
63     * @param assemblyAnnotation
64     *          the annotation where the constraints are defined
65     * @param source
66     *          information about the source of the constraint
67     * @param set
68     *          the constraint set to parse the constraints into
69     */
70    @SuppressWarnings("null")
71    public static void parse( // NOPMD - intentional
72        @Nullable AssemblyConstraints assemblyAnnotation,
73        @NonNull ISource source,
74        @NonNull IModelConstrained set) {
75      if (assemblyAnnotation != null) {
76        Arrays.stream(assemblyAnnotation.index())
77            .map(annotation -> ConstraintFactory.newIndexConstraint(annotation, source))
78            .forEachOrdered(set::addConstraint);
79  
80        Arrays.stream(assemblyAnnotation.unique())
81            .map(annotation -> ConstraintFactory.newUniqueConstraint(annotation, source))
82            .forEachOrdered(set::addConstraint);
83  
84        Arrays.stream(assemblyAnnotation.cardinality())
85            .map(annotation -> ConstraintFactory.newCardinalityConstraint(annotation, source))
86            .forEachOrdered(set::addConstraint);
87      }
88    }
89  }