Skip to content

Commit

Permalink
EqualPredicate.test(Object) should return true if the parameter is the
Browse files Browse the repository at this point in the history
same object as given the constructor
  • Loading branch information
garydgregory committed Dec 8, 2024
1 parent 38f1346 commit de4b7f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> e
}

/** The value to compare to */
private final T iValue;
private final T test;

/** The equator to use for comparison */
private final Equator<T> equator;
Expand All @@ -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<T> equator) {
iValue = object;
public EqualPredicate(final T test, final Equator<T> equator) {
this.test = test;
this.equator = equator;
}

Expand All @@ -101,7 +102,7 @@ public EqualPredicate(final T object, final Equator<T> equator) {
* @since 3.1
*/
public Object getValue() {
return iValue;
return test;
}

/**
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public void testNullArgumentEqualsNullPredicate() throws Exception {
@Test
public void testObjectFactoryUsesEqualsForTest() throws Exception {
final Predicate<EqualsTestObject> 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);
}

Expand Down

0 comments on commit de4b7f8

Please sign in to comment.