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.DefaultUniqueConstraint;
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 requires all matching data items found in a Metaschema
15   * data instance to have a unique key.
16   * <p>
17   * This rule is similar to the {@link IIndexConstraint} in how the keys are
18   * generated, but this constraint type does not persist a named index.
19   */
20  public interface IUniqueConstraint extends IKeyConstraint {
21    @Override
22    default Type getType() {
23      return Type.UNIQUE;
24    }
25  
26    @Override
27    default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
28      return visitor.visitUniqueConstraint(this, state);
29    }
30  
31    /**
32     * Create a new constraint builder.
33     *
34     * @return the builder
35     */
36    @NonNull
37    static Builder builder() {
38      return new Builder();
39    }
40  
41    /**
42     * Provides a builder pattern for constructing a new {@link IUniqueConstraint}.
43     */
44    final class Builder
45        extends AbstractKeyConstraintBuilder<Builder, IUniqueConstraint> {
46      private Builder() {
47        // disable construction
48      }
49  
50      @Override
51      protected Builder getThis() {
52        return this;
53      }
54  
55      @Override
56      protected IUniqueConstraint newInstance() {
57        return new DefaultUniqueConstraint(
58            getId(),
59            getFormalName(),
60            getDescription(),
61            ObjectUtils.notNull(getSource()),
62            getLevel(),
63            getTarget(),
64            getProperties(),
65            getKeyFields(),
66            getMessage(),
67            getRemarks());
68      }
69    }
70  }