001    package org.hamcrest.core;
002    
003    import java.util.ArrayList;
004    import java.util.Arrays;
005    import java.util.List;
006    
007    import org.hamcrest.Description;
008    import org.hamcrest.Factory;
009    import org.hamcrest.Matcher;
010    
011    /**
012     * Calculates the logical disjunction of multiple matchers. Evaluation is shortcut, so
013     * subsequent matchers are not called if an earlier matcher returns <code>true</code>.
014     */
015    public class AnyOf<T> extends ShortcutCombination<T> {
016    
017        public AnyOf(Iterable<Matcher<? super T>> matchers) {
018            super(matchers);
019        }
020    
021        @Override
022        public boolean matches(Object o) {
023            return matches(o, true);
024        }
025    
026        @Override
027        public void describeTo(Description description) {
028            describeTo(description, "or");
029        }
030    
031        /**
032         * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
033         * <p/>
034         * For example:
035         * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
036         */
037        @Factory
038        public static <T> AnyOf<T> anyOf(Iterable<Matcher<? super T>> matchers) {
039            return new AnyOf<T>(matchers);
040        }
041        
042        /**
043         * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
044         * <p/>
045         * For example:
046         * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
047         */
048        @Factory
049        public static <T> AnyOf<T> anyOf(Matcher<? super T>... matchers) {
050            return anyOf(Arrays.asList(matchers));
051        }
052    
053        /**
054         * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
055         * <p/>
056         * For example:
057         * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
058         */
059        @Factory
060        public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second) {
061            List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
062            matchers.add(first);
063            matchers.add(second);
064            return anyOf(matchers);
065        }
066    
067        /**
068         * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
069         * <p/>
070         * For example:
071         * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
072         */
073        @Factory
074        public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third) {
075            List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
076            matchers.add(first);
077            matchers.add(second);
078            matchers.add(third);
079            return anyOf(matchers);
080        }
081    
082        /**
083         * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
084         * <p/>
085         * For example:
086         * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
087         */
088        @Factory
089        public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth) {
090            List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
091            matchers.add(first);
092            matchers.add(second);
093            matchers.add(third);
094            matchers.add(fourth);
095            return anyOf(matchers);
096        }
097    
098        /**
099         * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
100         * <p/>
101         * For example:
102         * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
103         */
104        @Factory
105        public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth) {
106            List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
107            matchers.add(first);
108            matchers.add(second);
109            matchers.add(third);
110            matchers.add(fourth);
111            matchers.add(fifth);
112            return anyOf(matchers);
113        }
114    
115        /**
116         * Creates a matcher that matches if the examined object matches <b>ANY</b> of the specified matchers.
117         * <p/>
118         * For example:
119         * <pre>assertThat("myValue", anyOf(startsWith("foo"), containsString("Val")))</pre>
120         */
121        @Factory
122        public static <T> AnyOf<T> anyOf(Matcher<T> first, Matcher<? super T> second, Matcher<? super T> third, Matcher<? super T> fourth, Matcher<? super T> fifth, Matcher<? super T> sixth) {
123            List<Matcher<? super T>> matchers = new ArrayList<Matcher<? super T>>();
124            matchers.add(first);
125            matchers.add(second);
126            matchers.add(third);
127            matchers.add(fourth);
128            matchers.add(fifth);
129            matchers.add(sixth);
130            return anyOf(matchers);
131        }
132    }