![JAR search and dependency download from the Maven repository](/logo.png)
org.hamcrest.number.IsCloseTo Maven / Gradle / Ivy
/* Copyright (c) 2000-2006 hamcrest.org
*/
package org.hamcrest.number;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Factory;
import org.hamcrest.TypeSafeMatcher;
/**
* Is the value a number equal to a value within some range of
* acceptable error?
*/
public class IsCloseTo extends TypeSafeMatcher {
private final double delta;
private final double value;
public IsCloseTo(double value, double error) {
this.delta = error;
this.value = value;
}
@Override
public boolean matchesSafely(Double item) {
return actualDelta(item) <= 0.0;
}
@Override
public void describeMismatchSafely(Double item, Description mismatchDescription) {
mismatchDescription.appendValue(item)
.appendText(" differed by ")
.appendValue(actualDelta(item));
}
public void describeTo(Description description) {
description.appendText("a numeric value within ")
.appendValue(delta)
.appendText(" of ")
.appendValue(value);
}
private double actualDelta(Double item) {
return (Math.abs((item - value)) - delta);
}
@Factory
public static Matcher closeTo(double operand, double error) {
return new IsCloseTo(operand, error);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy