package org.opentrafficsim.base.parameters.constraint;
import java.util.Collection;
import java.util.LinkedHashSet;
import org.djutils.exceptions.Throw;
/**
* Constraint that checks whether a collection (the parameter value) is a subset of a constraint collection.
*
* Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version 10 sep. 2017
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
* @param object type
*/
public class SubCollectionConstraint implements Constraint>
{
/** Acceptable objects. */
@SuppressWarnings("checkstyle:visibilitymodifier")
protected final Collection objects;
/**
* @param objects Collection<T>; acceptable objects
*/
public SubCollectionConstraint(final Collection objects)
{
Throw.whenNull(objects, "Collection of acceptable objects may not be null.");
this.objects = objects;
}
/** {@inheritDoc} */
@Override
@SuppressWarnings("checkstyle:designforextension")
public boolean accept(final Collection value)
{
return this.objects.containsAll(value);
}
/** {@inheritDoc} */
@Override
@SuppressWarnings("checkstyle:designforextension")
public String failMessage()
{
return "Value of parameter '%s' contains value(s) not in the collection of acceptable values.";
}
/**
* Creates a new instance with given collection.
* @param objs T...; acceptable objects
* @param type
* @return new instance with given collection
*/
@SafeVarargs
public static SubCollectionConstraint newInstance(final T... objs)
{
Collection collection = new LinkedHashSet<>();
for (T t : objs)
{
collection.add(t);
}
return new SubCollectionConstraint<>(collection);
}
/** {@inheritDoc} */
@Override
@SuppressWarnings("checkstyle:designforextension")
public String toString()
{
return "SubCollectionConstraint [objects=" + this.objects + "]";
}
}