Skip to content

Commit

Permalink
Reuse Objects.equals() and only extract a key element once
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 8, 2024
1 parent e39c712 commit 10d1ced
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/main/java/org/apache/commons/collections4/map/MultiKeyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extend
final MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 2 &&
(key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
(key2 == multi.getKey(1) || key2 != null && key2.equals(multi.getKey(1)));
Objects.equals(key1, multi.getKey(0)) &&
Objects.equals(key2, multi.getKey(1));
}

/**
Expand All @@ -478,9 +478,9 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extend
final MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 3 &&
(key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
(key2 == multi.getKey(1) || key2 != null && key2.equals(multi.getKey(1))) &&
(key3 == multi.getKey(2) || key3 != null && key3.equals(multi.getKey(2)));
Objects.equals(key1, multi.getKey(0)) &&
Objects.equals(key2, multi.getKey(1)) &&
Objects.equals(key3, multi.getKey(2));
}

/**
Expand All @@ -498,10 +498,10 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extend
final MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 4 &&
(key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
(key2 == multi.getKey(1) || key2 != null && key2.equals(multi.getKey(1))) &&
(key3 == multi.getKey(2) || key3 != null && key3.equals(multi.getKey(2))) &&
(key4 == multi.getKey(3) || key4 != null && key4.equals(multi.getKey(3)));
Objects.equals(key1, multi.getKey(0)) &&
Objects.equals(key2, multi.getKey(1)) &&
Objects.equals(key3, multi.getKey(2)) &&
Objects.equals(key4, multi.getKey(3));
}

/**
Expand All @@ -520,11 +520,11 @@ protected boolean isEqualKey(final AbstractHashedMap.HashEntry<MultiKey<? extend
final MultiKey<? extends K> multi = entry.getKey();
return
multi.size() == 5 &&
(key1 == multi.getKey(0) || key1 != null && key1.equals(multi.getKey(0))) &&
(key2 == multi.getKey(1) || key2 != null && key2.equals(multi.getKey(1))) &&
(key3 == multi.getKey(2) || key3 != null && key3.equals(multi.getKey(2))) &&
(key4 == multi.getKey(3) || key4 != null && key4.equals(multi.getKey(3))) &&
(key5 == multi.getKey(4) || key5 != null && key5.equals(multi.getKey(4)));
Objects.equals(key1, multi.getKey(0)) &&
Objects.equals(key2, multi.getKey(1)) &&
Objects.equals(key3, multi.getKey(2)) &&
Objects.equals(key4, multi.getKey(3)) &&
Objects.equals(key5, multi.getKey(4));
}

@Override
Expand Down Expand Up @@ -695,7 +695,7 @@ public boolean removeAll(final Object key1) {
while (it.hasNext()) {
final MultiKey<? extends K> multi = it.next();
if (multi.size() >= 1 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0)))) {
Objects.equals(key1, multi.getKey(0))) {
it.remove();
modified = true;
}
Expand All @@ -720,8 +720,8 @@ public boolean removeAll(final Object key1, final Object key2) {
while (it.hasNext()) {
final MultiKey<? extends K> multi = it.next();
if (multi.size() >= 2 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1)))) {
Objects.equals(key1, multi.getKey(0)) &&
Objects.equals(key2, multi.getKey(1))) {
it.remove();
modified = true;
}
Expand All @@ -747,9 +747,9 @@ public boolean removeAll(final Object key1, final Object key2, final Object key3
while (it.hasNext()) {
final MultiKey<? extends K> multi = it.next();
if (multi.size() >= 3 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
(key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2)))) {
Objects.equals(key1, multi.getKey(0)) &&
Objects.equals(key2, multi.getKey(1)) &&
Objects.equals(key3, multi.getKey(2))) {
it.remove();
modified = true;
}
Expand All @@ -776,10 +776,10 @@ public boolean removeAll(final Object key1, final Object key2, final Object key3
while (it.hasNext()) {
final MultiKey<? extends K> multi = it.next();
if (multi.size() >= 4 &&
(key1 == null ? multi.getKey(0) == null : key1.equals(multi.getKey(0))) &&
(key2 == null ? multi.getKey(1) == null : key2.equals(multi.getKey(1))) &&
(key3 == null ? multi.getKey(2) == null : key3.equals(multi.getKey(2))) &&
(key4 == null ? multi.getKey(3) == null : key4.equals(multi.getKey(3)))) {
Objects.equals(key1, multi.getKey(0)) &&
Objects.equals(key2, multi.getKey(1)) &&
Objects.equals(key3, multi.getKey(2)) &&
Objects.equals(key4, multi.getKey(3))) {
it.remove();
modified = true;
}
Expand Down

0 comments on commit 10d1ced

Please sign in to comment.