All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hamcrest.core.Every Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
package org.hamcrest.core;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class Every extends TypeSafeDiagnosingMatcher> {
    private final Matcher matcher;

    public Every(Matcher matcher) {
        this.matcher= matcher;
    }

    @Override
    public boolean matchesSafely(Iterable collection, Description mismatchDescription) {
        for (T t : collection) {
            if (!matcher.matches(t)) {
                mismatchDescription.appendText("an item ");
                matcher.describeMismatch(t, mismatchDescription);
                return false;
            }
        }
        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("every item is ").appendDescriptionOf(matcher);
    }

    /**
     * Creates a matcher for {@link Iterable}s that only matches when a single pass over the
     * examined {@link Iterable} yields items that are all matched by the specified
     * itemMatcher.
     * 

* For example: *

assertThat(Arrays.asList("bar", "baz"), everyItem(startsWith("ba")))
* * @param itemMatcher * the matcher to apply to every item provided by the examined {@link Iterable} */ @Factory public static Matcher> everyItem(final Matcher itemMatcher) { return new Every(itemMatcher); } }