This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push: new e85e26e7b When possible use java.lang.Objects#equals; eliminated a couple of nulls (#307) e85e26e7b is described below commit e85e26e7bb433d38d7e383361f6f42c1a8d6aa50 Author: Steve Bosman <steve.bos...@opencastsoftware.com> AuthorDate: Mon Jun 6 15:06:12 2022 +0100 When possible use java.lang.Objects#equals; eliminated a couple of nulls (#307) --- .../org/apache/commons/collections4/CollectionUtils.java | 2 +- src/main/java/org/apache/commons/collections4/ListUtils.java | 8 +++----- .../commons/collections4/comparators/ComparatorChain.java | 6 +++--- .../collections4/comparators/TransformingComparator.java | 5 +++-- .../apache/commons/collections4/functors/DefaultEquator.java | 5 +++-- .../java/org/apache/commons/collections4/map/Flat3Map.java | 7 ++++--- .../org/apache/commons/collections4/map/StaticBucketMap.java | 12 ++++++------ .../commons/collections4/multiset/AbstractMultiSet.java | 7 +++---- .../commons/collections4/trie/AbstractBitwiseTrie.java | 4 ++-- 9 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index bd9ddf09a..5993ce15c 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -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; } diff --git a/src/main/java/org/apache/commons/collections4/ListUtils.java b/src/main/java/org/apache/commons/collections4/ListUtils.java index 6b518d246..2e94515a6 100644 --- a/src/main/java/org/apache/commons/collections4/ListUtils.java +++ b/src/main/java/org/apache/commons/collections4/ListUtils.java @@ -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; } } diff --git a/src/main/java/org/apache/commons/collections4/comparators/ComparatorChain.java b/src/main/java/org/apache/commons/collections4/comparators/ComparatorChain.java index ee1fdb5ce..465309217 100644 --- a/src/main/java/org/apache/commons/collections4/comparators/ComparatorChain.java +++ b/src/main/java/org/apache/commons/collections4/comparators/ComparatorChain.java @@ -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; } diff --git a/src/main/java/org/apache/commons/collections4/comparators/TransformingComparator.java b/src/main/java/org/apache/commons/collections4/comparators/TransformingComparator.java index 2631a35cb..a582a6f21 100644 --- a/src/main/java/org/apache/commons/collections4/comparators/TransformingComparator.java +++ b/src/main/java/org/apache/commons/collections4/comparators/TransformingComparator.java @@ -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; } diff --git a/src/main/java/org/apache/commons/collections4/functors/DefaultEquator.java b/src/main/java/org/apache/commons/collections4/functors/DefaultEquator.java index 010ed78cf..63b79c94f 100644 --- a/src/main/java/org/apache/commons/collections4/functors/DefaultEquator.java +++ b/src/main/java/org/apache/commons/collections4/functors/DefaultEquator.java @@ -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); } /** diff --git a/src/main/java/org/apache/commons/collections4/map/Flat3Map.java b/src/main/java/org/apache/commons/collections4/map/Flat3Map.java index 8e55be622..4ac53fcaa 100644 --- a/src/main/java/org/apache/commons/collections4/map/Flat3Map.java +++ b/src/main/java/org/apache/commons/collections4/map/Flat3Map.java @@ -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; } } diff --git a/src/main/java/org/apache/commons/collections4/map/StaticBucketMap.java b/src/main/java/org/apache/commons/collections4/map/StaticBucketMap.java index a526e7386..d8ccb31a7 100644 --- a/src/main/java/org/apache/commons/collections4/map/StaticBucketMap.java +++ b/src/main/java/org/apache/commons/collections4/map/StaticBucketMap.java @@ -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; } diff --git a/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java b/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java index 34b2703ed..fc7babb60 100644 --- a/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java +++ b/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java @@ -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; } diff --git a/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java b/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java index c8cae4309..714b395c0 100644 --- a/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java +++ b/src/main/java/org/apache/commons/collections4/trie/AbstractBitwiseTrie.java @@ -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); } /**