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  
22    @Override
23    default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
24      return visitor.visitUniqueConstraint(this, state);
25    }
26  
27    /**
28     * Create a new constraint builder.
29     *
30     * @return the builder
31     */
32    @NonNull
33    static Builder builder() {
34      return new Builder();
35    }
36  
37    final class Builder
38        extends AbstractKeyConstraintBuilder<Builder, IUniqueConstraint> {
39      private Builder() {
40        // disable construction
41      }
42  
43      @Override
44      protected Builder getThis() {
45        return this;
46      }
47  
48      @Override
49      protected IUniqueConstraint newInstance() {
50        return new DefaultUniqueConstraint(
51            getId(),
52            getFormalName(),
53            getDescription(),
54            ObjectUtils.notNull(getSource()),
55            getLevel(),
56            getTarget(),
57            getProperties(),
58            getKeyFields(),
59            getRemarks());
60      }
61    }
62  }