View Javadoc
1   package org.opentrafficsim.base.parameters.constraint;
2   
3   import java.util.Collection;
4   import java.util.HashSet;
5   
6   /**
7    * Constraint that checks whether the value is any of a given collection of classes, where each class is a sub class of a given
8    * type.
9    * <p>
10   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
12   * <p>
13   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 30 jun. 2017 <br>
14   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
17   * @param <T> super type for all possible classes, e.g. TacticalPlanner
18   */
19  public class ClassConstraint<T> extends CollectionConstraint<Class<? extends T>>
20  {
21  
22      /**
23       * @param classes Collection&lt;Class&lt;? extends T&gt;&gt;; acceptable classes
24       */
25      private ClassConstraint(final Collection<Class<? extends T>> classes)
26      {
27          super(classes);
28      }
29  
30      /**
31       * Creates a new instance with given collection.
32       * @param objs Class&lt;? extends T&gt;...; acceptable classes
33       * @param <T> type class
34       * @return new instance with given collection
35       */
36      @SafeVarargs
37      public static <T> ClassConstraint<T> newInstance(final Class<? extends T>... objs)
38      {
39          Collection<Class<? extends T>> collection = new HashSet<>();
40          for (Class<? extends T> clazz : objs)
41          {
42              collection.add(clazz);
43          }
44          return new ClassConstraint<>(collection);
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      @SuppressWarnings("checkstyle:designforextension")
50      public String toString()
51      {
52          return "ClassConstraint [classes=" + super.objects + "]";
53      }
54  
55  }