commit
d5de68c2d1
|
@ -21,6 +21,9 @@
|
|||
</properties>
|
||||
<body>
|
||||
<release version="4.5" date="2020-MM-DD" description="Maintenance release.">
|
||||
<action issue="COLLECTIONS-708" dev="kinow" type="fix" due-to="dota17">
|
||||
Add hashCode method to CollectionUtils that supports an equator parameter.
|
||||
</action>
|
||||
<action issue="COLLECTIONS-759" dev="kinow" type="fix">
|
||||
Fix checkstyle issues regarding missing newline at end of file, and CRLF vs LF.
|
||||
</action>
|
||||
|
|
|
@ -643,6 +643,33 @@ public class CollectionUtils {
|
|||
return isEqualCollection(collect(a, transformer), collect(b, transformer));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hash code of the input collection using the hash method of an equator.
|
||||
*
|
||||
* <p>
|
||||
* Returns 0 if the input collection is {@code null}.
|
||||
* </p>
|
||||
*
|
||||
* @param <E> the element type
|
||||
* @param collection the input collection
|
||||
* @param equator the equator used for generate hashCode
|
||||
* @return the hash code of the input collection using the hash method of an equator
|
||||
* @throws NullPointerException if the equator is {@code 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()}.
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue