View Javadoc
1   package org.opentrafficsim.base.parameters.constraint;
2   
3   import java.util.Collection;
4   
5   import org.djutils.exceptions.Throw;
6   
7   /**
8    * Constraint that checks whether a collection (the parameter value) is a subset of a constraint collection.
9    * <p>
10   * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12   * </p>
13   * @author Alexander Verbraeck
14   * @author Peter Knoppers
15   * @author Wouter Schakel
16   * @param <T> object type
17   */
18  public class SubCollectionConstraint<T> implements Constraint<Collection<T>>
19  {
20  
21      /** Acceptable objects. */
22      @SuppressWarnings("checkstyle:visibilitymodifier")
23      protected final Collection<T> objects;
24  
25      /**
26       * Constructor.
27       * @param objects acceptable objects
28       */
29      public SubCollectionConstraint(final Collection<T> objects)
30      {
31          Throw.whenNull(objects, "Collection of acceptable objects may not be null.");
32          this.objects = objects;
33      }
34  
35      @Override
36      public boolean accept(final Collection<T> value)
37      {
38          return this.objects.containsAll(value);
39      }
40  
41      @Override
42      public String failMessage()
43      {
44          return "Value of parameter '%s' contains value(s) not in the collection of acceptable values.";
45      }
46  
47      @Override
48      public String toString()
49      {
50          return "SubCollectionConstraint [objects=" + this.objects + "]";
51      }
52  
53  }