diff --git a/src/main/java/forge/gui/deckeditor/SEditorUtil.java b/src/main/java/forge/gui/deckeditor/SEditorUtil.java index b165f857843..b8960e0684c 100644 --- a/src/main/java/forge/gui/deckeditor/SEditorUtil.java +++ b/src/main/java/forge/gui/deckeditor/SEditorUtil.java @@ -93,15 +93,14 @@ public final class SEditorUtil { switch (s) { case TOTAL: view.getStatLabel(s).setText(String.valueOf( - Aggregates.sum(Iterables.filter(items, Predicates.compose(totalPred, items.getFnToPrinted())), items.getFnToCount()))); + Aggregates.sum(Iterables.filter(items, Predicates.compose(totalPred, items.FN_GET_KEY)), items.FN_GET_COUNT))); break; case PACK: view.getStatLabel(s).setText(String.valueOf( - Aggregates.sum(Iterables.filter(items, Predicates.compose(packPred, items.getFnToPrinted())), items.getFnToCount()))); + Aggregates.sum(Iterables.filter(items, Predicates.compose(packPred, items.FN_GET_KEY)), items.FN_GET_COUNT))); break; default: - view.getStatLabel(s).setText(String.valueOf( - Aggregates.sum(Iterables.filter(items, Predicates.compose(s.predicate, items.getFnToCard())), items.getFnToCount()))); + view.getStatLabel(s).setText(String.valueOf(items.countAll(Predicates.compose(s.predicate, CardPrinted.FN_GET_RULES), CardPrinted.class))); } } } diff --git a/src/main/java/forge/gui/deckeditor/controllers/CStatistics.java b/src/main/java/forge/gui/deckeditor/controllers/CStatistics.java index 24971e0bacf..5fd06fca47b 100644 --- a/src/main/java/forge/gui/deckeditor/controllers/CStatistics.java +++ b/src/main/java/forge/gui/deckeditor/controllers/CStatistics.java @@ -6,8 +6,6 @@ import javax.swing.JLabel; import com.google.common.base.Predicate; import com.google.common.base.Predicates; -import com.google.common.collect.Iterables; - import forge.Command; import forge.card.MagicColor; import forge.card.CardRulesPredicates; @@ -21,7 +19,6 @@ import forge.item.CardPrinted; import forge.item.InventoryItem; import forge.item.ItemPool; import forge.item.ItemPoolView; -import forge.util.Aggregates; /** @@ -60,7 +57,7 @@ public enum CStatistics implements ICDoc { } private void setLabelValue(JLabel label, ItemPoolView deck, Predicate predicate, int total) { - int tmp = Aggregates.sum(Iterables.filter(deck, Predicates.compose(predicate, deck.getFnToCard())), deck.getFnToCount()); + int tmp = deck.countAll(Predicates.compose(predicate, CardPrinted.FN_GET_RULES)); label.setText(tmp + " (" + SEditorUtil.calculatePercentage(tmp, total) + "%)"); } diff --git a/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java b/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java index 9d03da3c4ed..905d5ddbb92 100644 --- a/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java +++ b/src/main/java/forge/gui/deckeditor/tables/EditorTableView.java @@ -479,14 +479,14 @@ public final class EditorTableView { } if (useFilter && this.wantUnique) { - Predicate> filterForPool = Predicates.compose(this.filter, this.pool.getFnToPrinted()); - Iterable> cards = Aggregates.uniqueByLast(Iterables.filter(this.pool, filterForPool), this.pool.getFnToCardName()); + Predicate> filterForPool = Predicates.compose(this.filter, this.pool.FN_GET_KEY); + Iterable> cards = Aggregates.uniqueByLast(Iterables.filter(this.pool, filterForPool), this.pool.FN_GET_NAME); this.model.addCards(cards); } else if (useFilter) { - Predicate> pred = Predicates.compose(this.filter, this.pool.getFnToPrinted()); + Predicate> pred = Predicates.compose(this.filter, this.pool.FN_GET_KEY); this.model.addCards(Iterables.filter(this.pool, pred)); } else if (this.wantUnique) { - Iterable> cards = Aggregates.uniqueByLast(this.pool, this.pool.getFnToCardName()); + Iterable> cards = Aggregates.uniqueByLast(this.pool, this.pool.FN_GET_NAME); this.model.addCards(cards); } else if (!useFilter && bForceFilter) { this.model.addCards(this.pool); diff --git a/src/main/java/forge/item/ItemPoolView.java b/src/main/java/forge/item/ItemPoolView.java index 81eefc6d75a..e6bfb03d9bd 100644 --- a/src/main/java/forge/item/ItemPoolView.java +++ b/src/main/java/forge/item/ItemPoolView.java @@ -25,10 +25,9 @@ import java.util.Map; import java.util.Map.Entry; import com.google.common.base.Function; +import com.google.common.base.Predicate; -import forge.card.CardRules; - /** *

* CardPoolView class. @@ -41,18 +40,8 @@ import forge.card.CardRules; */ public class ItemPoolView implements Iterable> { - // Field Accessors for select/aggregate operations with filters. - /** The fn to card. */ - private final transient Function, CardRules> fnToCard = new Function, CardRules>() { - @Override - public CardRules apply(final Entry from) { - final T item = from.getKey(); - return item instanceof CardPrinted ? ((CardPrinted) item).getRules() : null; - } - }; - /** The fn to printed. */ - private final transient Function, T> fnToPrinted = new Function, T>() { + public final transient Function, T> FN_GET_KEY = new Function, T>() { @Override public T apply(final Entry from) { return from.getKey(); @@ -60,7 +49,7 @@ public class ItemPoolView implements Iterable, String> fnToCardName = new Function, String>() { + public final transient Function, String> FN_GET_NAME = new Function, String>() { @Override public String apply(final Entry from) { return from.getKey().getName(); @@ -68,7 +57,7 @@ public class ItemPoolView implements Iterable, Integer> fnToCount = new Function, Integer>() { + public final transient Function, Integer> FN_GET_COUNT = new Function, Integer>() { @Override public Integer apply(final Entry from) { return from.getValue(); @@ -150,15 +139,28 @@ public class ItemPoolView implements Iterable condition) { + return countAll(condition, myClass); + } + + public final int countAll(Predicate condition, Class cls) { int result = 0; if (this.getCards() != null) { - for (final Integer n : this.getCards().values()) { - result += n; + final boolean isSameClass = cls == myClass; + for (final Entry kv : this) { + final T key = kv.getKey(); + @SuppressWarnings("unchecked") + final U castKey = isSameClass || cls.isInstance(key) ? (U)key : null; + if (null == condition || castKey != null && condition.apply(castKey)) + result += kv.getValue(); } } return result; } - + /** * * countDistinct. @@ -255,41 +257,6 @@ public class ItemPoolView implements Iterable, CardRules> getFnToCard() { - return this.fnToCard; - } - - /** - * Gets the fn to card name. - * - * @return the fnToCardName - */ - public Function, String> getFnToCardName() { - return this.fnToCardName; - } - - /** - * Gets the fn to count. - * - * @return the fnToCount - */ - public Function, Integer> getFnToCount() { - return this.fnToCount; - } - - /** - * Gets the fn to printed. - * - * @return the fnToPrinted - */ - public Function, T> getFnToPrinted() { - return this.fnToPrinted; - } /** * To item list string.