Skip to content

Commit

Permalink
Fixed KeyMap#removeIf fails if there are custom keys registered (#1849)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Aug 26, 2023
1 parent b53dc6b commit ec7f59a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Expand Up @@ -172,7 +172,24 @@ public Key getKey(Key original, @Nullable Key def) {

@Override
public boolean removeIf(Predicate<Key> predicate) {
return size() != 0 && entrySet().removeIf(entry -> predicate.test(entry.getKey()));
if (isEmpty())
return false;

boolean removed = false;

{
Map<EntityTypeKey, V> innerMap = this.innerMap.getIfPresent().orElse(null);
if (innerMap != null)
removed |= innerMap.entrySet().removeIf(entry -> predicate.test(entry.getKey()));
}

{
Map<CustomKey, V> customInnerMap = this.customInnerMap.getIfPresent().orElse(null);
if (customInnerMap != null)
removed |= customInnerMap.entrySet().removeIf(entry -> predicate.test(entry.getKey()));
}

return removed;
}

@Override
Expand Down
Expand Up @@ -13,11 +13,9 @@
import javax.annotation.Nullable;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.function.Supplier;

Expand Down Expand Up @@ -193,7 +191,24 @@ public Key getKey(Key original, @Nullable Key def) {

@Override
public boolean removeIf(Predicate<Key> predicate) {
return size() != 0 && entrySet().removeIf(entry -> predicate.test(entry.getKey()));
if (isEmpty())
return false;

boolean removed = false;

{
Map<MaterialKey, V> innerMap = this.innerMap.getIfPresent().orElse(null);
if (innerMap != null)
removed |= innerMap.entrySet().removeIf(entry -> predicate.test(entry.getKey()));
}

{
Map<CustomKey, V> customInnerMap = this.customInnerMap.getIfPresent().orElse(null);
if (customInnerMap != null)
removed |= customInnerMap.entrySet().removeIf(entry -> predicate.test(entry.getKey()));
}

return removed;
}

@Override
Expand Down

0 comments on commit ec7f59a

Please sign in to comment.