org.hamcrest.core.CombinableMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtdata-lib-curves4 Show documentation
Show all versions of virtdata-lib-curves4 Show documentation
Statistical sampling library for use in virtualdataset libraries, based on apache commons math 4
package org.hamcrest.core;
import org.hamcrest.*;
import java.util.ArrayList;
public class CombinableMatcher extends TypeSafeDiagnosingMatcher {
private final Matcher super T> matcher;
public CombinableMatcher(Matcher super T> matcher) {
this.matcher = matcher;
}
@Override
protected boolean matchesSafely(T item, Description mismatch) {
if (!matcher.matches(item)) {
matcher.describeMismatch(item, mismatch);
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendDescriptionOf(matcher);
}
public CombinableMatcher and(Matcher super T> other) {
return new CombinableMatcher(new AllOf(templatedListWith(other)));
}
public CombinableMatcher or(Matcher super T> other) {
return new CombinableMatcher(new AnyOf(templatedListWith(other)));
}
private ArrayList> templatedListWith(Matcher super T> other) {
ArrayList> matchers = new ArrayList>();
matchers.add(matcher);
matchers.add(other);
return matchers;
}
/**
* Creates a matcher that matches when both of the specified matchers match the examined object.
*
* For example:
* assertThat("fab", both(containsString("a")).and(containsString("b")))
*/
@Factory
public static CombinableBothMatcher both(Matcher super LHS> matcher) {
return new CombinableBothMatcher(matcher);
}
public static final class CombinableBothMatcher {
private final Matcher super X> first;
public CombinableBothMatcher(Matcher super X> matcher) {
this.first = matcher;
}
public CombinableMatcher and(Matcher super X> other) {
return new CombinableMatcher(first).and(other);
}
}
/**
* Creates a matcher that matches when either of the specified matchers match the examined object.
*
* For example:
* assertThat("fan", either(containsString("a")).and(containsString("b")))
*/
@Factory
public static CombinableEitherMatcher either(Matcher super LHS> matcher) {
return new CombinableEitherMatcher(matcher);
}
public static final class CombinableEitherMatcher {
private final Matcher super X> first;
public CombinableEitherMatcher(Matcher super X> matcher) {
this.first = matcher;
}
public CombinableMatcher or(Matcher super X> other) {
return new CombinableMatcher(first).or(other);
}
}
}