org.hamcrest.core.AllOf Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-realer Show documentation
Show all versions of virtdata-lib-realer Show documentation
With inspiration from other libraries
package org.hamcrest.core;
import org.hamcrest.Description;
import org.hamcrest.DiagnosingMatcher;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Calculates the logical conjunction of multiple matchers. Evaluation is shortcut, so
* subsequent matchers are not called if an earlier matcher returns false
.
*/
public class AllOf extends DiagnosingMatcher {
private final Iterable> matchers;
public AllOf(Iterable> matchers) {
this.matchers = matchers;
}
@Override
public boolean matches(Object o, Description mismatch) {
for (Matcher super T> matcher : matchers) {
if (!matcher.matches(o)) {
mismatch.appendDescriptionOf(matcher).appendText(" ");
matcher.describeMismatch(o, mismatch);
return false;
}
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendList("(", " " + "and" + " ", ")", matchers);
}
/**
* Creates a matcher that matches if the examined object matches ALL of the specified matchers.
*
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*/
@Factory
public static Matcher allOf(Iterable> matchers) {
return new AllOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches ALL of the specified matchers.
*
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*/
@Factory
public static Matcher allOf(Matcher super T>... matchers) {
return allOf(Arrays.asList(matchers));
}
/**
* Creates a matcher that matches if the examined object matches ALL of the specified matchers.
*
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*/
@Factory
public static Matcher allOf(Matcher super T> first, Matcher super T> second) {
List> matchers = new ArrayList>(2);
matchers.add(first);
matchers.add(second);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches ALL of the specified matchers.
*
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*/
@Factory
public static Matcher allOf(Matcher super T> first, Matcher super T> second, Matcher super T> third) {
List> matchers = new ArrayList>(3);
matchers.add(first);
matchers.add(second);
matchers.add(third);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches ALL of the specified matchers.
*
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*/
@Factory
public static Matcher allOf(Matcher super T> first, Matcher super T> second, Matcher super T> third, Matcher super T> fourth) {
List> matchers = new ArrayList>(4);
matchers.add(first);
matchers.add(second);
matchers.add(third);
matchers.add(fourth);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches ALL of the specified matchers.
*
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*/
@Factory
public static Matcher allOf(Matcher super T> first, Matcher super T> second, Matcher super T> third, Matcher super T> fourth, Matcher super T> fifth) {
List> matchers = new ArrayList>(5);
matchers.add(first);
matchers.add(second);
matchers.add(third);
matchers.add(fourth);
matchers.add(fifth);
return allOf(matchers);
}
/**
* Creates a matcher that matches if the examined object matches ALL of the specified matchers.
*
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*/
@Factory
public static Matcher allOf(Matcher super T> first, Matcher super T> second, Matcher super T> third, Matcher super T> fourth, Matcher super T> fifth, Matcher super T> sixth) {
List> matchers = new ArrayList>(6);
matchers.add(first);
matchers.add(second);
matchers.add(third);
matchers.add(fourth);
matchers.add(fifth);
matchers.add(sixth);
return allOf(matchers);
}
}