Skip to content

Commit

Permalink
(llorllale#235) Use NaturalOrdering in IsNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoss committed May 9, 2021
1 parent 8c85d1b commit d0efdb9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 27 deletions.
30 changes: 5 additions & 25 deletions src/main/java/org/llorllale/cactoos/matchers/IsNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,24 @@
*/
package org.llorllale.cactoos.matchers;

import java.util.Comparator;
import org.hamcrest.comparator.ComparatorMatcherBuilder;

/**
* Matcher for {@link Number}.
* Matcher for {@link Number} equality.
*
* @since 1.0.0
*/
public final class IsNumber extends MatcherEnvelope<Number> {

/**
* Comparator of numbers.
*/
private static final Comparator<Number> FNC =
Comparator
.comparing(Number::doubleValue)
.thenComparing(Number::intValue)
.thenComparing(Number::longValue)
.thenComparing(Number::floatValue);

/**
* Ctor.
* @param expected The expected value
* @todo #165:30min Introduce a ComparatorMatcher that encapsulates comparison logic here.
* It would only take a {@code Comparator<X>} and an expected X for example.
*/
public IsNumber(final Number expected) {
super(
new MatcherOf<>(
actual -> IsNumber.FNC.compare(actual, expected) == 0,
desc -> desc.appendText("equals ").appendValue(expected),
(actual, desc) -> desc
.appendText("comparator returns ")
.appendValue(IsNumber.FNC.compare(actual, expected))
.appendText(" when ")
.appendValue(expected)
.appendText(" compared to ")
.appendValue(actual)
)
ComparatorMatcherBuilder
.comparedBy(new NumberComparator())
.comparesEqualTo(expected)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void mismatchesDouble() {
new IsNumber(Double.POSITIVE_INFINITY),
new Mismatches<>(
1234,
"equals <Infinity>",
"comparator returns <-1> when <Infinity> compared to <1234>"
"a value equal to <Infinity> when compared by <NumberComparator{}>",
"<1234> was less than <Infinity> when compared by <NumberComparator{}>"
)
).affirm();
}
Expand Down

0 comments on commit d0efdb9

Please sign in to comment.