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  import java.util.regex.Pattern;
11  
12  import dev.metaschema.core.datatype.IDataTypeAdapter;
13  import dev.metaschema.core.datatype.markup.MarkupLine;
14  import dev.metaschema.core.datatype.markup.MarkupMultiline;
15  import dev.metaschema.core.metapath.IMetapathExpression;
16  import dev.metaschema.core.model.IAttributable;
17  import dev.metaschema.core.model.ISource;
18  import dev.metaschema.core.model.constraint.ConstraintInitializationException;
19  import dev.metaschema.core.model.constraint.IConstraint;
20  import dev.metaschema.core.model.constraint.IMatchesConstraint;
21  import edu.umd.cs.findbugs.annotations.NonNull;
22  import edu.umd.cs.findbugs.annotations.Nullable;
23  
24  /**
25   * Represents a matches constraint.
26   * <p>
27   * Enforces a value pattern and/or data type.
28   */
29  public final class DefaultMatchesConstraint
30      extends AbstractConfigurableMessageConstraint
31      implements IMatchesConstraint {
32    private final Pattern pattern;
33    private final IDataTypeAdapter<?> dataType;
34  
35    /**
36     * Construct a new matches constraint.
37     *
38     * @param id
39     *          the optional identifier for the constraint
40     * @param formalName
41     *          the constraint's formal name or {@code null} if not provided
42     * @param description
43     *          the constraint's semantic description or {@code null} if not
44     *          provided
45     * @param source
46     *          information about the constraint source
47     * @param level
48     *          the significance of a violation of this constraint
49     * @param target
50     *          the Metapath expression identifying the nodes the constraint targets
51     * @param properties
52     *          a collection of associated properties
53     * @param pattern
54     *          the value pattern to match or {@code null} if there is no match
55     *          pattern
56     * @param dataType
57     *          the value data type to match or {@code null} if there is no match
58     *          data type
59     * @param message
60     *          an optional message to emit when the constraint is violated
61     * @param remarks
62     *          optional remarks describing the intent of the constraint
63     */
64    @SuppressWarnings("PMD.ExcessiveParameterList")
65    public DefaultMatchesConstraint(
66        @Nullable String id,
67        @Nullable String formalName,
68        @Nullable MarkupLine description,
69        @NonNull ISource source,
70        @NonNull Level level,
71        @NonNull IMetapathExpression target,
72        @NonNull Map<IAttributable.Key, Set<String>> properties,
73        @Nullable Pattern pattern,
74        @Nullable IDataTypeAdapter<?> dataType,
75        @Nullable String message,
76        @Nullable MarkupMultiline remarks) {
77      super(id, formalName, description, source, level, target, properties, message, remarks);
78      if (pattern == null && dataType == null) {
79        throw new ConstraintInitializationException(
80            String.format("The constraint %s must provide a pattern or data type in '%s'",
81                IConstraint.getConstraintIdentity(this),
82                source.getLocationHint()));
83      }
84      this.pattern = pattern;
85      this.dataType = dataType;
86    }
87  
88    @Override
89    public Pattern getPattern() {
90      return pattern;
91    }
92  
93    @Override
94    public IDataTypeAdapter<?> getDataType() {
95      return dataType;
96    }
97  
98  }