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.List;
9   import java.util.Map;
10  import java.util.Set;
11  
12  import dev.metaschema.core.datatype.markup.MarkupLine;
13  import dev.metaschema.core.datatype.markup.MarkupMultiline;
14  import dev.metaschema.core.metapath.IMetapathExpression;
15  import dev.metaschema.core.model.IAttributable;
16  import dev.metaschema.core.model.ISource;
17  import dev.metaschema.core.model.constraint.ConstraintInitializationException;
18  import dev.metaschema.core.model.constraint.IConstraint;
19  import dev.metaschema.core.model.constraint.IIndexConstraint;
20  import dev.metaschema.core.model.constraint.IKeyField;
21  import edu.umd.cs.findbugs.annotations.NonNull;
22  import edu.umd.cs.findbugs.annotations.Nullable;
23  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24  
25  /**
26   * Represents an index constraint.
27   * <p>
28   * Uses a set of key fields to build a key and check that that key is unique
29   * compared to other keys.
30   */
31  public final class DefaultIndexConstraint
32      extends AbstractKeyConstraint
33      implements IIndexConstraint {
34    @NonNull
35    private final String name;
36  
37    /**
38     * Construct a new index constraint.
39     *
40     * @param id
41     *          the optional identifier for the constraint
42     * @param formalName
43     *          the constraint's formal name or {@code null} if not provided
44     * @param description
45     *          the constraint's semantic description or {@code null} if not
46     *          provided
47     * @param source
48     *          information about the constraint source
49     * @param level
50     *          the significance of a violation of this constraint
51     * @param target
52     *          the Metapath expression identifying the nodes the constraint targets
53     * @param properties
54     *          a collection of associated properties
55     * @param name
56     *          the name of the index
57     * @param keyFields
58     *          a list of key fields associated with the constraint
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    @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields")
66    public DefaultIndexConstraint(
67        @Nullable String id,
68        @Nullable String formalName,
69        @Nullable MarkupLine description,
70        @NonNull ISource source,
71        @NonNull Level level,
72        @NonNull IMetapathExpression target,
73        @NonNull Map<IAttributable.Key, Set<String>> properties,
74        @NonNull String name,
75        @NonNull List<IKeyField> keyFields,
76        @Nullable String message,
77        @Nullable MarkupMultiline remarks) {
78      super(id, formalName, description, source, level, target, properties, keyFields, message, remarks);
79      if (name.isBlank()) {
80        throw new ConstraintInitializationException(
81            String.format("The index name must be a non-blank string in the constraint %s in '%s'",
82                IConstraint.getConstraintIdentity(this),
83                source.getLocationHint()));
84      }
85      this.name = name;
86    }
87  
88    @Override
89    public String getName() {
90      return name;
91    }
92  }