1 package org.hamcrest.collection; 2 3 import java.util.Arrays; 4 import java.util.Collection; 5 6 import org.hamcrest.BaseMatcher; 7 import org.hamcrest.Description; 8 import org.hamcrest.Factory; 9 import org.hamcrest.Matcher; 10 11 public class IsIn<T> extends BaseMatcher<T> { 12 private final Collection<T> collection; 13 14 public IsIn(Collection<T> collection) { 15 this.collection = collection; 16 } 17 18 public IsIn(T[] elements) { 19 collection = Arrays.asList(elements); 20 } 21 22 public boolean matches(Object o) { 23 return collection.contains(o); 24 } 25 26 public void describeTo(Description buffer) { 27 buffer.appendText("one of "); 28 buffer.appendValueList("{", ", ", "}", collection); 29 } 30 31 @Factory 32 public static <T> Matcher<T> isIn(Collection<T> collection) { 33 return new IsIn<T>(collection); 34 } 35 36 @Factory 37 public static <T> Matcher<T> isIn(T[] elements) { 38 return new IsIn<T>(elements); 39 } 40 41 @Factory 42 public static <T> Matcher<T> isOneOf(T... elements) { 43 return isIn(elements); 44 } 45 } 46