Home | History | Annotate | Download | only in core
      1 package org.hamcrest.core;
      2 
      3 import org.hamcrest.Description;
      4 import org.hamcrest.Matcher;
      5 
      6 import java.util.Arrays;
      7 import java.util.List;
      8 
      9 /**
     10  * Calculates the logical disjunction of multiple matchers. Evaluation is shortcut, so
     11  * subsequent matchers are not called if an earlier matcher returns <code>true</code>.
     12  */
     13 public class AnyOf<T> extends ShortcutCombination<T> {
     14 
     15     public AnyOf(Iterable<Matcher<? super T>> matchers) {
     16         super(matchers);
     17     }
     18 
     19     @Override
     20     public boolean matches(Object o) {
     21         return matches(o, true);
     22     }
     23 
     24     @Override
     25     public void describeTo(Description description) {
     26         describeTo(description, "or");
     27     }
     28 
     29     /**
     30      * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
     31      * For example:
     32      * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
     33      */
     34     public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
     35         return new AnyOf<>(matchers);
     36     }
     37 
     38     /**
     39      * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
     40      * For example:
     41      * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
     42      */
     43     @SafeVarargs
     44     public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers) {
     45         return anyOf((List) Arrays.asList(matchers));
     46     }
     47 }
     48