1 package org.hamcrest.collection; 2 3 import org.hamcrest.Description; 4 import org.hamcrest.Matcher; 5 import org.hamcrest.TypeSafeMatcher; 6 7 import java.util.Arrays; 8 9 /** 10 * Matcher for array whose elements satisfy a sequence of matchers. 11 * The array size must equal the number of element matchers. 12 */ 13 public class IsArray<T> extends TypeSafeMatcher<T[]> { 14 private final Matcher<? super T>[] elementMatchers; 15 IsArray(Matcher<? super T>[] elementMatchers)16 public IsArray(Matcher<? super T>[] elementMatchers) { 17 this.elementMatchers = elementMatchers.clone(); 18 } 19 20 @Override matchesSafely(T[] array)21 public boolean matchesSafely(T[] array) { 22 if (array.length != elementMatchers.length) return false; 23 24 for (int i = 0; i < array.length; i++) { 25 if (!elementMatchers[i].matches(array[i])) return false; 26 } 27 28 return true; 29 } 30 31 @Override describeMismatchSafely(T[] actual, Description mismatchDescription)32 public void describeMismatchSafely(T[] actual, Description mismatchDescription) { 33 if (actual.length != elementMatchers.length) { 34 mismatchDescription.appendText("array length was ").appendValue(actual.length); 35 return; 36 } 37 for (int i = 0; i < actual.length; i++) { 38 if (!elementMatchers[i].matches(actual[i])) { 39 mismatchDescription.appendText("element ").appendValue(i).appendText(" "); 40 elementMatchers[i].describeMismatch(actual[i], mismatchDescription); 41 return; 42 } 43 } 44 } 45 46 @Override 47 @SuppressWarnings("unchecked") describeTo(Description description)48 public void describeTo(Description description) { 49 description.appendList(descriptionStart(), descriptionSeparator(), descriptionEnd(), 50 Arrays.asList(elementMatchers)); 51 } 52 53 /** 54 * Returns the string that starts the description. 55 * 56 * Can be overridden in subclasses to customise how the matcher is 57 * described. 58 */ descriptionStart()59 protected String descriptionStart() { 60 return "["; 61 } 62 63 /** 64 * Returns the string that separates the elements in the description. 65 * 66 * Can be overridden in subclasses to customise how the matcher is 67 * described. 68 */ descriptionSeparator()69 protected String descriptionSeparator() { 70 return ", "; 71 } 72 73 /** 74 * Returns the string that ends the description. 75 * 76 * Can be overridden in subclasses to customise how the matcher is 77 * described. 78 */ descriptionEnd()79 protected String descriptionEnd() { 80 return "]"; 81 } 82 83 /** 84 * Creates a matcher that matches arrays whose elements are satisfied by the specified matchers. Matches 85 * positively only if the number of matchers specified is equal to the length of the examined array and 86 * each matcher[i] is satisfied by array[i]. 87 * For example: 88 * <pre>assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))</pre> 89 * 90 * @param elementMatchers 91 * the matchers that the elements of examined arrays should satisfy 92 */ array(Matcher<? super T>.... elementMatchers)93 public static <T> IsArray<T> array(Matcher<? super T>... elementMatchers) { 94 return new IsArray<T>(elementMatchers); 95 } 96 97 } 98