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    /**
22     * Get the name of the index, which is used to refer to the index by an
23     * {@link IIndexHasKeyConstraint}.
24     *
25     * @return the name of the index
26     */
27    @NonNull
28    String getName();
29  
30    @Override
31    default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
32      return visitor.visitIndexConstraint(this, state);
33    }
34  
35    /**
36     * Create a new constraint builder.
37     *
38     * @param name
39     *          the identifier for the index
40     *
41     * @return the builder
42     */
43    @NonNull
44    static Builder builder(@NonNull String name) {
45      return new Builder(name);
46    }
47  
48    /**
49     * Provides a builder pattern for constructing a new {@link IIndexConstraint}.
50     */
51    final class Builder
52        extends AbstractKeyConstraintBuilder<Builder, IIndexConstraint> {
53      @NonNull
54      private final String name;
55  
56      private Builder(@NonNull String name) {
57        this.name = name;
58      }
59  
60      @Override
61      protected Builder getThis() {
62        return this;
63      }
64  
65      @NonNull
66      private String getName() {
67        return name;
68      }
69  
70      @Override
71      protected DefaultIndexConstraint newInstance() {
72        return new DefaultIndexConstraint(
73            getId(),
74            getFormalName(),
75            getDescription(),
76            ObjectUtils.notNull(getSource()),
77            getLevel(),
78            getTarget(),
79            getProperties(),
80            getName(),
81            getKeyFields(),
82            getMessage(),
83            getRemarks());
84      }
85    }
86  }