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 conjunction of two matchers. Evaluation is
12 * shortcut, so that the second matcher is not called if the first
13 * matcher returns <code>false</code>.
14 */
15public class AllOf<T> extends BaseMatcher<T> {
16    private final Iterable<Matcher<? extends T>> matchers;
17
18    public AllOf(Iterable<Matcher<? extends T>> matchers) {
19        this.matchers = matchers;
20    }
21
22    public boolean matches(Object o) {
23        for (Matcher<? extends T> matcher : matchers) {
24            if (!matcher.matches(o)) {
25                return false;
26            }
27        }
28        return true;
29    }
30
31    public void describeTo(Description description) {
32    	description.appendList("(", " and ", ")", matchers);
33    }
34
35    /**
36     * Evaluates to true only if ALL of the passed in matchers evaluate to true.
37     */
38    @Factory
39    public static <T> Matcher<T> allOf(Matcher<? extends T>... matchers) {
40        return allOf(Arrays.asList(matchers));
41    }
42
43    /**
44     * Evaluates to true only if ALL of the passed in matchers evaluate to true.
45     */
46    @Factory
47    public static <T> Matcher<T> allOf(Iterable<Matcher<? extends T>> matchers) {
48        return new AllOf<T>(matchers);
49    }
50
51}
52