1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.model.constraint.impl;
7   
8   import java.util.Map;
9   import java.util.Set;
10  
11  import dev.metaschema.core.datatype.markup.MarkupLine;
12  import dev.metaschema.core.datatype.markup.MarkupMultiline;
13  import dev.metaschema.core.metapath.IMetapathExpression;
14  import dev.metaschema.core.model.IAttributable;
15  import dev.metaschema.core.model.ISource;
16  import dev.metaschema.core.model.constraint.IAllowedValue;
17  import dev.metaschema.core.model.constraint.IAllowedValuesConstraint;
18  import edu.umd.cs.findbugs.annotations.NonNull;
19  import edu.umd.cs.findbugs.annotations.Nullable;
20  
21  /**
22   * Represents an allowed values constraint.
23   * <p>
24   * Ensures that a target instance's value matches one of the allowed values.
25   * This match is required if {@link #isAllowedOther()} is {@code false},
26   * otherwise the constraint will generate a validation warning message if the
27   * target instance's value does not match any of the associated allowed value
28   * constraints targeting it.
29   */
30  public final class DefaultAllowedValuesConstraint
31      extends AbstractConstraint
32      implements IAllowedValuesConstraint {
33    private final boolean allowedOther;
34    @NonNull
35    private final Extensible extensible;
36    @NonNull
37    private final Map<String, IAllowedValue> allowedValues;
38  
39    /**
40     * Construct a new allowed values constraint.
41     *
42     * @param id
43     *          the optional identifier for the constraint
44     * @param formalName
45     *          the constraint's formal name or {@code null} if not provided
46     * @param description
47     *          the constraint's semantic description or {@code null} if not
48     *          provided
49     * @param source
50     *          information about the constraint source
51     * @param level
52     *          the significance of a violation of this constraint
53     * @param target
54     *          the Metapath expression identifying the nodes the constraint targets
55     * @param properties
56     *          a collection of associated properties
57     * @param allowedValues
58     *          the list of allowed values for this constraint
59     * @param allowedOther
60     *          when {@code true} values other than the values specified by
61     *          {@code allowedValues} are allowed, or disallowed if {@code false}
62     * @param extensible
63     *          indicates the degree to which extended values should be allowed
64     * @param remarks
65     *          optional remarks describing the intent of the constraint
66     */
67    public DefaultAllowedValuesConstraint( // NOPMD necessary
68        @Nullable String id,
69        @Nullable String formalName,
70        @Nullable MarkupLine description,
71        @NonNull ISource source,
72        @NonNull Level level,
73        @NonNull IMetapathExpression target,
74        @NonNull Map<IAttributable.Key, Set<String>> properties,
75        @NonNull Map<String, IAllowedValue> allowedValues,
76        boolean allowedOther,
77        @NonNull Extensible extensible,
78        @Nullable MarkupMultiline remarks) {
79      super(id, formalName, description, source, level, target, properties, remarks);
80      this.allowedValues = allowedValues;
81      this.allowedOther = allowedOther;
82      this.extensible = extensible;
83    }
84  
85    @Override
86    public Map<String, IAllowedValue> getAllowedValues() {
87      return allowedValues;
88    }
89  
90    @Override
91    public boolean isAllowedOther() {
92      return allowedOther;
93    }
94  
95    @Override
96    public Extensible getExtensible() {
97      return extensible;
98    }
99  }