diff options
Diffstat (limited to 'hamcrest-library/src/main/java/org/hamcrest/number')
4 files changed, 111 insertions, 0 deletions
diff --git a/hamcrest-library/src/main/java/org/hamcrest/number/IsCloseTo.java b/hamcrest-library/src/main/java/org/hamcrest/number/IsCloseTo.java new file mode 100644 index 0000000..dc183e8 --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/number/IsCloseTo.java @@ -0,0 +1,40 @@ +/* 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<Double> { + private final double error; + private final double value; + + public IsCloseTo(double value, double error) { + this.error = error; + this.value = value; + } + + public boolean matchesSafely(Double item) { + return Math.abs((item - value)) <= error; + } + + public void describeTo(Description description) { + description.appendText("a numeric value within ") + .appendValue(error) + .appendText(" of ") + .appendValue(value); + } + + @Factory + public static Matcher<Double> closeTo(double operand, double error) { + return new IsCloseTo(operand, error); + } + +} diff --git a/hamcrest-library/src/main/java/org/hamcrest/number/IsGreaterThan.java b/hamcrest-library/src/main/java/org/hamcrest/number/IsGreaterThan.java new file mode 100644 index 0000000..34046b7 --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/number/IsGreaterThan.java @@ -0,0 +1,26 @@ +/* Copyright (c) 2000-2006 hamcrest.org + */ +package org.hamcrest.number; + +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; + +/** + * Is the value less than or greater than another {@link java.lang.Comparable} value? + */ +public class IsGreaterThan<T extends Comparable<T>> extends TypeSafeMatcher<T> { + private final Comparable<T> compareTo; + + public IsGreaterThan(Comparable<T> compareTo) { + this.compareTo = compareTo; + } + + public boolean matchesSafely(T item) { + return compareTo.compareTo(item) < 0; + } + + public void describeTo(Description description) { + description.appendText("a value greater than "); + description.appendValue(compareTo); + } +} diff --git a/hamcrest-library/src/main/java/org/hamcrest/number/OrderingComparisons.java b/hamcrest-library/src/main/java/org/hamcrest/number/OrderingComparisons.java new file mode 100644 index 0000000..1791a40 --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/number/OrderingComparisons.java @@ -0,0 +1,38 @@ +package org.hamcrest.number; + +import static org.hamcrest.core.AnyOf.anyOf; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.DescribedAs.describedAs; + +import org.hamcrest.Factory; +import org.hamcrest.Matcher; + +@SuppressWarnings("unchecked") +public class OrderingComparisons { + @Factory + public static <T extends Comparable<T>> Matcher<T> greaterThan(T value) { + return new IsGreaterThan<T>(value); + } + + @Factory + public static <T extends Comparable<T>> Matcher<T> greaterThanOrEqualTo(T value) { + return describedAs("a value greater than or equal to %0", + anyOf(greaterThan(value), equalTo(value)), + value); + } + + @Factory + public static <T extends Comparable<T>> Matcher<T> lessThan(T value) { + return describedAs("a value less than %0", + not(greaterThanOrEqualTo(value)), + value); + } + + @Factory + public static <T extends Comparable<T>> Matcher<T> lessThanOrEqualTo(T value) { + return describedAs("a value less than or equal to %0", + not(greaterThan(value)), + value); + } +} diff --git a/hamcrest-library/src/main/java/org/hamcrest/number/package.html b/hamcrest-library/src/main/java/org/hamcrest/number/package.html new file mode 100644 index 0000000..2fbb07f --- /dev/null +++ b/hamcrest-library/src/main/java/org/hamcrest/number/package.html @@ -0,0 +1,7 @@ +<html> +<head> +</head> +<body> + <p>Matchers that perform numeric comparisons.</p> +</body> +</html> |