diff --git a/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java b/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java index 6143d17583..b59379e4ad 100644 --- a/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java +++ b/src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java @@ -17,6 +17,7 @@ package org.apache.commons.collections4.functors; import java.io.Serializable; +import java.util.Objects; import org.apache.commons.collections4.Equator; import org.apache.commons.collections4.Predicate; @@ -64,7 +65,7 @@ public static Predicate equalPredicate(final T object, final Equator e } /** The value to compare to */ - private final T iValue; + private final T test; /** The equator to use for comparison */ private final Equator equator; @@ -85,12 +86,12 @@ public EqualPredicate(final T object) { * Constructor that performs no validation. * Use {@code equalPredicate} if you want that. * - * @param object the object to compare to + * @param test the object to compare to * @param equator the equator to use for comparison * @since 4.0 */ - public EqualPredicate(final T object, final Equator equator) { - iValue = object; + public EqualPredicate(final T test, final Equator equator) { + this.test = test; this.equator = equator; } @@ -101,7 +102,7 @@ public EqualPredicate(final T object, final Equator equator) { * @since 3.1 */ public Object getValue() { - return iValue; + return test; } /** @@ -113,9 +114,9 @@ public Object getValue() { @Override public boolean test(final T object) { if (equator != null) { - return equator.equate(iValue, object); + return equator.equate(test, object); } - return iValue.equals(object); + return Objects.equals(test, object); } } diff --git a/src/test/java/org/apache/commons/collections4/functors/EqualPredicateTest.java b/src/test/java/org/apache/commons/collections4/functors/EqualPredicateTest.java index 990d7e37b8..bb9627bbca 100644 --- a/src/test/java/org/apache/commons/collections4/functors/EqualPredicateTest.java +++ b/src/test/java/org/apache/commons/collections4/functors/EqualPredicateTest.java @@ -52,7 +52,10 @@ public void testNullArgumentEqualsNullPredicate() throws Exception { @Test public void testObjectFactoryUsesEqualsForTest() throws Exception { final Predicate predicate = EqualPredicate.equalPredicate(FALSE_OBJECT); - assertPredicateFalse(predicate, FALSE_OBJECT); + assertPredicateFalse(predicate, null); + assertPredicateFalse(predicate, TRUE_OBJECT); // different object + assertPredicateTrue(predicate, FALSE_OBJECT); // the same object + assertPredicateFalse(predicate, new EqualsTestObject(false)); // different object but always returns false from equals() assertPredicateTrue(EqualPredicate.equalPredicate(TRUE_OBJECT), TRUE_OBJECT); }