This commit is contained in:
Gary Gregory 2022-06-06 10:22:43 -04:00
commit 5e9b391ce2
10 changed files with 31 additions and 28 deletions

View File

@ -97,6 +97,9 @@
<action issue="COLLECTIONS-812" type="fix" dev="kinow" due-to="Ng Tsz Sum">
Fix flaky EmptyPropertiesTest#testSave.
</action>
<action type="fix" dev="ggregory" due-to="Steve Bosman">
Use java.lang.Objects#equals; eliminate a couple of nulls #307
</action>
<!-- ADD -->
<action issue="COLLECTIONS-760" dev="kinow" type="add" due-to="Isira Seneviratne">
Add tests for MapUtils.

View File

@ -443,7 +443,7 @@ public class CollectionUtils {
while (it.hasNext()) {
final Object p = it.next();
elementsAlreadySeen.add(p);
if (nextElement == null ? p == null : nextElement.equals(p)) {
if (Objects.equals(nextElement, p)) {
foundCurrentElement = true;
break;
}

View File

@ -323,14 +323,12 @@ public class ListUtils {
final Iterator<?> it1 = list1.iterator();
final Iterator<?> it2 = list2.iterator();
Object obj1 = null;
Object obj2 = null;
while (it1.hasNext() && it2.hasNext()) {
obj1 = it1.next();
obj2 = it2.next();
final Object obj1 = it1.next();
final Object obj2 = it2.next();
if (!(obj1 == null ? obj2 == null : obj1.equals(obj2))) {
if (!(Objects.equals(obj1, obj2))) {
return false;
}
}

View File

@ -22,6 +22,7 @@ import java.util.BitSet;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
/**
* A ComparatorChain is a Comparator that wraps one or more Comparators in
@ -338,9 +339,8 @@ public class ComparatorChain<E> implements Comparator<E>, Serializable {
}
if (object.getClass().equals(this.getClass())) {
final ComparatorChain<?> chain = (ComparatorChain<?>) object;
return (null == orderingBits ? null == chain.orderingBits : orderingBits.equals(chain.orderingBits)) &&
(null == comparatorChain ? null == chain.comparatorChain :
comparatorChain.equals(chain.comparatorChain));
return (Objects.equals(orderingBits, chain.orderingBits)) &&
(Objects.equals(comparatorChain, chain.comparatorChain));
}
return false;
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.collections4.comparators;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Objects;
import org.apache.commons.collections4.ComparatorUtils;
import org.apache.commons.collections4.Transformer;
@ -120,8 +121,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 (Objects.equals(decorated, comp.decorated)) &&
(Objects.equals(transformer, comp.transformer));
}
return false;
}

View File

@ -17,6 +17,7 @@
package org.apache.commons.collections4.functors;
import java.io.Serializable;
import java.util.Objects;
import org.apache.commons.collections4.Equator;
@ -57,11 +58,11 @@ public class DefaultEquator<T> implements Equator<T>, Serializable {
}
/**
* {@inheritDoc} Delegates to {@link Object#equals(Object)}.
* {@inheritDoc} Delegates to {@link Objects#equals(Object, Object)}.
*/
@Override
public boolean equate(final T o1, final T o2) {
return o1 == o2 || o1 != null && o1.equals(o2);
return Objects.equals(o1, o2);
}
/**

View File

@ -26,6 +26,7 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
@ -1187,7 +1188,7 @@ public class Flat3Map<K, V> implements IterableMap<K, V>, Serializable, Cloneabl
return false;
}
otherValue = other.get(key3);
if (value3 == null ? otherValue != null : !value3.equals(otherValue)) {
if (!Objects.equals(value3, otherValue)) {
return false;
}
case 2:
@ -1195,7 +1196,7 @@ public class Flat3Map<K, V> implements IterableMap<K, V>, Serializable, Cloneabl
return false;
}
otherValue = other.get(key2);
if (value2 == null ? otherValue != null : !value2.equals(otherValue)) {
if (!Objects.equals(value2, otherValue)) {
return false;
}
case 1:
@ -1203,7 +1204,7 @@ public class Flat3Map<K, V> implements IterableMap<K, V>, Serializable, Cloneabl
return false;
}
otherValue = other.get(key1);
if (value1 == null ? otherValue != null : !value1.equals(otherValue)) {
if (!Objects.equals(value1, otherValue)) {
return false;
}
}

View File

@ -208,7 +208,7 @@ public final class StaticBucketMap<K, V> extends AbstractIterableMap<K, V> {
Node<K, V> n = buckets[hash];
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
if (Objects.equals(n.key, key)) {
return n.value;
}
@ -232,7 +232,7 @@ public final class StaticBucketMap<K, V> extends AbstractIterableMap<K, V> {
Node<K, V> n = buckets[hash];
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
if (Objects.equals(n.key, key)) {
return true;
}
@ -255,7 +255,7 @@ public final class StaticBucketMap<K, V> extends AbstractIterableMap<K, V> {
Node<K, V> n = buckets[i];
while (n != null) {
if (n.value == value || (n.value != null && n.value.equals(value))) {
if (Objects.equals(n.value, value)) {
return true;
}
@ -295,7 +295,7 @@ public final class StaticBucketMap<K, V> extends AbstractIterableMap<K, V> {
for (Node<K, V> next = n; next != null; next = next.next) {
n = next;
if (n.key == key || (n.key != null && n.key.equals(key))) {
if (Objects.equals(n.key, key)) {
final V returnVal = n.value;
n.value = value;
return returnVal;
@ -328,7 +328,7 @@ public final class StaticBucketMap<K, V> extends AbstractIterableMap<K, V> {
Node<K, V> prev = null;
while (n != null) {
if (n.key == key || (n.key != null && n.key.equals(key))) {
if (Objects.equals(n.key, key)) {
// Remove this node from the linked list of nodes.
if (null == prev) {
// This node was the head, set the next node to be the new head.
@ -647,7 +647,7 @@ public final class StaticBucketMap<K, V> extends AbstractIterableMap<K, V> {
synchronized (locks[hash]) {
for (Node<K, V> n = buckets[hash]; n != null; n = n.next) {
final Object k = n.getKey();
if ((k == obj) || ((k != null) && k.equals(obj))) {
if (Objects.equals(k, obj)) {
StaticBucketMap.this.remove(k);
return true;
}

View File

@ -23,6 +23,7 @@ import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import org.apache.commons.collections4.IteratorUtils;
@ -74,8 +75,7 @@ public abstract class AbstractMultiSet<E> extends AbstractCollection<E> implemen
public int getCount(final Object object) {
for (final Entry<E> entry : entrySet()) {
final E element = entry.getElement();
if (element == object ||
element != null && element.equals(object)) {
if (Objects.equals(element, object)) {
return entry.getCount();
}
}
@ -405,8 +405,7 @@ public abstract class AbstractMultiSet<E> extends AbstractCollection<E> implemen
final Object otherElement = other.getElement();
return this.getCount() == other.getCount() &&
(element == otherElement ||
element != null && element.equals(otherElement));
(Objects.equals(element, otherElement));
}
return false;
}

View File

@ -134,10 +134,10 @@ public abstract class AbstractBitwiseTrie<K, V> extends AbstractMap<K, V>
}
/**
* Returns true if both values are either null or equal.
* Delegates to {@link Objects#equals(Object, Object)}.
*/
static boolean compare(final Object a, final Object b) {
return a == null ? b == null : a.equals(b);
return Objects.equals(a, b);
}
/**