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