Skip to content

Commit

Permalink
Reuse Objects.equals() in org.apache.commons.collections4.map
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 8, 2024
1 parent 10d1ced commit c147999
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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;
Expand Down Expand Up @@ -196,8 +197,8 @@ public boolean equals(final Object obj) {
}
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
Expand Down Expand Up @@ -1244,7 +1245,7 @@ public boolean isEmpty() {
* @return true if equal
*/
protected boolean isEqualKey(final Object key1, final Object key2) {
return key1 == key2 || key1.equals(key2);
return Objects.equals(key1, key2);
}

/**
Expand All @@ -1257,7 +1258,7 @@ protected boolean isEqualKey(final Object key1, final Object key2) {
* @return true if equal
*/
protected boolean isEqualValue(final Object value1, final Object value2) {
return value1 == value2 || value1.equals(value2);
return Objects.equals(value1, value2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ public boolean isEmpty() {
@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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,8 @@ public boolean contains(final Object o) {
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
Expand Down Expand Up @@ -822,7 +821,7 @@ boolean containsValue(final Object value) {
} else {
v = e.dereferenceValue(opaque);
}
if (value.equals(v)) {
if (Objects.equals(value, v)) {
return true;
}
}
Expand Down Expand Up @@ -864,7 +863,7 @@ V getValue(final K key, final V value, final Function<? super K, ? extends V> fu
}

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) {
Expand Down Expand Up @@ -1093,7 +1092,7 @@ private boolean replaceInternal2(final K key, final int hash, final V oldValue,
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);
}
Expand All @@ -1113,7 +1112,7 @@ void setTable(final HashEntry<K, V>[] newTable) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ public boolean equals(final Object obj) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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.BoundedMap;
Expand Down Expand Up @@ -399,7 +400,7 @@ public boolean isEmpty() {
* @return true if equal
*/
protected boolean isEqualKey(final Object key) {
return key == null ? getKey() == null : key.equals(getKey());
return Objects.equals(key, getKey());
}

/**
Expand All @@ -409,7 +410,7 @@ protected boolean isEqualKey(final Object key) {
* @return true if equal
*/
protected boolean isEqualValue(final Object value) {
return value == null ? getValue() == null : value.equals(getValue());
return Objects.equals(value, getValue());
}

// BoundedMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ public boolean equals(final Object obj) {
}

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
Expand Down

0 comments on commit c147999

Please sign in to comment.