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    final class Builder
49        extends AbstractKeyConstraintBuilder<Builder, DefaultIndexConstraint> {
50      @NonNull
51      private final String name;
52  
53      private Builder(@NonNull String name) {
54        this.name = name;
55      }
56  
57      @Override
58      protected Builder getThis() {
59        return this;
60      }
61  
62      @NonNull
63      private String getName() {
64        return name;
65      }
66  
67      @Override
68      protected DefaultIndexConstraint newInstance() {
69        return new DefaultIndexConstraint(
70            getId(),
71            getFormalName(),
72            getDescription(),
73            ObjectUtils.notNull(getSource()),
74            getLevel(),
75            getTarget(),
76            getProperties(),
77            getName(),
78            getKeyFields(),
79            getRemarks());
80      }
81    }
82  }