Commit daa9c139 authored by mey's avatar mey
Browse files added factory method with custom error

    showing unit of first item in description
parent d22aff50
......@@ -2,34 +2,55 @@ package org.hamcrest;
import static org.hamcrest.Matchers.closeTo;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import javax.measure.unit.Unit;
import org.hamcrest.number.IsCloseTo;
import org.jscience.physics.amount.Amount;
* {@code IsCloseTo} implementation for {@link Amount}.
* @see IsCloseTo
* @author mey
public class AmountCloseTo extends TypeSafeMatcher<Amount<?>> {
private static final double DEFAULT_ERROR = 1E-14d;
private final Amount<?> amount;
private static final double MAX_ERROR = 1E-14d;
private final double error;
AmountCloseTo(Class<?> expectedType, Amount<?> amount) {
public AmountCloseTo(Class<?> expectedType, Amount<?> amount, double error) {
this.amount = amount;
this.error = error;
public void describeTo(Description description) {
closeTo(amount.getEstimatedValue(), MAX_ERROR).describeTo(description);
closeTo(amount.getEstimatedValue(), error).describeTo(description);
description.appendText(" ").appendValue(amount.getUnit());
protected boolean matchesSafely(Amount<?> item) {
return Matchers.closeTo(amount.getEstimatedValue(), MAX_ERROR)
Unit<?> unit = amount.getUnit();
return Matchers.closeTo(amount.getEstimatedValue(), error)
* @param amount
* @return {@link Matcher} with default error
public static Matcher<Amount<?>> amountCloseTo(final Amount<?> amount) {
return amountCloseTo(amount, DEFAULT_ERROR);
public static org.hamcrest.Matcher<Amount<?>> amountCloseTo(final Amount<?> amount) {
return new AmountCloseTo(Amount.class, amount);
public static Matcher<Amount<?>> amountCloseTo(final Amount<?> amount, double error) {
return new AmountCloseTo(Amount.class, amount, error);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment