1package org.hamcrest.collection;
2
3import org.hamcrest.BaseMatcher;
4import org.hamcrest.Description;
5import org.hamcrest.Matcher;
6
7import java.util.Arrays;
8import java.util.Collection;
9
10public class IsIn<T> extends BaseMatcher<T> {
11    private final Collection<T> collection;
12
13    public IsIn(Collection<T> collection) {
14        this.collection = collection;
15    }
16
17    public IsIn(T[] elements) {
18        collection = Arrays.asList(elements);
19    }
20
21    @SuppressWarnings("SuspiciousMethodCalls")
22    @Override
23    public boolean matches(Object o) {
24        return collection.contains(o);
25    }
26
27    @Override
28    public void describeTo(Description buffer) {
29        buffer.appendText("one of ");
30        buffer.appendValueList("{", ", ", "}", collection);
31    }
32
33    /**
34     * Creates a matcher that matches when the examined object is found within the
35     * specified collection.
36     * For example:
37     * <pre>assertThat("foo", isIn(Arrays.asList("bar", "foo")))</pre>
38     *
39     * @deprecated use is(in(...)) instead
40     *
41     * @param collection
42     *     the collection in which matching items must be found
43     *
44     */
45    @Deprecated
46    public static <T> Matcher<T> isIn(Collection<T> collection) {
47        return in(collection);
48    }
49
50    /**
51     * Creates a matcher that matches when the examined object is found within the
52     * specified collection.
53     * For example:
54     * <pre>assertThat("foo", is(in(Arrays.asList("bar", "foo"))))</pre>
55     *
56     * @param collection
57     *     the collection in which matching items must be found
58     *
59     */
60    public static <T> Matcher<T> in(Collection<T> collection) {
61        return new IsIn<T>(collection);
62    }
63
64    /**
65     * Creates a matcher that matches when the examined object is found within the
66     * specified array.
67     * For example:
68     * <pre>assertThat("foo", isIn(new String[]{"bar", "foo"}))</pre>
69     *
70     * @deprecated use is(in(...)) instead
71     *
72     * @param elements
73     *     the array in which matching items must be found
74     *
75     */
76    @Deprecated
77    public static <T> Matcher<T> isIn(T[] elements) {
78        return in(elements);
79    }
80
81    /**
82     * Creates a matcher that matches when the examined object is found within the
83     * specified array.
84     * For example:
85     * <pre>assertThat("foo", is(in(new String[]{"bar", "foo"})))</pre>
86     *
87     * @param elements
88     *     the array in which matching items must be found
89     *
90     */
91    public static <T> Matcher<T> in(T[] elements) {
92        return new IsIn<T>(elements);
93    }
94
95    /**
96     * Creates a matcher that matches when the examined object is equal to one of the
97     * specified elements.
98     * For example:
99     * <pre>assertThat("foo", isOneOf("bar", "foo"))</pre>
100     *
101     * @deprecated use is(oneOf(...)) instead
102     *
103     * @param elements
104     *     the elements amongst which matching items will be found
105     *
106     */
107    @Deprecated
108    public static <T> Matcher<T> isOneOf(T... elements) {
109        return oneOf(elements);
110    }
111
112    /**
113     * Creates a matcher that matches when the examined object is equal to one of the
114     * specified elements.
115     * For example:
116     * <pre>assertThat("foo", is(oneOf("bar", "foo")))</pre>
117     *
118     * @param elements
119     *     the elements amongst which matching items will be found
120     *
121     */
122    public static <T> Matcher<T> oneOf(T... elements) {
123        return in(elements);
124    }
125}
126