ClassConstraint.java

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

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

  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-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  9.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  10.  * </p>
  11.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  12.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  13.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  14.  * @param <T> super type for all possible classes, e.g. TacticalPlanner
  15.  */
  16. public final class ClassConstraint<T> extends CollectionConstraint<Class<? extends T>>
  17. {

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

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

  41.     @Override
  42.     public String toString()
  43.     {
  44.         return "ClassConstraint [classes=" + super.objects + "]";
  45.     }

  46. }