ItemPoolView - final Functions are made public, useless getters removed

ItemPoolView - countAll() now accepts predicates, this allowed to drop 'fnToCard' field as well as reference to forge.card.*
This commit is contained in:
Maxmtg
2013-02-18 09:02:07 +00:00
parent 897be76bef
commit 81185964c7
4 changed files with 28 additions and 65 deletions

View File

@@ -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)));
}
}
}

View File

@@ -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<CardPrinted> deck, Predicate<CardRules> 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) + "%)");
}

View File

@@ -479,14 +479,14 @@ public final class EditorTableView<T extends InventoryItem> {
}
if (useFilter && this.wantUnique) {
Predicate<Entry<T, Integer>> filterForPool = Predicates.compose(this.filter, this.pool.getFnToPrinted());
Iterable<Entry<T, Integer>> cards = Aggregates.uniqueByLast(Iterables.filter(this.pool, filterForPool), this.pool.getFnToCardName());
Predicate<Entry<T, Integer>> filterForPool = Predicates.compose(this.filter, this.pool.FN_GET_KEY);
Iterable<Entry<T, Integer>> cards = Aggregates.uniqueByLast(Iterables.filter(this.pool, filterForPool), this.pool.FN_GET_NAME);
this.model.addCards(cards);
} else if (useFilter) {
Predicate<Entry<T, Integer>> pred = Predicates.compose(this.filter, this.pool.getFnToPrinted());
Predicate<Entry<T, Integer>> pred = Predicates.compose(this.filter, this.pool.FN_GET_KEY);
this.model.addCards(Iterables.filter(this.pool, pred));
} else if (this.wantUnique) {
Iterable<Entry<T, Integer>> cards = Aggregates.uniqueByLast(this.pool, this.pool.getFnToCardName());
Iterable<Entry<T, Integer>> cards = Aggregates.uniqueByLast(this.pool, this.pool.FN_GET_NAME);
this.model.addCards(cards);
} else if (!useFilter && bForceFilter) {
this.model.addCards(this.pool);

View File

@@ -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;
/**
* <p>
* CardPoolView class.
@@ -41,18 +40,8 @@ import forge.card.CardRules;
*/
public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T, Integer>> {
// Field Accessors for select/aggregate operations with filters.
/** The fn to card. */
private final transient Function<Entry<T, Integer>, CardRules> fnToCard = new Function<Entry<T, Integer>, CardRules>() {
@Override
public CardRules apply(final Entry<T, Integer> from) {
final T item = from.getKey();
return item instanceof CardPrinted ? ((CardPrinted) item).getRules() : null;
}
};
/** The fn to printed. */
private final transient Function<Entry<T, Integer>, T> fnToPrinted = new Function<Entry<T, Integer>, T>() {
public final transient Function<Entry<T, Integer>, T> FN_GET_KEY = new Function<Entry<T, Integer>, T>() {
@Override
public T apply(final Entry<T, Integer> from) {
return from.getKey();
@@ -60,7 +49,7 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
};
/** The fn to card name. */
private final transient Function<Entry<T, Integer>, String> fnToCardName = new Function<Entry<T, Integer>, String>() {
public final transient Function<Entry<T, Integer>, String> FN_GET_NAME = new Function<Entry<T, Integer>, String>() {
@Override
public String apply(final Entry<T, Integer> from) {
return from.getKey().getName();
@@ -68,7 +57,7 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
};
/** The fn to count. */
private final transient Function<Entry<T, Integer>, Integer> fnToCount = new Function<Entry<T, Integer>, Integer>() {
public final transient Function<Entry<T, Integer>, Integer> FN_GET_COUNT = new Function<Entry<T, Integer>, Integer>() {
@Override
public Integer apply(final Entry<T, Integer> from) {
return from.getValue();
@@ -150,10 +139,23 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
* @return int
*/
public final int countAll() {
return countAll(null, myClass);
}
public final int countAll(Predicate<T> condition) {
return countAll(condition, myClass);
}
public final <U extends InventoryItem> int countAll(Predicate<U> condition, Class<U> 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<T, Integer> 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;
@@ -255,41 +257,6 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
this.isListInSync = isListInSync0;
}
/**
* Gets the fn to card.
*
* @return the fnToCard
*/
public Function<Entry<T, Integer>, CardRules> getFnToCard() {
return this.fnToCard;
}
/**
* Gets the fn to card name.
*
* @return the fnToCardName
*/
public Function<Entry<T, Integer>, String> getFnToCardName() {
return this.fnToCardName;
}
/**
* Gets the fn to count.
*
* @return the fnToCount
*/
public Function<Entry<T, Integer>, Integer> getFnToCount() {
return this.fnToCount;
}
/**
* Gets the fn to printed.
*
* @return the fnToPrinted
*/
public Function<Entry<T, Integer>, T> getFnToPrinted() {
return this.fnToPrinted;
}
/**
* To item list string.