package uk.co.flyingsquirrels.test; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; public class Matchers { public static Matcher<Float> closeTo(float value) { return new IsCloseTo(value, 0.001f); } private static class IsCloseTo extends TypeSafeMatcher<Float> { private final float error; private final float value; public IsCloseTo(float value, float error) { this.error = error; this.value = value; } public boolean matchesSafely(Float item) { return Math.abs((item - value)) <= error; } public void describeTo(Description description) { description.appendText("a numeric value within ") .appendValue(error) .appendText(" of ") .appendValue(value); } } }