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