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    /**
45     * Provides a builder pattern for constructing a new
46     * {@link IIndexHasKeyConstraint}.
47     */
48    final class Builder
49        extends AbstractKeyConstraintBuilder<Builder, IIndexHasKeyConstraint> {
50      @NonNull
51      private final String indexName;
52  
53      private Builder(@NonNull String useIndex) {
54        this.indexName = useIndex;
55      }
56  
57      @Override
58      protected Builder getThis() {
59        return this;
60      }
61  
62      @NonNull
63      private String getIndexName() {
64        return indexName;
65      }
66  
67      @Override
68      protected IIndexHasKeyConstraint newInstance() {
69        return new DefaultIndexHasKeyConstraint(
70            getId(),
71            getFormalName(),
72            getDescription(),
73            ObjectUtils.notNull(getSource()),
74            getLevel(),
75            getTarget(),
76            getProperties(),
77            getIndexName(),
78            getKeyFields(),
79            getMessage(),
80            getRemarks());
81      }
82    }
83  }