package org.hamcrest.collection; import java.util.Arrays; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; public class IsArray extends TypeSafeMatcher { private final Matcher[] elementMatchers; public IsArray(Matcher[] elementMatchers) { this.elementMatchers = elementMatchers.clone(); } public boolean matchesSafely(T[] array) { if (array.length != elementMatchers.length) return false; for (int i = 0; i < array.length; i++) { if (!elementMatchers[i].matches(array[i])) return false; } return true; } public void describeTo(Description description) { description.appendList(descriptionStart(), descriptionSeparator(), descriptionEnd(), Arrays.asList(elementMatchers)); } /** * Returns the string that starts the description. * * Can be overridden in subclasses to customise how the matcher is * described. */ protected String descriptionStart() { return "["; } /** * Returns the string that separates the elements in the description. * * Can be overridden in subclasses to customise how the matcher is * described. */ protected String descriptionSeparator() { return ", "; } /** * Returns the string that ends the description. * * Can be overridden in subclasses to customise how the matcher is * described. */ protected String descriptionEnd() { return "]"; } public static IsArray array(Matcher... elementMatchers) { return new IsArray(elementMatchers); } }