diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index bd9ddf09a5..5993ce15c9 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 static boolean containsAll(final Collection coll1, final Collection 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 6b518d246f..2e94515a67 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 static boolean isEqualList(final Collection list1, final Collection 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 ee1fdb5cea..4653092172 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.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 boolean equals(final Object object) { } 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 2631a35cb9..a582a6f21c 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 @@ 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 boolean equals(final Object object) { } 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 010ed78cff..63b79c94f5 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 @@ private DefaultEquator() { } /** - * {@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 8e55be6220..4ac53fcaa8 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.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 boolean equals(final Object obj) { 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 boolean equals(final Object obj) { 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 boolean equals(final Object obj) { 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 a526e73861..d8ccb31a7a 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 V get(final Object key) { Node 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 boolean containsKey(final Object key) { Node 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 boolean containsValue(final Object value) { Node 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 V put(final K key, final V value) { for (Node 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 V remove(final Object key) { Node 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 boolean remove(final Object obj) { synchronized (locks[hash]) { for (Node 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 34b2703ed8..fc7babb605 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.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 int size() { public int getCount(final Object object) { for (final Entry 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 boolean equals(final Object object) { 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 c8cae4309b..714b395c00 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 @@ final boolean compareKeys(final K key, final K other) { } /** - * 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); } /**