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.DefaultExpectConstraint;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  
11  import edu.umd.cs.findbugs.annotations.NonNull;
12  
13  /**
14   * Represents a rule requiring a Metaschema assembly, field, or flag data
15   * instance to pass a Metapath-based test.
16   * <p>
17   * A custom message can be used to indicate what a test failure signifies.
18   */
19  public interface IExpectConstraint extends IConfigurableMessageConstraint {
20    /**
21     * Get the test to use to validate selected nodes.
22     *
23     * @return the test metapath expression to use
24     */
25    @NonNull
26    String getTest();
27  
28    @Override
29    default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
30      return visitor.visitExpectConstraint(this, state);
31    }
32  
33    /**
34     * Create a new constraint builder.
35     *
36     * @return the builder
37     */
38    @NonNull
39    static Builder builder() {
40      return new Builder();
41    }
42  
43    /**
44     * Provides a builder pattern for constructing a new {@link IExpectConstraint}.
45     */
46    final class Builder
47        extends AbstractConfigurableMessageConstraintBuilder<Builder, IExpectConstraint> {
48      private String test;
49  
50      private Builder() {
51        // disable construction
52      }
53  
54      /**
55       * Use the provided test to validate selected nodes.
56       *
57       * @param test
58       *          the test metapath expression to use
59       * @return this builder
60       */
61      @NonNull
62      public Builder test(@NonNull String test) {
63        this.test = test;
64        return this;
65      }
66  
67      @Override
68      protected Builder getThis() {
69        return this;
70      }
71  
72      @Override
73      protected void validate() {
74        super.validate();
75  
76        ObjectUtils.requireNonNull(getTest());
77      }
78  
79      private String getTest() {
80        return test;
81      }
82  
83      @Override
84      protected IExpectConstraint newInstance() {
85        return new DefaultExpectConstraint(
86            getId(),
87            getFormalName(),
88            getDescription(),
89            ObjectUtils.notNull(getSource()),
90            getLevel(),
91            getTarget(),
92            getProperties(),
93            ObjectUtils.requireNonNull(getTest()),
94            getMessage(),
95            getRemarks());
96      }
97    }
98  }