Skip to content

Commit

Permalink
(#1174) Allow using Equals on ComparableText objects
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Feb 26, 2021
1 parent 82dd93e commit ffe6721
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
9 changes: 5 additions & 4 deletions src/main/java/org/cactoos/scalar/Equals.java
Expand Up @@ -36,10 +36,11 @@
* use the {@link Unchecked} decorator. Or you may use
* {@link IoChecked} to wrap it in an IOException.</p>
*
* @param <R> Type of object to compare
* @param <T> Type of object to compare
* @since 0.9
*/
public final class Equals<T extends Comparable<T>> implements Scalar<Boolean> {
public final class Equals<R, T extends Comparable<R>> implements Scalar<Boolean> {

/**
* The first scalar.
Expand All @@ -49,14 +50,14 @@ public final class Equals<T extends Comparable<T>> implements Scalar<Boolean> {
/**
* The second scalar.
*/
private final Scalar<? extends T> second;
private final Scalar<? extends R> second;

/**
* Ctor.
* @param source The first scalar to compare.
* @param compared The second scalar to compare.
*/
public Equals(final T source, final T compared) {
public Equals(final T source, final R compared) {
this(new Constant<>(source), new Constant<>(compared));
}

Expand All @@ -65,7 +66,7 @@ public Equals(final T source, final T compared) {
* @param source The first scalar to compare.
* @param compared The second scalar to compare.
*/
public Equals(final Scalar<? extends T> source, final Scalar<? extends T> compared) {
public Equals(final Scalar<? extends T> source, final Scalar<? extends R> compared) {
this.first = source;
this.second = compared;
}
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/cactoos/scalar/ScalarWithFallback.java
Expand Up @@ -24,6 +24,7 @@
package org.cactoos.scalar;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import org.cactoos.Fallback;
import org.cactoos.Scalar;
Expand All @@ -41,6 +42,7 @@
* @param <T> Type of result
* @see FuncWithFallback
* @since 0.31
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class ScalarWithFallback<T> implements Scalar<T> {

Expand Down Expand Up @@ -103,16 +105,18 @@ public T value() throws Exception {
*/
@SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes")
private T fallback(final Throwable exp) throws Exception {
final Sorted<Map.Entry<Fallback<T>, Integer>> candidates =
final Iterator<Map.Entry<Fallback<T>, Integer>> candidates =
new Sorted<>(
Comparator.comparing(Map.Entry::getValue),
new Filtered<>(
entry -> new Not(
new Equals<>(
entry::getValue,
() -> Integer.MIN_VALUE
new org.cactoos.func.Flattened<>(
entry -> new Not(
new Equals<Integer, Integer>(
entry::getValue,
new Constant<>(Integer.MIN_VALUE)
)
)
).value(),
),
new MapOf<>(
fbk -> fbk,
fbk -> fbk.support(exp),
Expand Down
34 changes: 18 additions & 16 deletions src/test/java/org/cactoos/scalar/EqualsTest.java
Expand Up @@ -23,6 +23,8 @@
*/
package org.cactoos.scalar;

import org.cactoos.text.ComparableText;
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasValue;
Expand All @@ -31,18 +33,15 @@
* Test case for {@link Equals}.
*
* @since 0.9
* @checkstyle JavadocMethodCheck (500 lines)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class EqualsTest {

@Test
void compareEquals() throws Exception {
new Assertion<>(
"Must compare if two integers are equal",
new Equals<>(
Integer.valueOf(1),
Integer.valueOf(1)
),
new Equals<>(1, 1),
new HasValue<>(true)
).affirm();
}
Expand All @@ -51,10 +50,7 @@ void compareEquals() throws Exception {
void compareNotEquals() throws Exception {
new Assertion<>(
"Must compare if two integers are not equal",
new Equals<>(
Integer.valueOf(1),
Integer.valueOf(2)
),
new Equals<>(1, 2),
new HasValue<>(false)
).affirm();
}
Expand All @@ -64,10 +60,7 @@ void compareEqualsTextScalar() throws Exception {
final String str = "hello";
new Assertion<>(
"Must compare if two strings are equal",
new Equals<>(
str,
str
),
new Equals<>(str, str),
new HasValue<>(true)
).affirm();
}
Expand All @@ -76,11 +69,20 @@ void compareEqualsTextScalar() throws Exception {
void compareNotEqualsTextScalar() throws Exception {
new Assertion<>(
"Must compare if two strings are not equal",
new Equals<>("world", "worle"),
new HasValue<>(false)
).affirm();
}

@Test
void compareText() throws Exception {
new Assertion<>(
"Must compare if two comparable test are equal, see #1174",
new Equals<>(
"world",
"worle"
new ComparableText(new TextOf("hello")),
new ComparableText(new TextOf("hello"))
),
new HasValue<>(false)
new HasValue<>(true)
).affirm();
}
}

0 comments on commit ffe6721

Please sign in to comment.