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; ...@@ -2,34 +2,55 @@ package org.hamcrest;
import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.closeTo;
import org.hamcrest.Description; import javax.measure.unit.Unit;
import org.hamcrest.Factory;
import org.hamcrest.Matchers; import org.hamcrest.number.IsCloseTo;
import org.hamcrest.TypeSafeMatcher;
import org.jscience.physics.amount.Amount; import org.jscience.physics.amount.Amount;
* {@code IsCloseTo} implementation for {@link Amount}.
* @see IsCloseTo
* @author mey
public class AmountCloseTo extends TypeSafeMatcher<Amount<?>> { public class AmountCloseTo extends TypeSafeMatcher<Amount<?>> {
private static final double DEFAULT_ERROR = 1E-14d;
private final Amount<?> amount; 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) {
super(expectedType); super(expectedType);
this.amount = amount; this.amount = amount;
this.error = error;
} }
@Override @Override
public void describeTo(Description description) { public void describeTo(Description description) {
closeTo(amount.getEstimatedValue(), MAX_ERROR).describeTo(description); closeTo(amount.getEstimatedValue(), error).describeTo(description);
description.appendText(" ").appendValue(amount.getUnit());
} }
@Override @Override
protected boolean matchesSafely(Amount<?> item) { protected boolean matchesSafely(Amount<?> item) {
return Matchers.closeTo(amount.getEstimatedValue(), MAX_ERROR) Unit<?> unit = amount.getUnit();
.matches(; 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);
} }
@Factory @Factory
public static org.hamcrest.Matcher<Amount<?>> amountCloseTo(final Amount<?> amount) { public static Matcher<Amount<?>> amountCloseTo(final Amount<?> amount, double error) {
return new AmountCloseTo(Amount.class, amount); return new AmountCloseTo(Amount.class, amount, error);
} }
} }
\ No newline at end of file
Supports Markdown
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