DefaultAllowedValuesConstraint.java

  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. package gov.nist.secauto.metaschema.core.model.constraint;

  27. import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
  28. import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
  29. import gov.nist.secauto.metaschema.core.metapath.MetapathExpression;
  30. import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
  31. import gov.nist.secauto.metaschema.core.util.ObjectUtils;

  32. import java.util.LinkedHashMap;
  33. import java.util.Map;
  34. import java.util.Set;

  35. import javax.xml.namespace.QName;

  36. import edu.umd.cs.findbugs.annotations.NonNull;
  37. import edu.umd.cs.findbugs.annotations.Nullable;

  38. public final class DefaultAllowedValuesConstraint
  39.     extends AbstractConstraint
  40.     implements IAllowedValuesConstraint {
  41.   private final boolean allowedOther;
  42.   @NonNull
  43.   private final Extensible extensible;
  44.   @NonNull
  45.   private final Map<String, DefaultAllowedValue> allowedValues;

  46.   /**
  47.    * Construct a new allowed values constraint which ensures that a target
  48.    * instance's value match one of the allowed values. This match is required if
  49.    * {@link #isAllowedOther()} is {@code false}, otherwise the constraint will
  50.    * generate a validation warning message if the target instance's value does not
  51.    * match any of the associated allowed value constraints targeting it.
  52.    *
  53.    * the associated test evaluates to {@link IBooleanItem#TRUE} against the
  54.    * target.
  55.    *
  56.    * @param id
  57.    *          the optional identifier for the constraint
  58.    * @param formalName
  59.    *          the constraint's formal name or {@code null} if not provided
  60.    * @param description
  61.    *          the constraint's semantic description or {@code null} if not
  62.    *          provided
  63.    * @param source
  64.    *          information about the constraint source
  65.    * @param level
  66.    *          the significance of a violation of this constraint
  67.    * @param target
  68.    *          the Metapath expression identifying the nodes the constraint targets
  69.    * @param properties
  70.    *          a collection of associated properties
  71.    * @param allowedValues
  72.    *          the list of allowed values for this constraint
  73.    * @param allowedOther
  74.    *          when {@code true} values other than the values specified by
  75.    *          {@code allowedValues} are allowed, or disallowed if {@code false}
  76.    * @param extensible
  77.    *          indicates the degree to which extended values should be allowed
  78.    * @param remarks
  79.    *          optional remarks describing the intent of the constraint
  80.    */
  81.   private DefaultAllowedValuesConstraint( // NOPMD necessary
  82.       @Nullable String id,
  83.       @Nullable String formalName,
  84.       @Nullable MarkupLine description,
  85.       @NonNull ISource source,
  86.       @NonNull Level level,
  87.       @NonNull MetapathExpression target,
  88.       @NonNull Map<QName, Set<String>> properties,
  89.       @NonNull Map<String, DefaultAllowedValue> allowedValues,
  90.       boolean allowedOther,
  91.       @NonNull Extensible extensible,
  92.       @Nullable MarkupMultiline remarks) {
  93.     super(id, formalName, description, source, level, target, properties, remarks);
  94.     this.allowedValues = allowedValues;
  95.     this.allowedOther = allowedOther;
  96.     this.extensible = extensible;
  97.   }

  98.   @Override
  99.   public Map<String, DefaultAllowedValue> getAllowedValues() {
  100.     return allowedValues;
  101.   }

  102.   @Override
  103.   public boolean isAllowedOther() {
  104.     return allowedOther;
  105.   }

  106.   @Override
  107.   public Extensible getExtensible() {
  108.     return extensible;
  109.   }

  110.   @Override
  111.   public <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
  112.     return visitor.visitAllowedValues(this, state);
  113.   }

  114.   @NonNull
  115.   public static Builder builder() {
  116.     return new Builder();
  117.   }

  118.   public static final class Builder
  119.       extends AbstractConstraintBuilder<Builder, DefaultAllowedValuesConstraint> {
  120.     @NonNull
  121.     private final Map<String, DefaultAllowedValue> allowedValues = new LinkedHashMap<>(); // NOPMD not thread safe
  122.     private boolean allowedOther = IAllowedValuesConstraint.DEFAULT_ALLOW_OTHER;
  123.     @NonNull
  124.     private Extensible extensible = IAllowedValuesConstraint.DEFAULT_EXTENSIBLE;

  125.     private Builder() {
  126.       // disable construction
  127.     }

  128.     public Builder allowedValue(@NonNull DefaultAllowedValue allowedValue) {
  129.       this.allowedValues.put(allowedValue.getValue(), allowedValue);
  130.       return this;
  131.     }

  132.     public Builder allowedValues(@NonNull Map<String, DefaultAllowedValue> allowedValues) {
  133.       this.allowedValues.putAll(allowedValues);
  134.       return this;
  135.     }

  136.     public Builder allowedOther(boolean bool) {
  137.       this.allowedOther = bool;
  138.       return this;
  139.     }

  140.     public Builder extensible(@NonNull Extensible extensible) {
  141.       this.extensible = extensible;
  142.       return this;
  143.     }

  144.     @Override
  145.     protected Builder getThis() {
  146.       return this;
  147.     }

  148.     @NonNull
  149.     protected Map<String, DefaultAllowedValue> getAllowedValues() {
  150.       return allowedValues;
  151.     }

  152.     protected boolean isAllowedOther() {
  153.       return allowedOther;
  154.     }

  155.     @NonNull
  156.     protected Extensible getExtensible() {
  157.       return extensible;
  158.     }

  159.     @Override
  160.     protected DefaultAllowedValuesConstraint newInstance() {
  161.       return new DefaultAllowedValuesConstraint(
  162.           getId(),
  163.           getFormalName(),
  164.           getDescription(),
  165.           ObjectUtils.notNull(getSource()),
  166.           getLevel(),
  167.           getTarget(),
  168.           getProperties(),
  169.           getAllowedValues(),
  170.           isAllowedOther(),
  171.           getExtensible(),
  172.           getRemarks());
  173.     }
  174.   }
  175. }