Skip to content

Commit de4b7f8

Browse files
committed
EqualPredicate.test(Object) should return true if the parameter is the
same object as given the constructor
1 parent 38f1346 commit de4b7f8

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/main/java/org/apache/commons/collections4/functors/EqualPredicate.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.collections4.functors;
1818

1919
import java.io.Serializable;
20+
import java.util.Objects;
2021

2122
import org.apache.commons.collections4.Equator;
2223
import org.apache.commons.collections4.Predicate;
@@ -64,7 +65,7 @@ public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> e
6465
}
6566

6667
/** The value to compare to */
67-
private final T iValue;
68+
private final T test;
6869

6970
/** The equator to use for comparison */
7071
private final Equator<T> equator;
@@ -85,12 +86,12 @@ public EqualPredicate(final T object) {
8586
* Constructor that performs no validation.
8687
* Use {@code equalPredicate} if you want that.
8788
*
88-
* @param object the object to compare to
89+
* @param test the object to compare to
8990
* @param equator the equator to use for comparison
9091
* @since 4.0
9192
*/
92-
public EqualPredicate(final T object, final Equator<T> equator) {
93-
iValue = object;
93+
public EqualPredicate(final T test, final Equator<T> equator) {
94+
this.test = test;
9495
this.equator = equator;
9596
}
9697

@@ -101,7 +102,7 @@ public EqualPredicate(final T object, final Equator<T> equator) {
101102
* @since 3.1
102103
*/
103104
public Object getValue() {
104-
return iValue;
105+
return test;
105106
}
106107

107108
/**
@@ -113,9 +114,9 @@ public Object getValue() {
113114
@Override
114115
public boolean test(final T object) {
115116
if (equator != null) {
116-
return equator.equate(iValue, object);
117+
return equator.equate(test, object);
117118
}
118-
return iValue.equals(object);
119+
return Objects.equals(test, object);
119120
}
120121

121122
}

src/test/java/org/apache/commons/collections4/functors/EqualPredicateTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public void testNullArgumentEqualsNullPredicate() throws Exception {
5252
@Test
5353
public void testObjectFactoryUsesEqualsForTest() throws Exception {
5454
final Predicate<EqualsTestObject> predicate = EqualPredicate.equalPredicate(FALSE_OBJECT);
55-
assertPredicateFalse(predicate, FALSE_OBJECT);
55+
assertPredicateFalse(predicate, null);
56+
assertPredicateFalse(predicate, TRUE_OBJECT); // different object
57+
assertPredicateTrue(predicate, FALSE_OBJECT); // the same object
58+
assertPredicateFalse(predicate, new EqualsTestObject(false)); // different object but always returns false from equals()
5659
assertPredicateTrue(EqualPredicate.equalPredicate(TRUE_OBJECT), TRUE_OBJECT);
5760
}
5861

0 commit comments

Comments
 (0)