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 c14799965 Reuse Objects.equals() in org.apache.commons.collections4.map
c14799965 is described below
commit c147999658b94e48163a0887388f4740d84e388e
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Dec 8 16:15:48 2024 -0500
Reuse Objects.equals() in org.apache.commons.collections4.map
---
.../apache/commons/collections4/map/AbstractHashedMap.java | 9 +++++----
.../commons/collections4/map/AbstractReferenceMap.java | 2 +-
.../collections4/map/ConcurrentReferenceHashMap.java | 13 ++++++-------
.../java/org/apache/commons/collections4/map/Flat3Map.java | 6 ++----
.../org/apache/commons/collections4/map/SingletonMap.java | 5 +++--
.../apache/commons/collections4/map/StaticBucketMap.java | 4 ++--
6 files changed, 19 insertions(+), 20 deletions(-)
diff --git
a/src/main/java/org/apache/commons/collections4/map/AbstractHashedMap.java
b/src/main/java/org/apache/commons/collections4/map/AbstractHashedMap.java
index 1fbcf2e95..d403e5d8f 100644
--- a/src/main/java/org/apache/commons/collections4/map/AbstractHashedMap.java
+++ b/src/main/java/org/apache/commons/collections4/map/AbstractHashedMap.java
@@ -28,6 +28,7 @@ import java.util.ConcurrentModificationException;
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;
@@ -196,8 +197,8 @@ public class AbstractHashedMap<K, V> extends AbstractMap<K,
V> implements Iterab
}
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()));
+ Objects.equals(getKey(), other.getKey()) &&
+ Objects.equals(getValue(), other.getValue());
}
@Override
@@ -1244,7 +1245,7 @@ public class AbstractHashedMap<K, V> extends
AbstractMap<K, V> implements Iterab
* @return true if equal
*/
protected boolean isEqualKey(final Object key1, final Object key2) {
- return key1 == key2 || key1.equals(key2);
+ return Objects.equals(key1, key2);
}
/**
@@ -1257,7 +1258,7 @@ public class AbstractHashedMap<K, V> extends
AbstractMap<K, V> implements Iterab
* @return true if equal
*/
protected boolean isEqualValue(final Object value1, final Object value2) {
- return value1 == value2 || value1.equals(value2);
+ return Objects.equals(value1, value2);
}
/**
diff --git
a/src/main/java/org/apache/commons/collections4/map/AbstractReferenceMap.java
b/src/main/java/org/apache/commons/collections4/map/AbstractReferenceMap.java
index 5b32ad3f1..ec23f6edf 100644
---
a/src/main/java/org/apache/commons/collections4/map/AbstractReferenceMap.java
+++
b/src/main/java/org/apache/commons/collections4/map/AbstractReferenceMap.java
@@ -971,7 +971,7 @@ public abstract class AbstractReferenceMap<K, V> extends
AbstractHashedMap<K, V>
@SuppressWarnings("unchecked")
protected boolean isEqualKey(final Object key1, Object key2) {
key2 = keyType == ReferenceStrength.HARD ? key2 : ((Reference<K>)
key2).get();
- return key1 == key2 || key1.equals(key2);
+ return Objects.equals(key1, key2);
}
/**
diff --git
a/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java
b/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java
index 74f2f4c69..bd47592f8 100644
---
a/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java
+++
b/src/main/java/org/apache/commons/collections4/map/ConcurrentReferenceHashMap.java
@@ -361,9 +361,8 @@ public class ConcurrentReferenceHashMap<K, V> extends
AbstractMap<K, V> implemen
if (!(o instanceof Map.Entry)) {
return false;
}
- final Entry<?, ?> e = (Entry<?, ?>) o;
- final V v = ConcurrentReferenceHashMap.this.get(e.getKey());
- return v != null && v.equals(e.getValue());
+ final V v = ConcurrentReferenceHashMap.this.get(((Entry<?, ?>)
o).getKey());
+ return Objects.equals(v, ((Entry<?, ?>) o).getValue());
}
@Override
@@ -822,7 +821,7 @@ public class ConcurrentReferenceHashMap<K, V> extends
AbstractMap<K, V> implemen
} else {
v = e.dereferenceValue(opaque);
}
- if (value.equals(v)) {
+ if (Objects.equals(value, v)) {
return true;
}
}
@@ -864,7 +863,7 @@ public class ConcurrentReferenceHashMap<K, V> extends
AbstractMap<K, V> implemen
}
private boolean keyEq(final Object src, final Object dest) {
- return identityComparisons ? src == dest : src.equals(dest);
+ return identityComparisons ? src == dest : Objects.equals(src,
dest);
}
HashEntry<K, V> newHashEntry(final K key, final int hash, final
HashEntry<K, V> next, final V value) {
@@ -1093,7 +1092,7 @@ public class ConcurrentReferenceHashMap<K, V> extends
AbstractMap<K, V> implemen
e = e.next;
}
boolean replaced = false;
- if (e != null && oldValue.equals(e.value())) {
+ if (e != null && Objects.equals(oldValue, e.value())) {
replaced = true;
e.setValue(newValue, valueType, refQueue);
}
@@ -1113,7 +1112,7 @@ public class ConcurrentReferenceHashMap<K, V> extends
AbstractMap<K, V> implemen
private static class SimpleEntry<K, V> implements Entry<K, V> {
private static boolean eq(final Object o1, final Object o2) {
- return o1 == null ? o2 == null : o1.equals(o2);
+ return Objects.equals(o1, o2);
}
private final K key;
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 ae4e7748b..552d4ee54 100644
--- a/src/main/java/org/apache/commons/collections4/map/Flat3Map.java
+++ b/src/main/java/org/apache/commons/collections4/map/Flat3Map.java
@@ -194,10 +194,8 @@ public class Flat3Map<K, V> implements IterableMap<K, V>,
Serializable, Cloneabl
return false;
}
final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
- final Object key = getKey();
- final Object value = getValue();
- return (key == null ? other.getKey() == null :
key.equals(other.getKey())) &&
- (value == null ? other.getValue() == null :
value.equals(other.getValue()));
+ return Objects.equals(getKey(), other.getKey()) &&
+ Objects.equals(getValue(), other.getValue());
}
@Override
diff --git
a/src/main/java/org/apache/commons/collections4/map/SingletonMap.java
b/src/main/java/org/apache/commons/collections4/map/SingletonMap.java
index e1a2f169f..67b3196ef 100644
--- a/src/main/java/org/apache/commons/collections4/map/SingletonMap.java
+++ b/src/main/java/org/apache/commons/collections4/map/SingletonMap.java
@@ -23,6 +23,7 @@ import java.util.Collections;
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.BoundedMap;
@@ -399,7 +400,7 @@ public class SingletonMap<K, V>
* @return true if equal
*/
protected boolean isEqualKey(final Object key) {
- return key == null ? getKey() == null : key.equals(getKey());
+ return Objects.equals(key, getKey());
}
/**
@@ -409,7 +410,7 @@ public class SingletonMap<K, V>
* @return true if equal
*/
protected boolean isEqualValue(final Object value) {
- return value == null ? getValue() == null : value.equals(getValue());
+ return Objects.equals(value, getValue());
}
// BoundedMap
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 0c6943a89..1449ff947 100644
--- a/src/main/java/org/apache/commons/collections4/map/StaticBucketMap.java
+++ b/src/main/java/org/apache/commons/collections4/map/StaticBucketMap.java
@@ -283,8 +283,8 @@ public final class StaticBucketMap<K, V> extends
AbstractIterableMap<K, V> {
}
final Map.Entry<?, ?> e2 = (Map.Entry<?, ?>) obj;
- return (key == null ? e2.getKey() == null :
key.equals(e2.getKey())) &&
- (value == null ? e2.getValue() == null :
value.equals(e2.getValue()));
+ return Objects.equals(key, e2.getKey()) &&
+ Objects.equals(value, e2.getValue());
}
@Override