Guava migration - ItemPool collector

This commit is contained in:
Jetz
2024-09-16 09:10:21 -04:00
parent ae251372c1
commit 7ab333a60a
2 changed files with 37 additions and 10 deletions

View File

@@ -18,15 +18,11 @@
package forge.util; package forge.util;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate; import java.util.function.*;
import java.util.stream.Collector;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import forge.item.InventoryItem; import forge.item.InventoryItem;
@@ -74,6 +70,37 @@ public class ItemPool<T extends InventoryItem> implements Iterable<Entry<T, Inte
return result; return result;
} }
public static <T extends InventoryItem> Collector<? extends InventoryItem, ?, ItemPool<T>> collector(Class<T> cls) {
return new Collector<InventoryItem, ItemPool<T>, ItemPool<T>>() {
@Override
public Supplier<ItemPool<T>> supplier() {
return () -> new ItemPool<T>(cls);
}
@Override
public BiConsumer<ItemPool<T>, InventoryItem> accumulator() {
return (pool, item) -> {
if (cls.isInstance(item)) pool.add(cls.cast(item), 1);
};
}
@Override
public BinaryOperator<ItemPool<T>> combiner() {
return (first, second) -> {
first.addAll(second);
return first;
};
}
@Override public Function<ItemPool<T>, ItemPool<T>> finisher() {
return Function.identity();
}
@Override public Set<Characteristics> characteristics() {
return EnumSet.of(Characteristics.IDENTITY_FINISH);
}
};
}
protected ItemPool(final Map<T, Integer> items0, final Class<T> cls) { protected ItemPool(final Map<T, Integer> items0, final Class<T> cls) {
if (items0 != null) { if (items0 != null) {
items = items0; items = items0;

View File

@@ -141,10 +141,10 @@ public final class CEditorVariant extends CDeckEditor<Deck> {
*/ */
@Override @Override
public void resetTables() { public void resetTables() {
Iterable<PaperCard> allNT = FModel.getMagicDb().getVariantCards().getAllCards(); ItemPool<PaperCard> allNT = FModel.getMagicDb().getVariantCards().streamAllCards()
allNT = Iterables.filter(allNT, cardPoolCondition); .filter(cardPoolCondition).collect(ItemPool.collector(PaperCard.class));
this.getCatalogManager().setPool(ItemPool.createFrom(allNT, PaperCard.class), true); this.getCatalogManager().setPool(allNT, true);
this.getDeckManager().setPool(this.controller.getModel().getOrCreate(this.sectionMode)); this.getDeckManager().setPool(this.controller.getModel().getOrCreate(this.sectionMode));
} }