1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.model.constraint;
7   
8   import dev.metaschema.core.model.constraint.impl.DefaultUniqueConstraint;
9   import dev.metaschema.core.util.ObjectUtils;
10  import edu.umd.cs.findbugs.annotations.NonNull;
11  
12  /**
13   * Represents a rule that requires all matching data items found in a Metaschema
14   * data instance to have a unique key.
15   * <p>
16   * This rule is similar to the {@link IIndexConstraint} in how the keys are
17   * generated, but this constraint type does not persist a named index.
18   */
19  public interface IUniqueConstraint extends IKeyConstraint {
20    @Override
21    default Type getType() {
22      return Type.UNIQUE;
23    }
24  
25    @Override
26    default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
27      return visitor.visitUniqueConstraint(this, state);
28    }
29  
30    /**
31     * Create a new constraint builder.
32     *
33     * @return the builder
34     */
35    @NonNull
36    static Builder builder() {
37      return new Builder();
38    }
39  
40    /**
41     * Provides a builder pattern for constructing a new {@link IUniqueConstraint}.
42     */
43    final class Builder
44        extends AbstractKeyConstraintBuilder<Builder, IUniqueConstraint> {
45      private Builder() {
46        // disable construction
47      }
48  
49      @Override
50      protected Builder getThis() {
51        return this;
52      }
53  
54      @Override
55      protected IUniqueConstraint newInstance() {
56        return new DefaultUniqueConstraint(
57            getId(),
58            getFormalName(),
59            getDescription(),
60            ObjectUtils.notNull(getSource()),
61            getLevel(),
62            getTarget(),
63            getProperties(),
64            getKeyFields(),
65            getMessage(),
66            getRemarks());
67      }
68    }
69  }