View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.metaschema.core.model.constraint;
28  
29  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
30  import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
31  import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
32  import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
33  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
34  
35  import java.util.LinkedHashMap;
36  import java.util.Map;
37  import java.util.Set;
38  
39  import javax.xml.namespace.QName;
40  
41  import edu.umd.cs.findbugs.annotations.NonNull;
42  import edu.umd.cs.findbugs.annotations.Nullable;
43  
44  public final class DefaultAllowedValuesConstraint
45      extends AbstractConstraint
46      implements IAllowedValuesConstraint {
47    private final boolean allowedOther;
48    @NonNull
49    private final Extensible extensible;
50    @NonNull
51    private final Map<String, DefaultAllowedValue> allowedValues;
52  
53    /**
54     * Construct a new allowed values constraint which ensures that a target
55     * instance's value match one of the allowed values. This match is required if
56     * {@link #isAllowedOther()} is {@code false}, otherwise the constraint will
57     * generate a validation warning message if the target instance's value does not
58     * match any of the associated allowed value constraints targeting it.
59     *
60     * the associated test evaluates to {@link IBooleanItem#TRUE} against the
61     * target.
62     *
63     * @param id
64     *          the optional identifier for the constraint
65     * @param formalName
66     *          the constraint's formal name or {@code null} if not provided
67     * @param description
68     *          the constraint's semantic description or {@code null} if not
69     *          provided
70     * @param source
71     *          information about the constraint source
72     * @param level
73     *          the significance of a violation of this constraint
74     * @param target
75     *          the Metapath expression identifying the nodes the constraint targets
76     * @param properties
77     *          a collection of associated properties
78     * @param allowedValues
79     *          the list of allowed values for this constraint
80     * @param allowedOther
81     *          when {@code true} values other than the values specified by
82     *          {@code allowedValues} are allowed, or disallowed if {@code false}
83     * @param extensible
84     *          indicates the degree to which extended values should be allowed
85     * @param remarks
86     *          optional remarks describing the intent of the constraint
87     */
88    private DefaultAllowedValuesConstraint( // NOPMD necessary
89        @Nullable String id,
90        @Nullable String formalName,
91        @Nullable MarkupLine description,
92        @NonNull ISource source,
93        @NonNull Level level,
94        @NonNull MetapathExpression target,
95        @NonNull Map<QName, Set<String>> properties,
96        @NonNull Map<String, DefaultAllowedValue> allowedValues,
97        boolean allowedOther,
98        @NonNull Extensible extensible,
99        @Nullable MarkupMultiline remarks) {
100     super(id, formalName, description, source, level, target, properties, remarks);
101     this.allowedValues = allowedValues;
102     this.allowedOther = allowedOther;
103     this.extensible = extensible;
104   }
105 
106   @Override
107   public Map<String, DefaultAllowedValue> getAllowedValues() {
108     return allowedValues;
109   }
110 
111   @Override
112   public boolean isAllowedOther() {
113     return allowedOther;
114   }
115 
116   @Override
117   public Extensible getExtensible() {
118     return extensible;
119   }
120 
121   @Override
122   public <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
123     return visitor.visitAllowedValues(this, state);
124   }
125 
126   @NonNull
127   public static Builder builder() {
128     return new Builder();
129   }
130 
131   public static final class Builder
132       extends AbstractConstraintBuilder<Builder, DefaultAllowedValuesConstraint> {
133     @NonNull
134     private final Map<String, DefaultAllowedValue> allowedValues = new LinkedHashMap<>(); // NOPMD not thread safe
135     private boolean allowedOther = IAllowedValuesConstraint.DEFAULT_ALLOW_OTHER;
136     @NonNull
137     private Extensible extensible = IAllowedValuesConstraint.DEFAULT_EXTENSIBLE;
138 
139     private Builder() {
140       // disable construction
141     }
142 
143     public Builder allowedValue(@NonNull DefaultAllowedValue allowedValue) {
144       this.allowedValues.put(allowedValue.getValue(), allowedValue);
145       return this;
146     }
147 
148     public Builder allowedValues(@NonNull Map<String, DefaultAllowedValue> allowedValues) {
149       this.allowedValues.putAll(allowedValues);
150       return this;
151     }
152 
153     public Builder allowedOther(boolean bool) {
154       this.allowedOther = bool;
155       return this;
156     }
157 
158     public Builder extensible(@NonNull Extensible extensible) {
159       this.extensible = extensible;
160       return this;
161     }
162 
163     @Override
164     protected Builder getThis() {
165       return this;
166     }
167 
168     @NonNull
169     protected Map<String, DefaultAllowedValue> getAllowedValues() {
170       return allowedValues;
171     }
172 
173     protected boolean isAllowedOther() {
174       return allowedOther;
175     }
176 
177     @NonNull
178     protected Extensible getExtensible() {
179       return extensible;
180     }
181 
182     @Override
183     protected DefaultAllowedValuesConstraint newInstance() {
184       return new DefaultAllowedValuesConstraint(
185           getId(),
186           getFormalName(),
187           getDescription(),
188           ObjectUtils.notNull(getSource()),
189           getLevel(),
190           getTarget(),
191           getProperties(),
192           getAllowedValues(),
193           isAllowedOther(),
194           getExtensible(),
195           getRemarks());
196     }
197   }
198 }