Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#235: Add matchers for Comparable objects #245

Merged
merged 14 commits into from
May 15, 2021
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs-annotations</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand All @@ -86,7 +97,6 @@
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.18.19</version>
</plugin>
<plugin>
<groupId>de.thetaphi</groupId>
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/IsEqualTo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.llorllale.cactoos.matchers;

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

/**
* Is {@link Comparable} equal to.
*
* @param <T> Underlying type.
* @since 1.0.0
*/
public final class IsEqualTo<T extends Comparable<? super T>> extends
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreoss I'm not totally convinced by that name… especially because there is already a class named like this in hamcrest :/ maybe IsComparableTo? It's not very good either to be honest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@victornoel IsComparableEqualTo, IsComparableGreaterThan, etc?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreoss yep, I think it's not bad what you propose, a bit verbose but no ambiguity :) let's that! thx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MatcherEnvelope<T> {
/**
* Ctor.
* @param expected The expected value
*/
public IsEqualTo(final T expected) {
this(new NaturalOrdering<>(), expected);
}

/**
* Ctor.
* @param comparator The comparator.
* @param expected The expected value
*/
public IsEqualTo(final Comparator<? super T> comparator, final T expected) {
super(
ComparatorMatcherBuilder
.comparedBy(comparator)
.comparesEqualTo(expected)
);
}

}
53 changes: 53 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/IsGreaterThan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.llorllale.cactoos.matchers;

import org.hamcrest.comparator.ComparatorMatcherBuilder;

/**
* Is {@link Comparable} object greater than.
*
* @param <T> Underlying type.
* @since 1.0.0
*/
public final class IsGreaterThan<T extends Comparable<? super T>> extends
MatcherEnvelope<T> {

/**
* Ctor.
*
* @param expected The expected value
*/
public IsGreaterThan(final T expected) {
super(
ComparatorMatcherBuilder
.comparedBy(new NaturalOrdering<T>())
.greaterThan(expected)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.llorllale.cactoos.matchers;

import org.hamcrest.comparator.ComparatorMatcherBuilder;

/**
* Is {@link Number} greater than or equal to.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreoss this is not only about Number, right?

*
* @param <T> Underlying type.
* @since 1.0.0
*/
public final class IsGreaterThanOrEqualTo<T extends Comparable<? super T>> extends
MatcherEnvelope<T> {
/**
* Ctor.
* @param expected The expected value
*/
public IsGreaterThanOrEqualTo(final T expected) {
super(
ComparatorMatcherBuilder
.comparedBy(new NaturalOrdering<T>())
.greaterThanOrEqualTo(expected)
);
}

}
51 changes: 51 additions & 0 deletions src/main/java/org/llorllale/cactoos/matchers/IsLessThan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.llorllale.cactoos.matchers;

import org.hamcrest.comparator.ComparatorMatcherBuilder;

/**
* Is {@link Comparable} object greater than.
*
* @param <T> Underlying type.
* @since 1.0.0
*/
public final class IsLessThan<T extends Comparable<? super T>> extends
MatcherEnvelope<T> {
/**
* Ctor.
* @param expected The expected value
*/
public IsLessThan(final T expected) {
super(
ComparatorMatcherBuilder
.comparedBy(new NaturalOrdering<T>())
.lessThan(expected)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Copyright (c) for portions of project cactoos-matchers are held by
* Yegor Bugayenko, 2017-2018, as part of project cactoos.
* All other copyright for project cactoos-matchers are held by
* George Aristy, 2018-2020.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.llorllale.cactoos.matchers;

import org.hamcrest.comparator.ComparatorMatcherBuilder;

/**
* Is {@link Comparable} object less than or equal to.
*
* @param <T> Underlying type.
* @since 1.0.0
*/
public final class IsLessThanOrEqualTo<T extends Comparable<? super T>> extends
MatcherEnvelope<T> {
/**
* Ctor.
* @param expected The expected value
*/
public IsLessThanOrEqualTo(final T expected) {
super(
ComparatorMatcherBuilder
.comparedBy(new NaturalOrdering<T>())
.lessThanOrEqualTo(expected)
);
}

}
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 @@ -40,13 +40,13 @@ public abstract class MatcherEnvelope<T> extends BaseMatcher<T> {
/**
* The matcher to test.
*/
private final Matcher<T> origin;
private final Matcher<? super T> origin;

/**
* Ctor.
* @param origin Encapsulated matcher.
*/
protected MatcherEnvelope(final Matcher<T> origin) {
protected MatcherEnvelope(final Matcher<? super T> origin) {
super();
this.origin = origin;
}
Expand Down