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
20 public class SubCollectionConstraint<T> implements Constraint<Collection<T>>
21 {
22
23
24 @SuppressWarnings("checkstyle:visibilitymodifier")
25 protected final Collection<T> objects;
26
27
28
29
30 public SubCollectionConstraint(final Collection<T> objects)
31 {
32 Throw.whenNull(objects, "Collection of acceptable objects may not be null.");
33 this.objects = objects;
34 }
35
36
37 @Override
38 @SuppressWarnings("checkstyle:designforextension")
39 public boolean accept(final Collection<T> value)
40 {
41 return this.objects.containsAll(value);
42 }
43
44
45 @Override
46 @SuppressWarnings("checkstyle:designforextension")
47 public String failMessage()
48 {
49 return "Value of parameter '%s' contains value(s) not in the collection of acceptable values.";
50 }
51
52
53
54
55
56
57
58 @SafeVarargs
59 public static <T> SubCollectionConstraint<T> newInstance(final T... objs)
60 {
61 Collection<T> collection = new LinkedHashSet<>();
62 for (T t : objs)
63 {
64 collection.add(t);
65 }
66 return new SubCollectionConstraint<>(collection);
67 }
68
69
70 @Override
71 @SuppressWarnings("checkstyle:designforextension")
72 public String toString()
73 {
74 return "SubCollectionConstraint [objects=" + this.objects + "]";
75 }
76
77 }