[COLLECTIONS-708] Add hashcode method to CollectionUtils that supports an equator parameter

This commit is contained in:
dota17 2019-10-22 09:55:26 +08:00 committed by Bruno P. Kinoshita
parent 62febb3ffb
commit d0551d01b3
2 changed files with 74 additions and 1 deletions

View File

@ -643,6 +643,32 @@ public class CollectionUtils {
return isEqualCollection(collect(a, transformer), collect(b, transformer));
}
/**
* Returns the hash code the the input collection using the hash method of the equator.
* <p>
* If the input collection is null return 0.
* </p>
*
* @param <E> the element type
* @param collection the input collection
* @param equator the equator used for generate hashCode
* @return the hashCode of Collection through the hash method of Equator
* @throws NullPointerException if the equator is null
* @since 4.5
*/
public static <E> int hashCode(final Collection<? extends E> collection,
final Equator<? super E> equator) {
Objects.requireNonNull(equator, "equator");
if (null == collection) {
return 0;
}
int hashCode = 1;
for (final E e : collection) {
hashCode = 31 * hashCode + equator.hash(e);
}
return hashCode;
}
/**
* Wraps another object and uses the provided Equator to implement
* {@link #equals(Object)} and {@link #hashCode()}.

View File

@ -37,6 +37,7 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.SortedMap;
@ -741,6 +742,52 @@ public class CollectionUtilsTest extends MockTestCase {
CollectionUtils.isEqualCollection(collectionA, collectionA, null);
}
@Test
public void testHashCode() {
final Equator<Integer> e = new Equator<Integer>() {
@Override
public boolean equate(final Integer o1, final Integer o2) {
if (o1 % 2 == 0 ^ o2 % 2 == 0) {
return false;
}
return true;
}
@Override
public int hash(final Integer o) {
return o == null ? 0 : Objects.hashCode(o);
}
};
assertEquals(collectionA.hashCode(), CollectionUtils.hashCode(collectionA, e));
}
@Test
public void testHashCodeNullCollection() {
final Equator<Integer> e = new Equator<Integer>() {
@Override
public boolean equate(final Integer o1, final Integer o2) {
if (o1 % 2 == 0 ^ o2 % 2 == 0) {
return false;
}
return true;
}
@Override
public int hash(final Integer o) {
return o == null ? 0 : Objects.hashCode(o);
}
};
final Collection<Integer> collection = null;
assertEquals(0, CollectionUtils.hashCode(collection, e));
}
@Test(expected=NullPointerException.class)
public void testHashCodeNullEquator() {
CollectionUtils.hashCode(collectionB, null);
}
@Test(expected = NullPointerException.class)
public void testIsEqualCollectionNullColl1() {
final Collection<Integer> list = new ArrayList<>(1);
@ -1073,7 +1120,7 @@ public class CollectionUtilsTest extends MockTestCase {
CollectionUtils.get(array, 2);
}
@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void getFromObject() throws Exception {
// Invalid object
final Object obj = new Object();