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
commit 38f1346176d439b0446dffbbe95fab484c24eb45 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Dec 8 17:05:58 2024 -0500 Reuse Objects.equals() in org.apache.commons.collections4.list --- src/changes/changes.xml | 1 + .../commons/collections4/bidimap/AbstractDualBidiMap.java | 2 +- .../org/apache/commons/collections4/bidimap/TreeBidiMap.java | 10 +++++----- .../commons/collections4/functors/ConstantTransformer.java | 3 ++- .../apache/commons/collections4/keyvalue/AbstractMapEntry.java | 6 +++--- .../apache/commons/collections4/keyvalue/DefaultKeyValue.java | 5 +++-- .../org/apache/commons/collections4/keyvalue/TiedMapEntry.java | 6 +++--- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d2f5f4f01..be5da9e04 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -45,6 +45,7 @@ <action type="fix" dev="ggregory" due-to="Gary Gregory" issue="COLLECTIONS-815">Javadoc: Update ClosureUtils Javadoc to match runtime.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory" issue="COLLECTIONS-777">Migrate to JUnit 5.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix NullPointerException in FilterIterator.setNextObject().</action> + <action type="fix" dev="ggregory" due-to="Gary Gregory">EqualPredicate.test(Object) should return true if the parameter is the same object as given the constructor.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">LayerManager.Builder implements Supplier.</action> <action type="add" dev="ggregory" due-to="Gary Gregory, hemanth0525">Add CollectionUtils.duplicateList(Collection).</action> diff --git a/src/main/java/org/apache/commons/collections4/bidimap/AbstractDualBidiMap.java b/src/main/java/org/apache/commons/collections4/bidimap/AbstractDualBidiMap.java index 2351dcc5d..3f48ffe3f 100644 --- a/src/main/java/org/apache/commons/collections4/bidimap/AbstractDualBidiMap.java +++ b/src/main/java/org/apache/commons/collections4/bidimap/AbstractDualBidiMap.java @@ -181,7 +181,7 @@ public abstract class AbstractDualBidiMap<K, V> implements BidiMap<K, V> { final Object key = entry.getKey(); if (parent.containsKey(key)) { final V value = parent.normalMap.get(key); - if (value == null ? entry.getValue() == null : value.equals(entry.getValue())) { + if (Objects.equals(value, entry.getValue())) { parent.normalMap.remove(key); parent.reverseMap.remove(value); return true; diff --git a/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java b/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java index 7482c5a97..45d3e6cfa 100644 --- a/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java +++ b/src/main/java/org/apache/commons/collections4/bidimap/TreeBidiMap.java @@ -231,7 +231,7 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>> final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj; final Object value = entry.getValue(); final Node<K, V> node = lookupKey(entry.getKey()); - return node != null && node.getValue().equals(value); + return node != null && Objects.equals(node.getValue(), value); } @Override @@ -247,7 +247,7 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>> final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj; final Object value = entry.getValue(); final Node<K, V> node = lookupKey(entry.getKey()); - if (node != null && node.getValue().equals(value)) { + if (node != null && Objects.equals(node.getValue(), value)) { doRedBlackDelete(node); return true; } @@ -424,7 +424,7 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>> final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj; final Object value = entry.getValue(); final Node<K, V> node = lookupValue(entry.getKey()); - return node != null && node.getKey().equals(value); + return node != null && Objects.equals(node.getKey(), value); } @Override @@ -440,7 +440,7 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>> final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) obj; final Object value = entry.getValue(); final Node<K, V> node = lookupValue(entry.getKey()); - if (node != null && node.getKey().equals(value)) { + if (node != null && Objects.equals(node.getKey(), value)) { doRedBlackDelete(node); return true; } @@ -605,7 +605,7 @@ public class TreeBidiMap<K extends Comparable<K>, V extends Comparable<V>> return false; } final Map.Entry<?, ?> e = (Map.Entry<?, ?>) obj; - return getKey().equals(e.getKey()) && getValue().equals(e.getValue()); + return Objects.equals(getKey(), e.getKey()) && Objects.equals(getValue(), e.getValue()); } private Object getData(final DataElement dataElement) { diff --git a/src/main/java/org/apache/commons/collections4/functors/ConstantTransformer.java b/src/main/java/org/apache/commons/collections4/functors/ConstantTransformer.java index 8db424086..b2362549e 100644 --- a/src/main/java/org/apache/commons/collections4/functors/ConstantTransformer.java +++ b/src/main/java/org/apache/commons/collections4/functors/ConstantTransformer.java @@ -17,6 +17,7 @@ package org.apache.commons.collections4.functors; import java.io.Serializable; +import java.util.Objects; import org.apache.commons.collections4.Transformer; @@ -92,7 +93,7 @@ public class ConstantTransformer<T, R> implements Transformer<T, R>, Serializabl return false; } final Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant(); - return otherConstant == getConstant() || otherConstant != null && otherConstant.equals(getConstant()); + return Objects.equals(otherConstant, getConstant()); } /** diff --git a/src/main/java/org/apache/commons/collections4/keyvalue/AbstractMapEntry.java b/src/main/java/org/apache/commons/collections4/keyvalue/AbstractMapEntry.java index 9442b4863..9ace5dd56 100644 --- a/src/main/java/org/apache/commons/collections4/keyvalue/AbstractMapEntry.java +++ b/src/main/java/org/apache/commons/collections4/keyvalue/AbstractMapEntry.java @@ -17,6 +17,7 @@ package org.apache.commons.collections4.keyvalue; import java.util.Map; +import java.util.Objects; /** * Abstract Pair class to assist with creating correct @@ -55,9 +56,8 @@ public abstract class AbstractMapEntry<K, V> extends AbstractKeyValue<K, V> impl return false; } final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj; - return - (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) && - (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue())); + return Objects.equals(getKey(), other.getKey()) && + Objects.equals(getValue(), other.getValue()); } /** diff --git a/src/main/java/org/apache/commons/collections4/keyvalue/DefaultKeyValue.java b/src/main/java/org/apache/commons/collections4/keyvalue/DefaultKeyValue.java index 030de05ce..fcbf02f1f 100644 --- a/src/main/java/org/apache/commons/collections4/keyvalue/DefaultKeyValue.java +++ b/src/main/java/org/apache/commons/collections4/keyvalue/DefaultKeyValue.java @@ -17,6 +17,7 @@ package org.apache.commons.collections4.keyvalue; import java.util.Map; +import java.util.Objects; import org.apache.commons.collections4.KeyValue; @@ -91,8 +92,8 @@ public class DefaultKeyValue<K, V> extends AbstractKeyValue<K, V> { final DefaultKeyValue<?, ?> other = (DefaultKeyValue<?, ?>) obj; return - (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) && - (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue())); + Objects.equals(getKey(), other.getKey()) && + Objects.equals(getValue(), other.getValue()); } /** diff --git a/src/main/java/org/apache/commons/collections4/keyvalue/TiedMapEntry.java b/src/main/java/org/apache/commons/collections4/keyvalue/TiedMapEntry.java index 0449dfc57..92295bbe1 100644 --- a/src/main/java/org/apache/commons/collections4/keyvalue/TiedMapEntry.java +++ b/src/main/java/org/apache/commons/collections4/keyvalue/TiedMapEntry.java @@ -18,6 +18,7 @@ package org.apache.commons.collections4.keyvalue; import java.io.Serializable; import java.util.Map; +import java.util.Objects; import org.apache.commons.collections4.KeyValue; @@ -71,10 +72,9 @@ public class TiedMapEntry<K, V> implements Map.Entry<K, V>, KeyValue<K, V>, Seri return false; } final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj; - final Object value = getValue(); return - (key == null ? other.getKey() == null : key.equals(other.getKey())) && - (value == null ? other.getValue() == null : value.equals(other.getValue())); + Objects.equals(key, other.getKey()) && + Objects.equals(getValue(), other.getValue()); } /**