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