From c552ef161655fa16cfd6dfd58e777cfd9caf4ad2 Mon Sep 17 00:00:00 2001 From: drdev Date: Sat, 10 Aug 2013 15:56:24 +0000 Subject: [PATCH] Make ItemPool and ItemPoolView more generic --- src/main/java/forge/item/ItemPool.java | 54 +++++++++--------- src/main/java/forge/item/ItemPoolView.java | 65 +++++++++++----------- 2 files changed, 59 insertions(+), 60 deletions(-) diff --git a/src/main/java/forge/item/ItemPool.java b/src/main/java/forge/item/ItemPool.java index f03aa39a23e..b6fa215a32a 100644 --- a/src/main/java/forge/item/ItemPool.java +++ b/src/main/java/forge/item/ItemPool.java @@ -22,9 +22,9 @@ import java.util.Map.Entry; /** *

- * CardPool class. + * ItemPool class. *

- * Represents a list of cards with amount of each + * Represents a list of items with amount of each * * @param * an Object @@ -82,54 +82,54 @@ public class ItemPool extends ItemPoolView { * @return a ItemPoolView */ public ItemPoolView getView() { - return new ItemPoolView(Collections.unmodifiableMap(this.getCards()), this.getMyClass()); + return new ItemPoolView(Collections.unmodifiableMap(this.getItems()), this.getMyClass()); } - // Cards manipulation + // Items manipulation /** * - * Add Card. + * Add a single item. * - * @param card + * @param item * a T */ - public void add(final T card) { - this.add(card, 1); + public void add(final T item) { + this.add(item, 1); } /** * - * add method. + * Add multiple items. * - * @param card + * @param item * a T * @param amount * a int */ - public void add(final T card, final int amount) { + public void add(final T item, final int amount) { if (amount <= 0) { return; } - this.getCards().put(card, Integer.valueOf(this.count(card) + amount)); + this.getItems().put(item, Integer.valueOf(this.count(item) + amount)); this.setListInSync(false); } - private void put(final T card, final int amount) { - this.getCards().put(card, amount); + private void put(final T item, final int amount) { + this.getItems().put(item, amount); this.setListInSync(false); } /** - * addAllCards. + * addAllFlat. * * @param * a InventoryItem - * @param cards + * @param items * a Iterable */ @SuppressWarnings("unchecked") - public void addAllFlat(final Iterable cards) { - for (final U cr : cards) { + public void addAllFlat(final Iterable items) { + for (final U cr : items) { if (this.getMyClass().isInstance(cr)) { this.add((T) cr); } @@ -160,31 +160,31 @@ public class ItemPool extends ItemPoolView { * * Remove. * - * @param card + * @param item * a T */ - public boolean remove(final T card) { - return this.remove(card, 1); + public boolean remove(final T item) { + return this.remove(item, 1); } /** * * Remove. * - * @param card + * @param item * a T * @param amount * a int */ - public boolean remove(final T card, final int amount) { - final int count = this.count(card); + public boolean remove(final T item, final int amount) { + final int count = this.count(item); if ((count == 0) || (amount <= 0)) { return false; } if (count <= amount) { - this.getCards().remove(card); + this.getItems().remove(item); } else { - this.getCards().put(card, count - amount); + this.getItems().put(item, count - amount); } this.setListInSync(false); return true; @@ -221,7 +221,7 @@ public class ItemPool extends ItemPoolView { * Clear. */ public void clear() { - this.getCards().clear(); + this.getItems().clear(); this.setListInSync(false); } } diff --git a/src/main/java/forge/item/ItemPoolView.java b/src/main/java/forge/item/ItemPoolView.java index e6bfb03d9bd..153f2da1b2c 100644 --- a/src/main/java/forge/item/ItemPoolView.java +++ b/src/main/java/forge/item/ItemPoolView.java @@ -30,13 +30,13 @@ import com.google.common.base.Predicate; /** *

- * CardPoolView class. + * ItemPoolView class. *

* * @param * an InventoryItem * @author Forge - * @version $Id: CardPoolView.java 9708 2011-08-09 19:34:12Z jendave $ + * @version $Id: ItemPoolView.java 9708 2011-08-09 19:34:12Z jendave $ */ public class ItemPoolView implements Iterable> { @@ -48,7 +48,7 @@ public class ItemPoolView implements Iterable, String> FN_GET_NAME = new Function, String>() { @Override public String apply(final Entry from) { @@ -70,13 +70,13 @@ public class ItemPoolView implements Iterable inMap, final Class cls) { - this.cards = inMap; + this.items = inMap; this.myClass = cls; } // Data members - /** The cards. */ - private final Map cards; + /** The items. */ + private final Map items; /** The my class. */ private final Class myClass; // class does not keep this in runtime by @@ -84,10 +84,10 @@ public class ItemPoolView implements Iterable> cardsListOrdered = new ArrayList>(); + /** The items ordered. */ + private final transient List> itemsOrdered = new ArrayList>(); - /** The is list in sync. */ + /** Whether list is in sync. */ private transient boolean isListInSync = false; /** @@ -97,38 +97,38 @@ public class ItemPoolView implements Iterable> iterator() { - return this.getCards().entrySet().iterator(); + return this.items.entrySet().iterator(); } - // Cards read only operations + // Items read only operations /** * * contains. * - * @param card + * @param item * a T * @return boolean */ - public final boolean contains(final T card) { - if (this.getCards() == null) { + public final boolean contains(final T item) { + if (this.items == null) { return false; } - return this.getCards().containsKey(card); + return this.items.containsKey(item); } /** * * count. * - * @param card + * @param item * a T * @return int */ - public final int count(final T card) { - if (this.getCards() == null) { + public final int count(final T item) { + if (this.items == null) { return 0; } - final Integer boxed = this.getCards().get(card); + final Integer boxed = this.items.get(item); return boxed == null ? 0 : boxed.intValue(); } @@ -148,7 +148,7 @@ public class ItemPoolView implements Iterable int countAll(Predicate condition, Class cls) { int result = 0; - if (this.getCards() != null) { + if (this.items != null) { final boolean isSameClass = cls == myClass; for (final Entry kv : this) { final T key = kv.getKey(); @@ -168,7 +168,7 @@ public class ItemPoolView implements Iterable implements Iterable implements Iterable e : this.getCards().entrySet()) { - this.cardsListOrdered.add(e); + this.itemsOrdered.clear(); + if (this.items != null) { + for (final Entry e : this.items.entrySet()) { + this.itemsOrdered.add(e); } } this.setListInSync(true); @@ -221,12 +221,12 @@ public class ItemPoolView implements Iterable getCards() { - return this.cards; + protected Map getItems() { + return this.items; } /** @@ -257,7 +257,6 @@ public class ItemPoolView implements Iterable implements Iterable toItemListString() { final List list = new ArrayList(); - for (final Entry e : this.cards.entrySet()) { + for (final Entry e : this.items.entrySet()) { list.add(String.format("%d x %s", e.getValue(), e.getKey().getName())); } return list;