From a8acbf049141fb55eb1285ad47b01d71776952d6 Mon Sep 17 00:00:00 2001 From: leriomaggio Date: Sat, 28 Aug 2021 20:46:44 +0100 Subject: [PATCH] Improved CountAll method implementation using Maps --- .../src/main/java/forge/util/ItemPool.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/forge-core/src/main/java/forge/util/ItemPool.java b/forge-core/src/main/java/forge/util/ItemPool.java index 7a065ca1639..8222f61f597 100644 --- a/forge-core/src/main/java/forge/util/ItemPool.java +++ b/forge-core/src/main/java/forge/util/ItemPool.java @@ -31,7 +31,9 @@ import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; import forge.item.InventoryItem; +import org.checkerframework.checker.nullness.compatqual.NullableDecl; /** *

@@ -137,30 +139,26 @@ public class ItemPool implements Iterable int countAll(Predicate condition){ + public int countAll(Predicate condition){ int count = 0; - Iterable matchingKeys = Iterables.filter(this.items.keySet(), new Predicate() { - @Override - public boolean apply(T item) { - return condition.apply((U)item); - } - }); - for (T key : matchingKeys) - count += this.items.get(key); + for (Integer v : Maps.filterKeys(this.items, condition).values()) + count += v; return count; + } @SuppressWarnings("unchecked") public final int countAll(Predicate condition, Class cls) { int count = 0; - Iterable matchingKeys = Iterables.filter(this.items.keySet(), new Predicate() { + Map matchingKeys = Maps.filterKeys(this.items, new Predicate() { @Override public boolean apply(T item) { - return cls.isInstance(item) && condition.apply((U)item); + return cls.isInstance(item) && (condition.apply((U)item)); } }); - for (T key : matchingKeys) - count += this.items.get(key); + for (Integer i : matchingKeys.values()) { + count += i; + } return count; }