ClassConstraint.java

  1. package org.opentrafficsim.base.parameters.constraint;

  2. import java.util.Collection;
  3. import java.util.HashSet;

  4. /**
  5.  * Constraint that checks whether the value is any of a given collection of classes, where each class is a sub class of a given
  6.  * type.
  7.  * <p>
  8.  * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  9.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  10.  * <p>
  11.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 30 jun. 2017 <br>
  12.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  13.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  14.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  15.  * @param <T> super type for all possible classes, e.g. TacticalPlanner
  16.  */
  17. public class ClassConstraint<T> extends CollectionConstraint<Class<? extends T>>
  18. {

  19.     /**
  20.      * @param classes Collection&lt;Class&lt;? extends T&gt;&gt;; acceptable classes
  21.      */
  22.     private ClassConstraint(final Collection<Class<? extends T>> classes)
  23.     {
  24.         super(classes);
  25.     }

  26.     /**
  27.      * Creates a new instance with given collection.
  28.      * @param objs Class&lt;? extends T&gt;...; acceptable classes
  29.      * @param <T> type class
  30.      * @return new instance with given collection
  31.      */
  32.     @SafeVarargs
  33.     public static <T> ClassConstraint<T> newInstance(final Class<? extends T>... objs)
  34.     {
  35.         Collection<Class<? extends T>> collection = new HashSet<>();
  36.         for (Class<? extends T> clazz : objs)
  37.         {
  38.             collection.add(clazz);
  39.         }
  40.         return new ClassConstraint<>(collection);
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     @SuppressWarnings("checkstyle:designforextension")
  45.     public String toString()
  46.     {
  47.         return "ClassConstraint [classes=" + super.objects + "]";
  48.     }

  49. }