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    /**
38     * Provides a builder pattern for constructing a new {@link IUniqueConstraint}.
39     */
40    final class Builder
41        extends AbstractKeyConstraintBuilder<Builder, IUniqueConstraint> {
42      private Builder() {
43        // disable construction
44      }
45  
46      @Override
47      protected Builder getThis() {
48        return this;
49      }
50  
51      @Override
52      protected IUniqueConstraint newInstance() {
53        return new DefaultUniqueConstraint(
54            getId(),
55            getFormalName(),
56            getDescription(),
57            ObjectUtils.notNull(getSource()),
58            getLevel(),
59            getTarget(),
60            getProperties(),
61            getKeyFields(),
62            getMessage(),
63            getRemarks());
64      }
65    }
66  }