1 package org.hamcrest.collection; 2 3 import org.hamcrest.Description; 4 import org.hamcrest.Matcher; 5 import org.hamcrest.TypeSafeMatcher; 6 import org.hamcrest.internal.NullSafety; 7 8 import java.util.ArrayList; 9 import java.util.Collection; 10 import java.util.List; 11 12 import static java.util.Arrays.asList; 13 import static org.hamcrest.core.IsEqual.equalTo; 14 15 public class IsArrayContainingInOrder<E> extends TypeSafeMatcher<E[]> { 16 private final Collection<Matcher<? super E>> matchers; 17 private final IsIterableContainingInOrder<E> iterableMatcher; 18 19 public IsArrayContainingInOrder(List<Matcher<? super E>> matchers) { 20 this.iterableMatcher = new IsIterableContainingInOrder<E>(matchers); 21 this.matchers = matchers; 22 } 23 24 @Override 25 public boolean matchesSafely(E[] item) { 26 return iterableMatcher.matches(asList(item)); 27 } 28 29 @Override 30 public void describeMismatchSafely(E[] item, Description mismatchDescription) { 31 iterableMatcher.describeMismatch(asList(item), mismatchDescription); 32 } 33 34 @Override 35 public void describeTo(Description description) { 36 description.appendList("[", ", ", "]", matchers); 37 } 38 39 /** 40 * Creates a matcher for arrays that matches when each item in the examined array is 41 * logically equal to the corresponding item in the specified items. For a positive match, 42 * the examined array must be of the same length as the number of specified items. 43 * For example: 44 * <pre>assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))</pre> 45 * 46 * @param items 47 * the items that must equal the items within an examined array 48 */ 49 public static <E> Matcher<E[]> arrayContaining(E... items) { 50 List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>(); 51 for (E item : items) { 52 matchers.add(equalTo(item)); 53 } 54 return arrayContaining(matchers); 55 } 56 57 /** 58 * Creates a matcher for arrays that matches when each item in the examined array satisfies the 59 * corresponding matcher in the specified matchers. For a positive match, the examined array 60 * must be of the same length as the number of specified matchers. 61 * For example: 62 * <pre>assertThat(new String[]{"foo", "bar"}, contains(equalTo("foo"), equalTo("bar")))</pre> 63 * 64 * @param itemMatchers 65 * the matchers that must be satisfied by the items in the examined array 66 */ 67 public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) { 68 //required for JDK 1.6 69 //noinspection RedundantTypeArguments 70 final List<Matcher<? super E>> nullSafeWithExplicitTypeMatchers = NullSafety.<E>nullSafe(itemMatchers); 71 72 return arrayContaining(nullSafeWithExplicitTypeMatchers); 73 } 74 75 /** 76 * Creates a matcher for arrays that matches when each item in the examined array satisfies the 77 * corresponding matcher in the specified list of matchers. For a positive match, the examined array 78 * must be of the same length as the specified list of matchers. 79 * For example: 80 * <pre>assertThat(new String[]{"foo", "bar"}, contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre> 81 * 82 * @param itemMatchers 83 * a list of matchers, each of which must be satisfied by the corresponding item in an examined array 84 */ 85 public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) { 86 return new IsArrayContainingInOrder<E>(itemMatchers); 87 } 88 89 } 90