mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-17 19:28:01 +00:00
Few impros for performance in itempool countAll
This implementation leverages on iterables.filter to filter all keys matching the input condition, which will be iterated for counting.
This commit is contained in:
@@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import forge.item.InventoryItem;
|
||||
|
||||
/**
|
||||
@@ -149,12 +150,14 @@ public class ItemPool<T extends InventoryItem> implements Iterable<Entry<T, Inte
|
||||
@SuppressWarnings("unchecked")
|
||||
public final <U extends InventoryItem> int countAll(Predicate<U> condition, Class<U> cls) {
|
||||
int count = 0;
|
||||
for (Entry<T, Integer> e : this) {
|
||||
T item = e.getKey();
|
||||
if (cls.isInstance(item) && condition.apply((U)item)) {
|
||||
count += e.getValue();
|
||||
Iterable<T> matchingKeys = Iterables.filter(this.items.keySet(), new Predicate<T>() {
|
||||
@Override
|
||||
public boolean apply(T item) {
|
||||
return cls.isInstance(item) && condition.apply((U)item);
|
||||
}
|
||||
}
|
||||
});
|
||||
for (T key : matchingKeys)
|
||||
count += this.items.get(key);
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package forge.itemmanager.filters;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
@@ -76,10 +77,12 @@ public abstract class StatTypeFilter<T extends InventoryItem> extends ToggleButt
|
||||
btnPackOrDeck.setText(String.valueOf(count));
|
||||
}
|
||||
|
||||
for (Map.Entry<StatTypes, FLabel> btn : buttonMap.entrySet()) {
|
||||
if (btn.getKey().predicate != null) {
|
||||
int count = items.countAll(Predicates.compose(btn.getKey().predicate, PaperCard.FN_GET_RULES), PaperCard.class);
|
||||
btn.getValue().setText(String.valueOf(count));
|
||||
Iterator<StatTypes> buttonMapStatsIterator = buttonMap.keySet().iterator();
|
||||
while (buttonMapStatsIterator.hasNext()){
|
||||
StatTypes statTypes = buttonMapStatsIterator.next();
|
||||
if (statTypes.predicate != null){
|
||||
int count = items.countAll(Predicates.compose(statTypes.predicate, PaperCard.FN_GET_RULES), PaperCard.class);
|
||||
buttonMap.get(statTypes).setText(String.valueOf(count));
|
||||
}
|
||||
}
|
||||
getWidget().revalidate();
|
||||
|
||||
Reference in New Issue
Block a user