1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.model.constraint;
7   
8   import dev.metaschema.core.model.constraint.impl.DefaultIndexConstraint;
9   import dev.metaschema.core.util.ObjectUtils;
10  import edu.umd.cs.findbugs.annotations.NonNull;
11  
12  /**
13   * Represents a rule that generates a key-based index containing references to
14   * data items found in a Metaschema data instance.
15   * <p>
16   * The generated index can be used to check cross-references between Metaschema
17   * data objects using the {@link IIndexHasKeyConstraint}.
18   */
19  public interface IIndexConstraint extends IKeyConstraint {
20    @Override
21    default Type getType() {
22      return Type.INDEX;
23    }
24  
25    /**
26     * Get the identifying name of the index, which is used to refer to the index by
27     * an {@link IIndexHasKeyConstraint}.
28     *
29     * @return the name of the index
30     */
31    @NonNull
32    String getName();
33  
34    @Override
35    default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
36      return visitor.visitIndexConstraint(this, state);
37    }
38  
39    /**
40     * Create a new constraint builder.
41     *
42     * @param name
43     *          the identifier for the index
44     *
45     * @return the builder
46     */
47    @NonNull
48    static Builder builder(@NonNull String name) {
49      return new Builder(name);
50    }
51  
52    /**
53     * Provides a builder pattern for constructing a new {@link IIndexConstraint}.
54     */
55    final class Builder
56        extends AbstractKeyConstraintBuilder<Builder, IIndexConstraint> {
57      @NonNull
58      private final String name;
59  
60      private Builder(@NonNull String name) {
61        // disable construction
62        this.name = name;
63      }
64  
65      @Override
66      protected Builder getThis() {
67        return this;
68      }
69  
70      @NonNull
71      private String getName() {
72        return name;
73      }
74  
75      @Override
76      protected DefaultIndexConstraint newInstance() {
77        return new DefaultIndexConstraint(
78            getId(),
79            getFormalName(),
80            getDescription(),
81            ObjectUtils.notNull(getSource()),
82            getLevel(),
83            getTarget(),
84            getProperties(),
85            getName(),
86            getKeyFields(),
87            getMessage(),
88            getRemarks());
89      }
90    }
91  }