![JAR search and dependency download from the Maven repository](/logo.png)
it.ozimov.cirneco.hamcrest.base.IsBetweenLowerBoundInclusive Maven / Gradle / Ivy
package it.ozimov.cirneco.hamcrest.base;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Is the value a number between two numbers, lower bound included?
*
* @since version 0.1 for JDK7
*/
public class IsBetweenLowerBoundInclusive> extends TypeSafeMatcher {
private final T from;
private final T to;
/**
* Creates and instance of the matcher. Observe that from
and to
cannot be null and
* from.compareTo(to)
must be negative.
*/
public IsBetweenLowerBoundInclusive(final T from, final T to) {
checkNotNull(from);
checkNotNull(to);
checkArgument(from.compareTo(to) < 0, "from must be before to");
this.from = from;
this.to = to;
}
/**
* Creates a matcher for {@code T}s that matches when the compareTo()
method returns
* a value between from
and to
, both included.
*
* For example:
*
assertThat(10, betweenLowerBoundInclusive(10, 11))
* will return true.
* while:
* assertThat(11, betweenLowerBoundInclusive(10, 11))
* will return false.
*/
public static > Matcher betweenLowerBoundInclusive(final T from, final T to) {
return new IsBetweenLowerBoundInclusive(from, to);
}
@Override
protected boolean matchesSafely(final T t) {
return t.compareTo(from) >= 0 && t.compareTo(to) < 0;
}
@Override
protected void describeMismatchSafely(final T item, final Description mismatchDescription) {
mismatchDescription.appendValue(item)
.appendText(" is not between ")
.appendValue(from)
.appendText(" included and ")
.appendValue(to)
.appendText(" excluded");
}
@Override
public void describeTo(final Description description) {
description.appendText("a value between ")
.appendValue(from)
.appendText(" included and ")
.appendValue(to)
.appendText(" excluded");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy