1package org.hamcrest.core; 2 3import org.hamcrest.BaseMatcher; 4import org.hamcrest.Matcher; 5import org.hamcrest.Description; 6import org.hamcrest.Factory; 7 8import java.util.Arrays; 9 10/** 11 * Calculates the logical disjunction of two matchers. Evaluation is 12 * shortcut, so that the second matcher is not called if the first 13 * matcher returns <code>true</code>. 14 */ 15public class AnyOf<T> extends BaseMatcher<T> { 16 17 private final Iterable<Matcher<? extends T>> matchers; 18 19 public AnyOf(Iterable<Matcher<? extends T>> matchers) { 20 this.matchers = matchers; 21 } 22 23 public boolean matches(Object o) { 24 for (Matcher<? extends T> matcher : matchers) { 25 if (matcher.matches(o)) { 26 return true; 27 } 28 } 29 return false; 30 } 31 32 public void describeTo(Description description) { 33 description.appendList("(", " or ", ")", matchers); 34 } 35 36 /** 37 * Evaluates to true if ANY of the passed in matchers evaluate to true. 38 */ 39 @Factory 40 public static <T> Matcher<T> anyOf(Matcher<? extends T>... matchers) { 41 return anyOf(Arrays.asList(matchers)); 42 } 43 44 /** 45 * Evaluates to true if ANY of the passed in matchers evaluate to true. 46 */ 47 @Factory 48 public static <T> Matcher<T> anyOf(Iterable<Matcher<? extends T>> matchers) { 49 return new AnyOf<T>(matchers); 50 } 51} 52