1 package org.opentrafficsim.base.parameters.constraint;
2
3 import java.util.Collection;
4 import java.util.LinkedHashSet;
5
6 import org.djutils.exceptions.Throw;
7
8
9
10
11
12
13
14
15
16
17
18
19 public class CollectionConstraint<T> implements Constraint<T>
20 {
21
22
23 @SuppressWarnings("checkstyle:visibilitymodifier")
24 protected final Collection<T> objects;
25
26
27
28
29
30 public CollectionConstraint(final Collection<T> objects)
31 {
32 Throw.whenNull(objects, "Collection of acceptable objects may not be null.");
33 this.objects = objects;
34 }
35
36 @Override
37 public boolean accept(final T value)
38 {
39 return this.objects.contains(value);
40 }
41
42 @Override
43 public String failMessage()
44 {
45 return "Value of parameter '%s' is not in the collection of acceptable values.";
46 }
47
48
49
50
51
52
53
54 @SafeVarargs
55 public static <T> CollectionConstraint<T> newInstance(final T... objs)
56 {
57 Collection<T> collection = new LinkedHashSet<>();
58 for (T t : objs)
59 {
60 collection.add(t);
61 }
62 return new CollectionConstraint<>(collection);
63 }
64
65 @Override
66 public String toString()
67 {
68 return "CollectionConstraint [objects=" + this.objects + "]";
69 }
70
71 }