checkstyle

This commit is contained in:
jendave
2011-10-26 08:44:16 +00:00
parent 21cdab9414
commit 7836c89dbe
8 changed files with 692 additions and 223 deletions

View File

@@ -4,57 +4,96 @@ import java.util.List;
import net.slightlymagic.braids.util.lambda.Lambda1;
import net.slightlymagic.maxmtg.Predicate;
import forge.SetUtils;
import forge.card.BoosterGenerator;
import forge.card.CardRules;
import forge.card.CardSet;
/**
* TODO: Write javadoc for this type.
*
/**
* TODO Write javadoc for this type.
*
*/
public class BoosterPack implements InventoryItemFromSet {
public final static Lambda1<BoosterPack, CardSet> fnFromSet = new Lambda1<BoosterPack, CardSet>() {
@Override public BoosterPack apply(CardSet arg1) { return new BoosterPack(arg1); } };
/** The Constant fnFromSet. */
public static final Lambda1<BoosterPack, CardSet> fnFromSet = new Lambda1<BoosterPack, CardSet>() {
@Override
public BoosterPack apply(final CardSet arg1) {
return new BoosterPack(arg1);
}
};
private final CardSet cardSet;
private final String name;
private List<CardPrinted> cards = null;
public BoosterPack(String set) {
/**
* Instantiates a new booster pack.
*
* @param set the set
*/
public BoosterPack(final String set) {
this(SetUtils.getSetByCodeOrThrow(set));
}
public BoosterPack(CardSet set) {
/**
* Instantiates a new booster pack.
*
* @param set the set
*/
public BoosterPack(final CardSet set) {
cardSet = set;
name = cardSet.getName() + " Booster Pack";
}
@Override public String getSet() { return cardSet.getCode(); }
@Override public String getName() { return name; }
@Override public String getImageFilename() {
return "booster/"+cardSet.getCode()+".png";
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getSet()
*/
/**
* @return String
*/
@Override
public final String getSet() {
return cardSet.getCode();
}
private CardPrinted getRandomBasicLand(CardSet set) {
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getName()
*/
/**
* @return String
*/
@Override
public final String getName() {
return name;
}
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getImageFilename()
*/
/**
* @return String
*/
@Override
public final String getImageFilename() {
return "booster/" + cardSet.getCode() + ".png";
}
private CardPrinted getRandomBasicLand(final CardSet set) {
return Predicate.and(CardPrinted.Predicates.printedInSets(set.getCode()),
CardRules.Predicates.Presets.isBasicLand,
CardPrinted.fnGetRules).random(CardDb.instance().getAllCards());
CardRules.Predicates.Presets.isBasicLand, CardPrinted.fnGetRules).random(
CardDb.instance().getAllCards());
}
private CardPrinted getLandFromNearestSet()
{
private CardPrinted getLandFromNearestSet() {
List<CardSet> sets = SetUtils.getAllSets();
int iThisSet = sets.indexOf(cardSet);
for (int iSet = iThisSet; iSet < sets.size(); iSet++)
{
for (int iSet = iThisSet; iSet < sets.size(); iSet++) {
CardPrinted land = getRandomBasicLand(sets.get(iSet));
if (null != land) return land;
if (null != land) {
return land;
}
}
// if not found (though that's impossible)
return getRandomBasicLand(SetUtils.getSetByCode("M12"));
@@ -63,48 +102,85 @@ public class BoosterPack implements InventoryItemFromSet {
private void generate() {
BoosterGenerator gen = new BoosterGenerator(cardSet);
cards = gen.getBoosterPack();
int cntLands = cardSet.getBoosterData().getLand();
if (cntLands > 0) {
cards.add(getLandFromNearestSet());
}
}
public List<CardPrinted> getCards() {
if (null == cards) { generate(); }
/**
* Gets the cards.
*
* @return the cards
*/
public final List<CardPrinted> getCards() {
if (null == cards) {
generate();
}
return cards;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
/**
* @return int
*/
@Override
public int hashCode() {
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cardSet == null) ? 0 : cardSet.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
public final boolean equals(final Object obj) {
if (this == obj) {
return true;
if (obj == null)
}
if (obj == null) {
return false;
if (getClass() != obj.getClass())
}
if (getClass() != obj.getClass()) {
return false;
}
BoosterPack other = (BoosterPack) obj;
if (cardSet == null) {
if (other.cardSet != null)
if (other.cardSet != null) {
return false;
} else if (!cardSet.equals(other.cardSet))
}
} else if (!cardSet.equals(other.cardSet)) {
return false;
}
return true;
}
@Override public String getType() { return "Booster Pack"; }
@Override public Object clone() {
return new BoosterPack(cardSet); // it's ok to share a reference to cardSet which is static anyway
/* (non-Javadoc)
* @see forge.item.InventoryItem#getType()
*/
/**
* @return String
*/
@Override
public final String getType() {
return "Booster Pack";
}
/* (non-Javadoc)
* @see java.lang.Object#clone()
*/
/**
* @return Object
*/
@Override
public final Object clone() {
return new BoosterPack(cardSet); // it's ok to share a reference to
// cardSet which is static anyway
}
}

View File

@@ -5,87 +5,126 @@ import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import net.slightlymagic.braids.util.lambda.Lambda1;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import forge.Card;
import forge.card.CardInSet;
import forge.card.CardRules;
import forge.card.MtgDataParser;
/**
* <p>CardDb class.</p>
*
* <p>
* CardDb class.
* </p>
*
* @author Forge
* @version $Id: CardDb.java 9708 2011-08-09 19:34:12Z jendave $
*/
public final class CardDb {
private static volatile CardDb onlyInstance = null; // 'volatile' keyword makes this working
public static CardDb instance() {
private static volatile CardDb onlyInstance = null; // 'volatile' keyword
// makes this working
/**
* Instance.
*
* @return the card db
*/
public static CardDb instance() {
if (onlyInstance == null) {
throw new NullPointerException("CardDb has not yet been initialized, run setup() first");
}
return onlyInstance;
}
/**
* Sets the up.
*
* @param list the new up
*/
public static void setup(final Iterator<CardRules> list) {
if (onlyInstance != null) {
throw new RuntimeException("CardDb has already been initialized, don't do it twice please");
}
synchronized (CardDb.class) {
if (onlyInstance == null) { // It's broken under 1.4 and below, on 1.5+ works again!
if (onlyInstance == null) { // It's broken under 1.4 and below, on
// 1.5+ works again!
onlyInstance = new CardDb(list);
}
}
}
// Here oracle cards
//private final Map<String, CardRules> cards = new Hashtable<String, CardRules>();
// private final Map<String, CardRules> cards = new Hashtable<String,
// CardRules>();
// Here are refs, get them by name
private final Map<String, CardPrinted> uniqueCards = new Hashtable<String, CardPrinted>();
// need this to obtain cardReference by name+set+artindex
private final Map<String, Map<String, CardPrinted[]>> allCardsBySet = new Hashtable<String, Map<String, CardPrinted[]>>();
private final Map<String, Map<String, CardPrinted[]>> allCardsBySet =
new Hashtable<String, Map<String, CardPrinted[]>>();
// this is the same list in flat storage
private final List<CardPrinted> allCardsFlat = new ArrayList<CardPrinted>();
// Lambda to get rules for selects from list of printed cards
public static final Lambda1<CardPrinted, Card> fnGetCardPrintedByForgeCard = new Lambda1<CardPrinted, Card>() {
@Override public CardPrinted apply(final Card from) { return CardDb.instance().getCard(from.getName()); }
/** The Constant fnGetCardPrintedByForgeCard. */
public static final Lambda1<CardPrinted, Card> fnGetCardPrintedByForgeCard =
new Lambda1<CardPrinted, Card>() {
@Override
public CardPrinted apply(final Card from) {
return CardDb.instance().getCard(from.getName());
}
};
private CardDb() {
this(new MtgDataParser()); // I wish cardname.txt parser was be here.
this(new MtgDataParser()); // I wish cardname.txt parser was be here.
}
private CardDb(final Iterator<CardRules> parser) {
while (parser.hasNext()) {
addNewCard(parser.next());
}
// TODO: consider using Collections.unmodifiableList wherever possible
// TODO consider using Collections.unmodifiableList wherever possible
}
/**
* Adds the new card.
*
* @param card the card
*/
public void addNewCard(final CardRules card) {
if (null == card) { return; } // consider that a success
//System.out.println(card.getName());
if (null == card) {
return;
} // consider that a success
// System.out.println(card.getName());
String cardName = card.getName().toLowerCase();
// 1. register among oracle uniques
//cards.put(cardName, card);
// cards.put(cardName, card);
// 2. Save refs into two lists: one flat and other keyed with sets & name
// 2. Save refs into two lists: one flat and other keyed with sets &
// name
CardPrinted lastAdded = null;
for (Entry<String, CardInSet> s : card.getSetsPrinted()) {
lastAdded = addToLists(card, cardName,s);
lastAdded = addToLists(card, cardName, s);
}
uniqueCards.put(cardName, lastAdded);
uniqueCards.put(cardName, lastAdded);
}
/**
* Adds the to lists.
*
* @param card the card
* @param cardName the card name
* @param s the s
* @return the card printed
*/
public CardPrinted addToLists(final CardRules card, final String cardName, final Entry<String, CardInSet> s) {
CardPrinted lastAdded = null;
String set = s.getKey();
@@ -101,32 +140,43 @@ public final class CardDb {
CardPrinted[] cardCopies = new CardPrinted[count];
setMap.put(cardName, cardCopies);
for (int i = 0; i < count; i++) {
lastAdded = CardPrinted.build(card, set, s.getValue().getRarity(), i, card.isAltState(),card.isDoubleFaced());
lastAdded = CardPrinted.build(card, set, s.getValue().getRarity(), i, card.isAltState(),
card.isDoubleFaced());
allCardsFlat.add(lastAdded);
cardCopies[i] = lastAdded;
}
return lastAdded;
}
public boolean isCardSupported(final String cardName) {
/**
* Checks if is card supported.
*
* @param cardName the card name
* @return true, if is card supported
*/
public boolean isCardSupported(final String cardName) {
ImmutablePair<String, String> nameWithSet = splitCardName(cardName);
if ( nameWithSet.right == null ) { return uniqueCards.containsKey(nameWithSet.left.toLowerCase()); }
if (nameWithSet.right == null) {
return uniqueCards.containsKey(nameWithSet.left.toLowerCase());
}
// Set exists?
Map<String, CardPrinted[]> cardsFromset = allCardsBySet.get(nameWithSet.right.toUpperCase());
if (cardsFromset == null) return false;
if (cardsFromset == null) {
return false;
}
// Card exists?
CardPrinted[] cardCopies = cardsFromset.get(nameWithSet.left.toLowerCase());
return cardCopies != null && cardCopies.length > 0;
}
/**
* Splits cardname into Name and set whenever deck line reads as name|set
* Splits cardname into Name and set whenever deck line reads as name|set.
*/
private static ImmutablePair<String, String> splitCardName(final String name) {
String cardName = name; // .trim() ?
int pipePos = cardName.indexOf('|');
if (pipePos >= 0) {
String setName = cardName.substring(pipePos + 1).trim();
cardName = cardName.substring(0, pipePos);
@@ -139,17 +189,47 @@ public final class CardDb {
}
// Single fetch
/**
* Gets the card.
*
* @param name the name
* @return the card
*/
public CardPrinted getCard(final String name) {
// Sometimes they read from decks things like "CardName|Set" - but we can handle it
// Sometimes they read from decks things like "CardName|Set" - but we
// can handle it
ImmutablePair<String, String> nameWithSet = splitCardName(name);
if (nameWithSet.right != null) { return getCard(nameWithSet.left, nameWithSet.right); }
if (nameWithSet.right != null) {
return getCard(nameWithSet.left, nameWithSet.right);
}
// OK, plain name here
CardPrinted card = uniqueCards.get(nameWithSet.left.toLowerCase());
if (card != null) { return card; }
if (card != null) {
return card;
}
throw new NoSuchElementException(String.format("Card '%s' not found in our database.", name));
}
// Advanced fetch by name+set
public CardPrinted getCard(final String name, final String set) { return getCard(name, set, 0); }
/**
* Gets the card.
*
* @param name the name
* @param set the set
* @return the card
*/
public CardPrinted getCard(final String name, final String set) {
return getCard(name, set, 0);
}
/**
* Gets the card.
*
* @param name the name
* @param set the set
* @param artIndex the art index
* @return the card
*/
public CardPrinted getCard(final String name, final String set, final int artIndex) {
// 1. get set
Map<String, CardPrinted[]> cardsFromset = allCardsBySet.get(set.toUpperCase());
@@ -164,29 +244,64 @@ public final class CardDb {
throw new NoSuchElementException(err);
}
// 3. Get the proper copy
if (artIndex >= 0 && artIndex <= cardCopies.length) { return cardCopies[artIndex]; }
if (artIndex >= 0 && artIndex <= cardCopies.length) {
return cardCopies[artIndex];
}
String err = String.format("Asked for '%s' from '%s' #%d: db didn't find that copy.", name, set, artIndex);
throw new NoSuchElementException(err);
}
// Fetch from Forge's Card instance. Well, there should be no errors, but we'll still check
// Fetch from Forge's Card instance. Well, there should be no errors, but
// we'll still check
/**
* Gets the card.
*
* @param forgeCard the forge card
* @return the card
*/
public CardPrinted getCard(final Card forgeCard) {
String name = forgeCard.getName();
String set = forgeCard.getCurSetCode();
if (StringUtils.isNotBlank(set)) { return getCard(name, set); }
if (StringUtils.isNotBlank(set)) {
return getCard(name, set);
}
return getCard(name);
}
// Multiple fetch
/**
* Gets the cards.
*
* @param names the names
* @return the cards
*/
public List<CardPrinted> getCards(final Iterable<String> names) {
List<CardPrinted> result = new ArrayList<CardPrinted>();
for (String name : names) { result.add(getCard(name)); }
for (String name : names) {
result.add(getCard(name));
}
return result;
}
// returns a list of all cards from their respective latest editions
public Iterable<CardPrinted> getAllUniqueCards() { return uniqueCards.values(); }
//public Iterable<CardRules> getAllCardRules() { return cards.values(); } // still not needed
public Iterable<CardPrinted> getAllCards() { return allCardsFlat; }
/**
* Gets the all unique cards.
*
* @return the all unique cards
*/
public Iterable<CardPrinted> getAllUniqueCards() {
return uniqueCards.values();
}
// public Iterable<CardRules> getAllCardRules() { return cards.values(); }
// // still not needed
/**
* Gets the all cards.
*
* @return the all cards
*/
public Iterable<CardPrinted> getAllCards() {
return allCardsFlat;
}
}

View File

@@ -3,11 +3,12 @@ package forge.item;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import net.slightlymagic.braids.util.lambda.Lambda1;
import net.slightlymagic.maxmtg.Predicate;
import net.slightlymagic.maxmtg.PredicateString;
import org.apache.commons.lang3.ArrayUtils;
import forge.AllZone;
import forge.Card;
import forge.CardUtil;
@@ -15,8 +16,10 @@ import forge.card.CardRarity;
import forge.card.CardRules;
/**
* <p>CardReference class.</p>
*
* <p>
* CardReference class.
* </p>
*
* @author Forge
* @version $Id: CardReference.java 9708 2011-08-09 19:34:12Z jendave $
*/
@@ -33,35 +36,108 @@ public final class CardPrinted implements Comparable<CardPrinted>, InventoryItem
private final boolean isDoubleFaced;
// Calculated fields are below:
private final transient CardRarity rarity; // rarity is given in ctor when set is assigned
private final transient CardRarity rarity; // rarity is given in ctor when
// set is assigned
// need this to be sure that different cased names won't break the system (and create uniqie cardref entries)
// need this to be sure that different cased names won't break the system
// (and create uniqie cardref entries)
private final transient String nameLcase;
// image filename is calculated only after someone request it
private transient String imageFilename = null;
// field RO accessors
public String getName() { return name; }
public String getSet() { return cardSet; }
public int getArtIndex() { return artIndex; }
public boolean isFoil() { return foiled; }
public CardRules getCard() { return card; }
public CardRarity getRarity() { return rarity; }
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getName()
*/
/**
* @return String
*/
public String getName() {
return name;
}
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getSet()
*/
/**
* @return String
*/
public String getSet() {
return cardSet;
}
/**
* Gets the art index.
*
* @return the art index
*/
public int getArtIndex() {
return artIndex;
}
/**
* Checks if is foil.
*
* @return true, if is foil
*/
public boolean isFoil() {
return foiled;
}
/**
* Gets the card.
*
* @return the card
*/
public CardRules getCard() {
return card;
}
/**
* Gets the rarity.
*
* @return the rarity
*/
public CardRarity getRarity() {
return rarity;
}
/* (non-Javadoc)
* @see forge.item.InventoryItemFromSet#getImageFilename()
*/
/**
* @return String
*/
public String getImageFilename() {
if (imageFilename == null) { imageFilename = CardUtil.buildFilename(this); }
if (imageFilename == null) {
imageFilename = CardUtil.buildFilename(this);
}
return imageFilename;
}
public String getType() { return card.getType().toString(); }
/* (non-Javadoc)
* @see forge.item.InventoryItem#getType()
*/
/**
* @return String
*/
public String getType() {
return card.getType().toString();
}
// Lambda to get rules for selects from list of printed cards
/** The Constant fnGetRules. */
public static final Lambda1<CardRules, CardPrinted> fnGetRules = new Lambda1<CardRules, CardPrinted>() {
@Override public CardRules apply(final CardPrinted from) { return from.card; }
@Override
public CardRules apply(final CardPrinted from) {
return from.card;
}
};
// Constructor is private. All non-foiled instances are stored in CardDb
private CardPrinted(final CardRules c, final String set, final CardRarity rare,
final int index, final boolean foil, final boolean isAlt, final boolean isDF)
private CardPrinted(final CardRules c, final String set, final CardRarity rare, final int index,
final boolean foil, final boolean isAlt, final boolean isDF)
{
card = c;
name = c.getName();
@@ -75,51 +151,98 @@ public final class CardPrinted implements Comparable<CardPrinted>, InventoryItem
}
/* package visibility */
static CardPrinted build(final CardRules c, final String set, final CardRarity rare, final int index, final boolean isAlt, final boolean isDF) {
/**
* Builds the.
*
* @param c the c
* @param set the set
* @param rare the rare
* @param index the index
* @param isAlt the is alt
* @param isDF the is df
* @return the card printed
*/
static CardPrinted build(final CardRules c, final String set, final CardRarity rare, final int index,
final boolean isAlt, final boolean isDF)
{
return new CardPrinted(c, set, rare, index, false, isAlt, isDF);
}
/* foiled don't need to stay in CardDb's structures, so u'r free to create */
/**
* Make foiled.
*
* @param c the c
* @return the card printed
*/
public static CardPrinted makeFoiled(final CardPrinted c) {
return new CardPrinted(c.card, c.cardSet, c.rarity, c.artIndex, true, c.isAlternate, c.isDoubleFaced);
}
// Want this class to be a key for HashTable
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
if (this == obj) { return true; }
if (obj == null) { return false; }
if (getClass() != obj.getClass()) { return false; }
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
CardPrinted other = (CardPrinted) obj;
if (!name.equals(other.name)) { return false; }
if (!cardSet.equals(other.cardSet)) { return false; }
if (other.foiled != this.foiled || other.artIndex != this.artIndex) { return false; }
if (!name.equals(other.name)) {
return false;
}
if (!cardSet.equals(other.cardSet)) {
return false;
}
if (other.foiled != this.foiled || other.artIndex != this.artIndex) {
return false;
}
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int code = nameLcase.hashCode() * 11 + cardSet.hashCode() * 59 + artIndex * 2;
if (foiled) { return code + 1; }
if (foiled) {
return code + 1;
}
return code;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return name;
// cannot still decide, if this "name|set" format is needed anymore
//return String.format("%s|%s", name, cardSet);
// return String.format("%s|%s", name, cardSet);
}
/**
* To forge card.
*
* @return the card
*/
public Card toForgeCard() {
Card c = AllZone.getCardFactory().getCard(name, null);
if (c != null) {
c.setCurSetCode(getSet());
c.setRandomPicture(artIndex+1);
c.setRandomPicture(artIndex + 1);
c.setImageFilename(getImageFilename());
if(c.hasAlternateState()) {
if (c.hasAlternateState()) {
c.changeState();
c.setImageFilename(CardUtil.buildFilename(c));
c.changeState();
@@ -129,53 +252,111 @@ public final class CardPrinted implements Comparable<CardPrinted>, InventoryItem
return c;
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(final CardPrinted o) {
int nameCmp = nameLcase.compareTo(o.nameLcase);
if (0 != nameCmp) { return nameCmp; }
// TODO: compare sets properly
if (0 != nameCmp) {
return nameCmp;
}
// TODO compare sets properly
return cardSet.compareTo(o.cardSet);
}
/**
* Checks if is alternate.
*
* @return true, if is alternate
*/
public boolean isAlternate() {
return isAlternate;
}
/**
* Checks if is double faced.
*
* @return true, if is double faced
*/
public boolean isDoubleFaced() {
return isDoubleFaced;
}
/**
* Number of filters based on CardPrinted values.
* Number of filters based on CardPrinted values.
*/
public abstract static class Predicates {
public static Predicate<CardPrinted> rarity(final boolean isEqual, final CardRarity value)
{
/**
* Rarity.
*
* @param isEqual the is equal
* @param value the value
* @return the predicate
*/
public static Predicate<CardPrinted> rarity(final boolean isEqual, final CardRarity value) {
return new PredicateRarity(value, isEqual);
}
public static Predicate<CardPrinted> printedInSets(final List<String> value, final boolean shouldContain)
{
/**
* Printed in sets.
*
* @param value the value
* @param shouldContain the should contain
* @return the predicate
*/
public static Predicate<CardPrinted> printedInSets(final List<String> value, final boolean shouldContain) {
if (value == null || value.isEmpty()) {
return Predicate.getTrue(CardPrinted.class);
}
return new PredicateSets(value, shouldContain);
}
/**
* Printed in sets.
*
* @param value the value
* @return the predicate
*/
public static Predicate<CardPrinted> printedInSets(final String value) {
if (value == null || value.isEmpty()) {
return Predicate.getTrue(CardPrinted.class);
}
return new PredicateSets(Arrays.asList(new String[]{value}), true);
return new PredicateSets(Arrays.asList(new String[] {value}), true);
}
/**
* Name.
*
* @param what the what
* @return the predicate
*/
public static Predicate<CardPrinted> name(final String what) {
return new PredicateName(PredicateString.StringOp.EQUALS, what);
}
}
/**
* Name.
*
* @param op the op
* @param what the what
* @return the predicate
*/
public static Predicate<CardPrinted> name(final PredicateString.StringOp op, final String what) {
return new PredicateName(op, what);
}
/**
* Names except.
*
* @param what the what
* @return the predicate
*/
public static Predicate<CardPrinted> namesExcept(final List<String> what) {
return new PredicateNamesExcept(what);
}
private static class PredicateNotAlternate extends Predicate<CardPrinted> {
@Override
public boolean isTrue(final CardPrinted card) {
@@ -201,9 +382,12 @@ public final class CardPrinted implements Comparable<CardPrinted>, InventoryItem
private static class PredicateSets extends Predicate<CardPrinted> {
private final List<String> sets;
private final boolean mustContain;
@Override public boolean isTrue(final CardPrinted card) {
@Override
public boolean isTrue(final CardPrinted card) {
return sets.contains(card.cardSet) == mustContain;
}
public PredicateSets(final List<String> wantSets, final boolean shouldContain) {
sets = wantSets; // maybe should make a copy here?
mustContain = shouldContain;
@@ -218,46 +402,64 @@ public final class CardPrinted implements Comparable<CardPrinted>, InventoryItem
return op(card.getName(), operand);
}
public PredicateName(final PredicateString.StringOp operator, final String operand)
{
public PredicateName(final PredicateString.StringOp operator, final String operand) {
super(operator);
this.operand = operand;
}
}
private static class PredicateNamesExcept extends PredicateString<CardPrinted> {
private final String[] operand;
@Override
public boolean isTrue(final CardPrinted card) {
String cardName = card.getName();
for(int i = 0; i < operand.length; i++) {
if ( op(cardName, operand[i]) ) return false;
for (int i = 0; i < operand.length; i++) {
if (op(cardName, operand[i])) {
return false;
}
}
return true;
}
public PredicateNamesExcept(final List<String> operand)
{
public PredicateNamesExcept(final List<String> operand) {
super(StringOp.EQUALS);
this.operand = operand.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
}
}
/**
* Pre-built predicates are stored here to allow their re-usage and easier access from code.
* Pre-built predicates are stored here to allow their re-usage and
* easier access from code.
*/
public abstract static class Presets {
// Think twice before using these, since rarity is a prop of printed card.
// Think twice before using these, since rarity is a prop of printed
// card.
/** The Constant isCommon. */
public static final Predicate<CardPrinted> isCommon = rarity(true, CardRarity.Common);
/** The Constant isUncommon. */
public static final Predicate<CardPrinted> isUncommon = rarity(true, CardRarity.Uncommon);
/** The Constant isRare. */
public static final Predicate<CardPrinted> isRare = rarity(true, CardRarity.Rare);
/** The Constant isMythicRare. */
public static final Predicate<CardPrinted> isMythicRare = rarity(true, CardRarity.MythicRare);
/** The Constant isRareOrMythic. */
public static final Predicate<CardPrinted> isRareOrMythic = Predicate.or(isRare, isMythicRare);
/** The Constant isSpecial. */
public static final Predicate<CardPrinted> isSpecial = rarity(true, CardRarity.Special);
/** The Constant exceptLands. */
public static final Predicate<CardPrinted> exceptLands = rarity(false, CardRarity.BasicLand);
/** The Constant isTrue. */
public static final Predicate<CardPrinted> isTrue = Predicate.getTrue(CardPrinted.class);
/** The Constant nonAlternate. */
public static final Predicate<CardPrinted> nonAlternate = new PredicateNotAlternate();
}
}

View File

@@ -1,34 +1,46 @@
package forge.item;
/**
* TODO: Write javadoc for this type.
/**
*
*
*/
public class CardPrintedCharacteristics {
private String name;
private String cardSet;
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
public final String getName() {
return name;
}
/**
* Sets the name.
*
* @param name0 the name to set
*/
public void setName(String name0) {
this.name = name0; // TODO: Add 0 to parameter's name.
public final void setName(final String name0) {
this.name = name0; // TODO Add 0 to parameter's name.
}
/**
* Gets the card set.
*
* @return the cardSet
*/
public String getCardSet() {
public final String getCardSet() {
return cardSet;
}
/**
* Sets the card set.
*
* @param cardSet0 the cardSet to set
*/
public void setCardSet(String cardSet0) {
public final void setCardSet(final String cardSet0) {
this.cardSet = cardSet0; // TODO: Add 0 to parameter's name.
}
}

View File

@@ -1,14 +1,29 @@
package forge.item;
/**
* Interface to define a player's inventory may hold.
* Should include CardPrinted, Booster, Pets, Plants... etc
/**
* Interface to define a player's inventory may hold. Should include
* CardPrinted, Booster, Pets, Plants... etc
*/
public interface InventoryItem /* extends Comparable */ {
/** An inventory item has to provide a name. */
public interface InventoryItem /* extends Comparable */{
/**
* An inventory item has to provide a name.
*
* @return the name
*/
String getName();
/** An inventory item has to provide a picture. */
/**
* An inventory item has to provide a picture.
*
* @return the image filename
*/
String getImageFilename();
/** Return type as a string */
/**
* Return type as a string.
*
* @return the type
*/
String getType();
}

View File

@@ -1,14 +1,29 @@
package forge.item;
/**
* Interface to define a player's inventory may hold.
* Should include CardPrinted, Booster, Pets, Plants... etc
/**
* Interface to define a player's inventory may hold. Should include
* CardPrinted, Booster, Pets, Plants... etc
*/
public interface InventoryItemFromSet extends InventoryItem {
/** An inventory item has to provide a name. */
/**
* An inventory item has to provide a name.
*
* @return the name
*/
String getName();
/** An inventory item has to provide a picture. */
/**
* An inventory item has to provide a picture.
*
* @return the image filename
*/
String getImageFilename();
/** An item belonging to a set should return its set as well*/
/**
* An item belonging to a set should return its set as well.
*
* @return the sets the
*/
String getSet();
}

View File

@@ -7,120 +7,136 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.slightlymagic.braids.util.lambda.Lambda1;
import forge.CardList;
import forge.card.CardRules;
import net.slightlymagic.braids.util.lambda.Lambda1;
/**
* <p>CardPoolView class.</p>
* <p>
* CardPoolView class.
* </p>
*
* @param <T> an InventoryItem
* @author Forge
* @version $Id: CardPoolView.java 9708 2011-08-09 19:34:12Z jendave $
* @param <T> an InventoryItem
*/
public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T, Integer>> {
// Field Accessors for select/aggregate operations with filters.
/**
*
*/
public final Lambda1<CardRules, Entry<T, Integer>> fnToCard =
new Lambda1<CardRules, Entry<T, Integer>>() {
@Override public CardRules apply(final Entry<T, Integer> from) {
T item = from.getKey();
return item instanceof CardPrinted ? ((CardPrinted) item).getCard() : null;
}
};
/** The fn to card. */
public final Lambda1<CardRules, Entry<T, Integer>> fnToCard = new Lambda1<CardRules, Entry<T, Integer>>() {
@Override
public CardRules apply(final Entry<T, Integer> from) {
T item = from.getKey();
return item instanceof CardPrinted ? ((CardPrinted) item).getCard() : null;
}
};
/**
*
*/
public final Lambda1<T, Entry<T, Integer>> fnToPrinted =
new Lambda1<T, Entry<T, Integer>>() {
@Override public T apply(final Entry<T, Integer> from) { return from.getKey(); }
};
/** The fn to printed. */
public final Lambda1<T, Entry<T, Integer>> fnToPrinted = new Lambda1<T, Entry<T, Integer>>() {
@Override
public T apply(final Entry<T, Integer> from) {
return from.getKey();
}
};
/**
*
*/
public final Lambda1<String, Entry<T, Integer>> fnToCardName =
new Lambda1<String, Entry<T, Integer>>() {
@Override public String apply(final Entry<T, Integer> from) { return from.getKey().getName(); }
};
/** The fn to card name. */
public final Lambda1<String, Entry<T, Integer>> fnToCardName = new Lambda1<String, Entry<T, Integer>>() {
@Override
public String apply(final Entry<T, Integer> from) {
return from.getKey().getName();
}
};
/**
*
*/
public final Lambda1<Integer, Entry<T, Integer>> fnToCount =
new Lambda1<Integer, Entry<T, Integer>>() {
@Override public Integer apply(final Entry<T, Integer> from) { return from.getValue(); }
};
/** The fn to count. */
public final Lambda1<Integer, Entry<T, Integer>> fnToCount = new Lambda1<Integer, Entry<T, Integer>>() {
@Override
public Integer apply(final Entry<T, Integer> from) {
return from.getValue();
}
};
// Constructors
/**
*
* ItemPoolView.
* @param cls a Class<T>
*/
public ItemPoolView(final Class<T> cls) { cards = new Hashtable<T, Integer>(); myClass = cls; }
/**
*
* ItemPoolView.
*
* @param cls
* a Class<T>
*/
public ItemPoolView(final Class<T> cls) {
cards = new Hashtable<T, Integer>();
myClass = cls;
}
/**
*
* ItemPoolView.
* @param inMap a Map<T, Integer>
* @param cls a Class<T>
*
* @param inMap
* a Map<T, Integer>
* @param cls
* a Class<T>
*/
public ItemPoolView(final Map<T, Integer> inMap, final Class<T> cls) { cards = inMap; myClass = cls; }
public ItemPoolView(final Map<T, Integer> inMap, final Class<T> cls) {
cards = inMap;
myClass = cls;
}
// Data members
/**
*
*/
/** The cards. */
protected Map<T, Integer> cards;
/**
*
*/
protected final Class<T> myClass; // class does not keep this in runtime by itself
/** The my class. */
protected final Class<T> myClass; // class does not keep this in runtime by
// itself
// same thing as above, it was copied to provide sorting (needed by table views in deck editors)
/**
*
*/
protected List<Entry<T, Integer>> cardsListOrdered = new ArrayList<Map.Entry<T,Integer>>();
// same thing as above, it was copied to provide sorting (needed by table
// views in deck editors)
/** The cards list ordered. */
protected List<Entry<T, Integer>> cardsListOrdered = new ArrayList<Map.Entry<T, Integer>>();
/**
*
*/
/** The is list in sync. */
protected boolean isListInSync = false;
/**
* iterator.
*
* @return Iterator<Entry<T, Integer>>
*/
@Override
public final Iterator<Entry<T, Integer>> iterator() { return cards.entrySet().iterator(); }
public final Iterator<Entry<T, Integer>> iterator() {
return cards.entrySet().iterator();
}
// Cards read only operations
/**
*
* contains.
* @param card a T
*
* @param card
* a T
* @return boolean
*/
public final boolean contains(final T card) {
if (cards == null) { return false; }
if (cards == null) {
return false;
}
return cards.containsKey(card);
}
/**
*
* count.
* @param card a T
*
* @param card
* a T
* @return int
*/
public final int count(final T card) {
if (cards == null) { return 0; }
if (cards == null) {
return 0;
}
Integer boxed = cards.get(card);
return boxed == null ? 0 : boxed.intValue();
}
@@ -128,35 +144,49 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
/**
*
* countAll.
*
* @return int
*/
public final int countAll() {
int result = 0;
if (cards != null) { for (Integer n : cards.values()) { result += n; } }
if (cards != null) {
for (Integer n : cards.values()) {
result += n;
}
}
return result;
}
/**
*
* countDistinct.
*
* @return int
*/
public final int countDistinct() { return cards.size(); }
public final int countDistinct() {
return cards.size();
}
/**
*
* isEmpty.
*
* @return boolean
*/
public final boolean isEmpty() { return cards == null || cards.isEmpty(); }
public final boolean isEmpty() {
return cards == null || cards.isEmpty();
}
/**
*
* getOrderedList.
*
* @return List<Entry<T, Integer>>
*/
public final List<Entry<T, Integer>> getOrderedList() {
if (!isListInSync) { rebuildOrderedList(); }
if (!isListInSync) {
rebuildOrderedList();
}
return cardsListOrdered;
}
@@ -173,12 +203,15 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
/**
*
* toFlatList.
*
* @return List<T>
*/
public final List<T> toFlatList() {
List<T> result = new ArrayList<T>();
for (Entry<T, Integer> e : this) {
for (int i = 0; i < e.getValue(); i++) { result.add(e.getKey()); }
for (int i = 0; i < e.getValue(); i++) {
result.add(e.getKey());
}
}
return result;
}
@@ -186,15 +219,16 @@ public class ItemPoolView<T extends InventoryItem> implements Iterable<Entry<T,
/**
*
* toForgeCardList.
*
* @return CardList
*/
public final CardList toForgeCardList() {
CardList result = new CardList();
for (Entry<T, Integer> e : this) {
if (e.getKey() instanceof CardPrinted) {
for (int i = 0; i < e.getValue(); i++) {
result.add(((CardPrinted) e.getKey()).toForgeCard());
}
for (int i = 0; i < e.getValue(); i++) {
result.add(((CardPrinted) e.getKey()).toForgeCard());
}
}
}
return result;

View File

@@ -1,2 +1,2 @@
/** Forge Card Game */
/** Forge Card Game. */
package forge.item;