mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
Make ItemPool and ItemPoolView more generic
This commit is contained in:
@@ -22,9 +22,9 @@ import java.util.Map.Entry;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* CardPool class.
|
* ItemPool class.
|
||||||
* </p>
|
* </p>
|
||||||
* Represents a list of cards with amount of each
|
* Represents a list of items with amount of each
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* an Object
|
* an Object
|
||||||
@@ -82,54 +82,54 @@ public class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
|
|||||||
* @return a ItemPoolView
|
* @return a ItemPoolView
|
||||||
*/
|
*/
|
||||||
public ItemPoolView<T> getView() {
|
public ItemPoolView<T> getView() {
|
||||||
return new ItemPoolView<T>(Collections.unmodifiableMap(this.getCards()), this.getMyClass());
|
return new ItemPoolView<T>(Collections.unmodifiableMap(this.getItems()), this.getMyClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cards manipulation
|
// Items manipulation
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Add Card.
|
* Add a single item.
|
||||||
*
|
*
|
||||||
* @param card
|
* @param item
|
||||||
* a T
|
* a T
|
||||||
*/
|
*/
|
||||||
public void add(final T card) {
|
public void add(final T item) {
|
||||||
this.add(card, 1);
|
this.add(item, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* add method.
|
* Add multiple items.
|
||||||
*
|
*
|
||||||
* @param card
|
* @param item
|
||||||
* a T
|
* a T
|
||||||
* @param amount
|
* @param amount
|
||||||
* a int
|
* a int
|
||||||
*/
|
*/
|
||||||
public void add(final T card, final int amount) {
|
public void add(final T item, final int amount) {
|
||||||
if (amount <= 0) {
|
if (amount <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.getCards().put(card, Integer.valueOf(this.count(card) + amount));
|
this.getItems().put(item, Integer.valueOf(this.count(item) + amount));
|
||||||
this.setListInSync(false);
|
this.setListInSync(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void put(final T card, final int amount) {
|
private void put(final T item, final int amount) {
|
||||||
this.getCards().put(card, amount);
|
this.getItems().put(item, amount);
|
||||||
this.setListInSync(false);
|
this.setListInSync(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* addAllCards.
|
* addAllFlat.
|
||||||
*
|
*
|
||||||
* @param <U>
|
* @param <U>
|
||||||
* a InventoryItem
|
* a InventoryItem
|
||||||
* @param cards
|
* @param items
|
||||||
* a Iterable<U>
|
* a Iterable<U>
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <U extends InventoryItem> void addAllFlat(final Iterable<U> cards) {
|
public <U extends InventoryItem> void addAllFlat(final Iterable<U> items) {
|
||||||
for (final U cr : cards) {
|
for (final U cr : items) {
|
||||||
if (this.getMyClass().isInstance(cr)) {
|
if (this.getMyClass().isInstance(cr)) {
|
||||||
this.add((T) cr);
|
this.add((T) cr);
|
||||||
}
|
}
|
||||||
@@ -160,31 +160,31 @@ public class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
|
|||||||
*
|
*
|
||||||
* Remove.
|
* Remove.
|
||||||
*
|
*
|
||||||
* @param card
|
* @param item
|
||||||
* a T
|
* a T
|
||||||
*/
|
*/
|
||||||
public boolean remove(final T card) {
|
public boolean remove(final T item) {
|
||||||
return this.remove(card, 1);
|
return this.remove(item, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Remove.
|
* Remove.
|
||||||
*
|
*
|
||||||
* @param card
|
* @param item
|
||||||
* a T
|
* a T
|
||||||
* @param amount
|
* @param amount
|
||||||
* a int
|
* a int
|
||||||
*/
|
*/
|
||||||
public boolean remove(final T card, final int amount) {
|
public boolean remove(final T item, final int amount) {
|
||||||
final int count = this.count(card);
|
final int count = this.count(item);
|
||||||
if ((count == 0) || (amount <= 0)) {
|
if ((count == 0) || (amount <= 0)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (count <= amount) {
|
if (count <= amount) {
|
||||||
this.getCards().remove(card);
|
this.getItems().remove(item);
|
||||||
} else {
|
} else {
|
||||||
this.getCards().put(card, count - amount);
|
this.getItems().put(item, count - amount);
|
||||||
}
|
}
|
||||||
this.setListInSync(false);
|
this.setListInSync(false);
|
||||||
return true;
|
return true;
|
||||||
@@ -221,7 +221,7 @@ public class ItemPool<T extends InventoryItem> extends ItemPoolView<T> {
|
|||||||
* Clear.
|
* Clear.
|
||||||
*/
|
*/
|
||||||
public void clear() {
|
public void clear() {
|
||||||
this.getCards().clear();
|
this.getItems().clear();
|
||||||
this.setListInSync(false);
|
this.setListInSync(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,13 +30,13 @@ import com.google.common.base.Predicate;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* CardPoolView class.
|
* ItemPoolView class.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* an InventoryItem
|
* an InventoryItem
|
||||||
* @author Forge
|
* @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<T extends InventoryItem> implements Iterable<Entry<T, Integer>> {
|
public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T, Integer>> {
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** The fn to card name. */
|
/** The fn to item name. */
|
||||||
public final transient Function<Entry<T, Integer>, String> FN_GET_NAME = new Function<Entry<T, Integer>, String>() {
|
public final transient Function<Entry<T, Integer>, String> FN_GET_NAME = new Function<Entry<T, Integer>, String>() {
|
||||||
@Override
|
@Override
|
||||||
public String apply(final Entry<T, Integer> from) {
|
public String apply(final Entry<T, Integer> from) {
|
||||||
@@ -70,13 +70,13 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ItemPoolView(final Map<T, Integer> inMap, final Class<T> cls) {
|
public ItemPoolView(final Map<T, Integer> inMap, final Class<T> cls) {
|
||||||
this.cards = inMap;
|
this.items = inMap;
|
||||||
this.myClass = cls;
|
this.myClass = cls;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data members
|
// Data members
|
||||||
/** The cards. */
|
/** The items. */
|
||||||
private final Map<T, Integer> cards;
|
private final Map<T, Integer> items;
|
||||||
|
|
||||||
/** The my class. */
|
/** The my class. */
|
||||||
private final Class<T> myClass; // class does not keep this in runtime by
|
private final Class<T> myClass; // class does not keep this in runtime by
|
||||||
@@ -84,10 +84,10 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
|
|
||||||
// same thing as above, it was copied to provide sorting (needed by table
|
// same thing as above, it was copied to provide sorting (needed by table
|
||||||
// views in deck editors)
|
// views in deck editors)
|
||||||
/** The cards list ordered. */
|
/** The items ordered. */
|
||||||
private final transient List<Entry<T, Integer>> cardsListOrdered = new ArrayList<Map.Entry<T, Integer>>();
|
private final transient List<Entry<T, Integer>> itemsOrdered = new ArrayList<Map.Entry<T, Integer>>();
|
||||||
|
|
||||||
/** The is list in sync. */
|
/** Whether list is in sync. */
|
||||||
private transient boolean isListInSync = false;
|
private transient boolean isListInSync = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,38 +97,38 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final Iterator<Entry<T, Integer>> iterator() {
|
public final Iterator<Entry<T, Integer>> iterator() {
|
||||||
return this.getCards().entrySet().iterator();
|
return this.items.entrySet().iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cards read only operations
|
// Items read only operations
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* contains.
|
* contains.
|
||||||
*
|
*
|
||||||
* @param card
|
* @param item
|
||||||
* a T
|
* a T
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public final boolean contains(final T card) {
|
public final boolean contains(final T item) {
|
||||||
if (this.getCards() == null) {
|
if (this.items == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return this.getCards().containsKey(card);
|
return this.items.containsKey(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* count.
|
* count.
|
||||||
*
|
*
|
||||||
* @param card
|
* @param item
|
||||||
* a T
|
* a T
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public final int count(final T card) {
|
public final int count(final T item) {
|
||||||
if (this.getCards() == null) {
|
if (this.items == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
final Integer boxed = this.getCards().get(card);
|
final Integer boxed = this.items.get(item);
|
||||||
return boxed == null ? 0 : boxed.intValue();
|
return boxed == null ? 0 : boxed.intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +148,7 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
|
|
||||||
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 result = 0;
|
int result = 0;
|
||||||
if (this.getCards() != null) {
|
if (this.items != null) {
|
||||||
final boolean isSameClass = cls == myClass;
|
final boolean isSameClass = cls == myClass;
|
||||||
for (final Entry<T, Integer> kv : this) {
|
for (final Entry<T, Integer> kv : this) {
|
||||||
final T key = kv.getKey();
|
final T key = kv.getKey();
|
||||||
@@ -168,7 +168,7 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
public final int countDistinct() {
|
public final int countDistinct() {
|
||||||
return this.getCards().size();
|
return this.items.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -178,7 +178,7 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public final boolean isEmpty() {
|
public final boolean isEmpty() {
|
||||||
return (this.getCards() == null) || this.getCards().isEmpty();
|
return (this.items == null) || this.items.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -191,14 +191,14 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
if (!this.isListInSync()) {
|
if (!this.isListInSync()) {
|
||||||
this.rebuildOrderedList();
|
this.rebuildOrderedList();
|
||||||
}
|
}
|
||||||
return this.cardsListOrdered;
|
return this.itemsOrdered;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rebuildOrderedList() {
|
private void rebuildOrderedList() {
|
||||||
this.cardsListOrdered.clear();
|
this.itemsOrdered.clear();
|
||||||
if (this.getCards() != null) {
|
if (this.items != null) {
|
||||||
for (final Entry<T, Integer> e : this.getCards().entrySet()) {
|
for (final Entry<T, Integer> e : this.items.entrySet()) {
|
||||||
this.cardsListOrdered.add(e);
|
this.itemsOrdered.add(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.setListInSync(true);
|
this.setListInSync(true);
|
||||||
@@ -221,12 +221,12 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the cards.
|
* Gets the items.
|
||||||
*
|
*
|
||||||
* @return the cards
|
* @return the items
|
||||||
*/
|
*/
|
||||||
protected Map<T, Integer> getCards() {
|
protected Map<T, Integer> getItems() {
|
||||||
return this.cards;
|
return this.items;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -257,7 +257,6 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
this.isListInSync = isListInSync0;
|
this.isListInSync = isListInSync0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To item list string.
|
* To item list string.
|
||||||
*
|
*
|
||||||
@@ -265,7 +264,7 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
|
|||||||
*/
|
*/
|
||||||
public Iterable<String> toItemListString() {
|
public Iterable<String> toItemListString() {
|
||||||
final List<String> list = new ArrayList<String>();
|
final List<String> list = new ArrayList<String>();
|
||||||
for (final Entry<T, Integer> e : this.cards.entrySet()) {
|
for (final Entry<T, Integer> e : this.items.entrySet()) {
|
||||||
list.add(String.format("%d x %s", e.getValue(), e.getKey().getName()));
|
list.add(String.format("%d x %s", e.getValue(), e.getKey().getName()));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
|
|||||||
Reference in New Issue
Block a user