![JAR search and dependency download from the Maven repository](/logo.png)
it.ozimov.cirneco.hamcrest.number.IsInfinity Maven / Gradle / Ivy
package it.ozimov.cirneco.hamcrest.number;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.math.BigDecimal;
/**
* Is the value a {@linkplain Number} with infinite value?
*
*
* Observe that for Cirneco, only {@linkplain Double} and {@linkplain Float} admit an
* infinite value according to the JDK 6 and superior (included {@linkplain BigDecimal}).
* Any third party implementation of a Number
is not handled.
*
* @since version 0.1 for JDK7
*/
public class IsInfinity extends TypeSafeMatcher {
/**
* Creates a matcher for {@code N} that matches when the number is a {@linkplain Double}
* or {@linkplain Float} with value equal to either POSITIVE_INFINITY
or NEGATIVE_INFINITY
.
*
* For example:
*
assertThat(10, negativeInfinity())
* will return false
.
* while:
* assertThat(Double.PositiveInfinity, negativeInfinity())
* assertThat(Float.NegativeInfinity, negativeInfinity())
* will both return true
.
*/
public static Matcher infinity() {
return new IsInfinity<>();
}
@Override
protected boolean matchesSafely(final N number) {
if (number instanceof Double) {
return ((Double) number).isInfinite();
} else if (number instanceof Float) {
return ((Float) number).isInfinite();
}
return false;
}
@Override
protected void describeMismatchSafely(final N item, final Description mismatchDescription) {
mismatchDescription.appendValue(item)
.appendText(" is not infinite (positive or negative)");
}
@Override
public void describeTo(final Description description) {
description.appendText("a value equals to infinite (positive or negative)");
}
}