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