From a73908ffad3a1b731272bad015f1406c59043d6a Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 23 Sep 2011 16:26:59 -0700 Subject: [PATCH] Order our equals() comparisons in the same way Java Collections do. ArrayList and friends explicitly call equals() on the supplied object with the array object as the argument, rather than the other way around. This allows one to, for example, pass in some sort of Key object that can compare itself for equality against a list of KeyedValue objects. This sort of delicate dance around the equality contract is dangerous, but we should let the caller decide whether they wish to dance with the devil. --- .../java/com/samskivert/util/ListUtil.java | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/samskivert/util/ListUtil.java b/src/main/java/com/samskivert/util/ListUtil.java index f6e51e12..2e21aebf 100644 --- a/src/main/java/com/samskivert/util/ListUtil.java +++ b/src/main/java/com/samskivert/util/ListUtil.java @@ -218,7 +218,7 @@ public class ListUtil index = i; } - } else if (eqc.equals(elem, element)) { + } else if (eqc.equals(element, elem)) { // oops, it's already in the list return null; } @@ -248,7 +248,7 @@ public class ListUtil /** * Looks for an element that is functionally equal to the supplied - * element (list[idx].equals(element)). + * element (element.equals(list[idx])). * * @return true if a matching element was found, false otherwise. */ @@ -295,7 +295,7 @@ public class ListUtil /** * Looks for an element that is functionally equal to the supplied - * element (list[idx].equals(element)). + * element (element.equals(list[idx])). * * @return the index of the matching element if one was found, -1 * otherwise. @@ -312,7 +312,7 @@ public class ListUtil requireNotNull(element); if (list != null) { for (int ii = 0, nn = list.length; ii < nn; ii++) { - if (eqc.equals(list[ii], element)) { + if (eqc.equals(element, list[ii])) { return ii; } } @@ -333,7 +333,7 @@ public class ListUtil /** * Clears out the first element that is functionally equal to the - * supplied element (list[idx].equals(element)). + * supplied element (element.equals(list[idx])). * * @return the object that was cleared from the array or null if no * matching object was found. @@ -372,7 +372,7 @@ public class ListUtil /** * Removes the first element that is functionally equal to the - * supplied element (list[idx].equals(element)). The + * supplied element (element.equals(list[idx])). The * elements after the removed element will be slid down the array one * spot to fill the place of the removed element. * @@ -553,21 +553,17 @@ public class ListUtil public boolean equals (Object o1, Object o2); } - protected static final EqualityComparator REFERENCE_COMP = - new EqualityComparator() { - public boolean equals (Object o1, Object o2) - { - return o1 == o2; - } - }; + protected static final EqualityComparator REFERENCE_COMP = new EqualityComparator() { + public boolean equals (Object o1, Object o2) { + return o1 == o2; + } + }; - protected static final EqualityComparator EQUALS_COMP = - new EqualityComparator() { - public boolean equals (Object o1, Object o2) - { - return ObjectUtil.equals(o1, o2); - } - }; + protected static final EqualityComparator EQUALS_COMP = new EqualityComparator() { + public boolean equals (Object o1, Object o2) { + return ObjectUtil.equals(o1, o2); + } + }; /** * The size of a list to create if we have to create one entirely