DefaultCardinalityConstraint.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.util.ObjectUtils;

  31. import java.util.Map;
  32. import java.util.Set;

  33. import javax.xml.namespace.QName;

  34. import edu.umd.cs.findbugs.annotations.NonNull;
  35. import edu.umd.cs.findbugs.annotations.Nullable;

  36. public final class DefaultCardinalityConstraint
  37.     extends AbstractConstraint
  38.     implements ICardinalityConstraint {
  39.   @Nullable
  40.   private final Integer minOccurs;
  41.   @Nullable
  42.   private final Integer maxOccurs;

  43.   /**
  44.    * Construct a new cardinality constraint which enforces that the number of
  45.    * items matching the target fall within the inclusive range described by the
  46.    * {@code minOccurs} or {@code maxOccurs} values.
  47.    *
  48.    * @param id
  49.    *          the optional identifier for the constraint
  50.    * @param formalName
  51.    *          the constraint's formal name or {@code null} if not provided
  52.    * @param description
  53.    *          the constraint's semantic description or {@code null} if not
  54.    *          provided
  55.    * @param source
  56.    *          information about the constraint source
  57.    * @param level
  58.    *          the significance of a violation of this constraint
  59.    * @param target
  60.    *          the Metapath expression identifying the nodes the constraint targets
  61.    * @param properties
  62.    *          a collection of associated properties
  63.    * @param minOccurs
  64.    *          if provided, the constraint ensures that the count of targets is at
  65.    *          least this value
  66.    * @param maxOccurs
  67.    *          if provided, the constraint ensures that the count of targets is at
  68.    *          most this value
  69.    * @param remarks
  70.    *          optional remarks describing the intent of the constraint
  71.    */
  72.   private DefaultCardinalityConstraint(
  73.       @Nullable String id,
  74.       @Nullable String formalName,
  75.       @Nullable MarkupLine description,
  76.       @NonNull ISource source,
  77.       @NonNull Level level,
  78.       @NonNull MetapathExpression target,
  79.       @NonNull Map<QName, Set<String>> properties,
  80.       @Nullable Integer minOccurs,
  81.       @Nullable Integer maxOccurs,
  82.       MarkupMultiline remarks) {
  83.     super(id, formalName, description, source, level, target, properties, remarks);
  84.     if (minOccurs == null && maxOccurs == null) {
  85.       throw new IllegalArgumentException("at least one of minOccurs or maxOccurs must be provided");
  86.     }
  87.     this.minOccurs = minOccurs;
  88.     this.maxOccurs = maxOccurs;
  89.   }

  90.   @Override
  91.   public Integer getMinOccurs() {
  92.     return minOccurs;
  93.   }

  94.   @Override
  95.   public Integer getMaxOccurs() {
  96.     return maxOccurs;
  97.   }

  98.   @Override
  99.   public <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
  100.     return visitor.visitCardinalityConstraint(this, state);
  101.   }

  102.   @NonNull
  103.   public static Builder builder() {
  104.     return new Builder();
  105.   }

  106.   public static final class Builder
  107.       extends AbstractConstraintBuilder<Builder, DefaultCardinalityConstraint> {
  108.     private Integer minOccurs;
  109.     private Integer maxOccurs;

  110.     private Builder() {
  111.       // disable construction
  112.     }

  113.     public Builder minOccurs(int value) {
  114.       this.minOccurs = value;
  115.       return this;
  116.     }

  117.     public Builder maxOccurs(int value) {
  118.       this.maxOccurs = value;
  119.       return this;
  120.     }

  121.     @Override
  122.     protected Builder getThis() {
  123.       return this;
  124.     }

  125.     @Override
  126.     protected void validate() {
  127.       super.validate();

  128.       if (getMinOccurs() == null && getMaxOccurs() == null) {
  129.         throw new IllegalStateException("At least one of minOccurs or maxOccurs must be provided.");
  130.       }
  131.     }

  132.     protected Integer getMinOccurs() {
  133.       return minOccurs;
  134.     }

  135.     protected Integer getMaxOccurs() {
  136.       return maxOccurs;
  137.     }

  138.     @Override
  139.     protected DefaultCardinalityConstraint newInstance() {
  140.       return new DefaultCardinalityConstraint(
  141.           getId(),
  142.           getFormalName(),
  143.           getDescription(),
  144.           ObjectUtils.notNull(getSource()),
  145.           getLevel(),
  146.           getTarget(),
  147.           getProperties(),
  148.           getMinOccurs(),
  149.           getMaxOccurs(),
  150.           getRemarks());
  151.     }
  152.   }
  153. }