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.DefaultIndexHasKeyConstraint;
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 checks that a key generated for a Metaschema data
15   * object exists in a named index that was generated using an
16   * {@link IIndexConstraint}.
17   */
18  public interface IIndexHasKeyConstraint extends IKeyConstraint {
19    /**
20     * The name of the index used to verify cross references.
21     *
22     * @return the index name
23     */
24    @NonNull
25    String getIndexName();
26  
27    @Override
28    default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
29      return visitor.visitIndexHasKeyConstraint(this, state);
30    }
31  
32    /**
33     * Create a new constraint builder.
34     *
35     * @param useIndex
36     *          the index name
37     * @return the builder
38     */
39    @NonNull
40    static Builder builder(@NonNull String useIndex) {
41      return new Builder(useIndex);
42    }
43  
44    final class Builder
45        extends AbstractKeyConstraintBuilder<Builder, IIndexHasKeyConstraint> {
46      @NonNull
47      private final String indexName;
48  
49      private Builder(@NonNull String useIndex) {
50        this.indexName = useIndex;
51      }
52  
53      @Override
54      protected Builder getThis() {
55        return this;
56      }
57  
58      @NonNull
59      private String getIndexName() {
60        return indexName;
61      }
62  
63      @Override
64      protected IIndexHasKeyConstraint newInstance() {
65        return new DefaultIndexHasKeyConstraint(
66            getId(),
67            getFormalName(),
68            getDescription(),
69            ObjectUtils.notNull(getSource()),
70            getLevel(),
71            getTarget(),
72            getProperties(),
73            getIndexName(),
74            getKeyFields(),
75            getRemarks());
76      }
77    }
78  }