[COLLECTIONS-512] Fix equals method for TransformingComparator. Thanks to Cyrille Artho.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1586477 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2014-04-10 21:55:29 +00:00
parent a431ff8881
commit bc85bab7a5
3 changed files with 20 additions and 2 deletions

View File

@ -22,6 +22,9 @@
<body>
<release version="4.1" date="TBD" description="">
<action issue="COLLECTIONS-512" dev="tn" type="fix" due-to="Cyrille Artho">
"TransformingComparator" did not comply with the contract of Object#equals.
</action>
<action issue="COLLECTIONS-510" dev="tn" type="fix" due-to="Hollis Waite">
Fix compilation errors when using source level 1.8 and a recent java 8 compiler.
</action>

View File

@ -120,8 +120,8 @@ public class TransformingComparator<I, O> implements Comparator<I>, Serializable
}
if (object.getClass().equals(this.getClass())) {
final TransformingComparator<?, ?> comp = (TransformingComparator<?, ?>) object;
return null == decorated ? null == comp.decorated : decorated.equals(comp.decorated) &&
null == transformer ? null == comp.transformer : transformer.equals(comp.transformer);
return (null == decorated ? null == comp.decorated : decorated.equals(comp.decorated)) &&
(null == transformer ? null == comp.transformer : transformer.equals(comp.transformer));
}
return false;
}

View File

@ -21,6 +21,7 @@ import java.util.LinkedList;
import java.util.List;
import org.apache.commons.collections4.ComparatorUtils;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.TransformerUtils;
/**
@ -60,6 +61,20 @@ public class TransformingComparatorTest extends AbstractComparatorTest<Integer>
return list;
}
public void testEquals() {
Transformer<String, String> t1 = TransformerUtils.nopTransformer();
TransformingComparator<String, String> comp1 = new TransformingComparator<String, String>(t1);
TransformingComparator<String, String> comp2 = new TransformingComparator<String, String>(t1, comp1);
// Checks the contract: equals-hashcode on comp1 and comp2
assertTrue("Contract failed: equals-hashcode",
comp1.equals(comp2) ? comp1.hashCode() == comp2.hashCode() : true);
// Checks the contract: equals-hashcode on comp1 and comp2
assertTrue("Contract failed: equals-hashcode",
comp2.equals(comp1) ? comp2.hashCode() == comp1.hashCode() : true);
}
@Override
public String getCompatibilityVersion() {
return "4";