checkstyle and refactor

This commit is contained in:
jendave
2011-10-30 07:17:41 +00:00
parent 21301e7b5f
commit 699f668b17
5 changed files with 124 additions and 96 deletions

View File

@@ -26,7 +26,8 @@ public class BoosterGenerator {
// Function to open a booster as it is.
/** The Constant IDENTITY_PICK. */
public static final Lambda1<List<CardPrinted>, BoosterGenerator> IDENTITY_PICK = new Lambda1<List<CardPrinted>, BoosterGenerator>() {
public static final Lambda1<List<CardPrinted>, BoosterGenerator> IDENTITY_PICK
= new Lambda1<List<CardPrinted>, BoosterGenerator>() {
@Override
public List<CardPrinted> apply(final BoosterGenerator arg1) {
return arg1.getBoosterPack();
@@ -43,7 +44,7 @@ public class BoosterGenerator {
* @return the simple picker
*/
public static Closure1<List<CardPrinted>, BoosterGenerator> getSimplePicker(final BoosterGenerator source) {
return new Closure1<List<CardPrinted>, BoosterGenerator>(IDENTITY_PICK, source);
return new Closure1<List<CardPrinted>, BoosterGenerator>(BoosterGenerator.IDENTITY_PICK, source);
}
// These lists are to hold cards grouped by rarity in advance.
@@ -59,7 +60,7 @@ public class BoosterGenerator {
// private List<CardPrinted> commonCreatures;
// private List<CardPrinted> commonNonCreatures;
private static final List<CardPrinted> emptyList = Collections.unmodifiableList(new ArrayList<CardPrinted>(0));
private static final List<CardPrinted> EMPTY_LIST = Collections.unmodifiableList(new ArrayList<CardPrinted>(0));
// Modern boosters contain 10 commons, 3 uncommmons, 1 rare/mythic
// They also contain 1 land and 1 token/rules, but we don't pick them now.
@@ -78,8 +79,8 @@ public class BoosterGenerator {
* the cards
*/
public BoosterGenerator(final Iterable<CardPrinted> cards) {
for (CardPrinted c : cards) {
addToRarity(c);
for (final CardPrinted c : cards) {
this.addToRarity(c);
}
}
@@ -90,8 +91,8 @@ public class BoosterGenerator {
* the d pool
*/
public BoosterGenerator(final Deck dPool) {
for (Entry<CardPrinted, Integer> e : dPool.getMain()) {
addToRarity(e.getKey());
for (final Entry<CardPrinted, Integer> e : dPool.getMain()) {
this.addToRarity(e.getKey());
}
}
@@ -107,34 +108,35 @@ public class BoosterGenerator {
if (!cardSet.canGenerateBooster()) {
throw new InvalidParameterException("BoosterGenerator: Set " + cardSet + " cannot generate boosters!");
}
CardSet.BoosterData bs = cardSet.getBoosterData();
final CardSet.BoosterData bs = cardSet.getBoosterData();
numCommons = bs.getCommon();
numUncommons = bs.getUncommon();
numRareSlots = bs.getRare();
numSpecials = bs.getSpecial();
numDoubleFaced = bs.getDoubleFaced();
this.numCommons = bs.getCommon();
this.numUncommons = bs.getUncommon();
this.numRareSlots = bs.getRare();
this.numSpecials = bs.getSpecial();
this.numDoubleFaced = bs.getDoubleFaced();
Predicate<CardPrinted> filter = CardPrinted.Predicates.printedInSets(cardSet.getCode());
List<CardPrinted> cardsInThisSet = filter.select(CardDb.instance().getAllCards());
final Predicate<CardPrinted> filter = CardPrinted.Predicates.printedInSets(cardSet.getCode());
final List<CardPrinted> cardsInThisSet = filter.select(CardDb.instance().getAllCards());
for (CardPrinted c : cardsInThisSet) {
addToRarity(c);
for (final CardPrinted c : cardsInThisSet) {
this.addToRarity(c);
// System.out.println(c);
}
// System.out.println("done");
}
private List<CardPrinted> pickRandomCards(final List<CardPrinted> source, final int count) {
return pickRandomCards(source, count, false);
return this.pickRandomCards(source, count, false);
}
private List<CardPrinted> pickRandomCards(final List<CardPrinted> source, final int count, boolean singleton) {
private List<CardPrinted> pickRandomCards(final List<CardPrinted> source,
final int count, final boolean singleton) {
int listSize = source == null ? 0 : source.size();
if (count <= 0 || listSize == 0) {
return emptyList;
if ((count <= 0) || (listSize == 0)) {
return BoosterGenerator.EMPTY_LIST;
}
List<CardPrinted> result = new ArrayList<CardPrinted>(count);
final List<CardPrinted> result = new ArrayList<CardPrinted>(count);
int index = Integer.MAX_VALUE;
for (int iCard = 0; iCard < count; iCard++) {
@@ -154,20 +156,21 @@ public class BoosterGenerator {
return result;
}
private List<CardPrinted> pickRandomRaresOrMythics(final List<CardPrinted> rares, final List<CardPrinted> mythics, final int count) {
int raresSize = rares == null ? 0 : rares.size();
int mythicsSize = mythics == null ? 0 : mythics.size();
if (count <= 0 || raresSize == 0) {
return emptyList;
private List<CardPrinted> pickRandomRaresOrMythics(final List<CardPrinted> rares, final List<CardPrinted> mythics,
final int count) {
final int raresSize = rares == null ? 0 : rares.size();
final int mythicsSize = mythics == null ? 0 : mythics.size();
if ((count <= 0) || (raresSize == 0)) {
return BoosterGenerator.EMPTY_LIST;
}
List<CardPrinted> result = new ArrayList<CardPrinted>(count);
final List<CardPrinted> result = new ArrayList<CardPrinted>(count);
int indexRares = Integer.MAX_VALUE;
int indexMythics = Integer.MAX_VALUE;
for (int iCard = 0; iCard < count; iCard++) {
int rollD8 = MyRandom.getRandom().nextInt(8);
boolean takeMythic = mythicsSize > 0 && rollD8 < 1;
final int rollD8 = MyRandom.getRandom().nextInt(8);
final boolean takeMythic = (mythicsSize > 0) && (rollD8 < 1);
if (takeMythic) {
if (indexRares >= raresSize) {
Collections.shuffle(mythics, MyRandom.getRandom());
@@ -187,25 +190,30 @@ public class BoosterGenerator {
return result;
}
/**
* Gets the booster pack.
*
* @return the booster pack
*/
public final List<CardPrinted> getBoosterPack() {
return getBoosterPack(numCommons, numUncommons, numRareSlots, 0, 0, numSpecials, numDoubleFaced, 0, 0);
return this.getBoosterPack(this.numCommons, this.numUncommons, this.numRareSlots, 0, 0, this.numSpecials,
this.numDoubleFaced, 0, 0);
}
public final List<CardPrinted> getSingletonBoosterPack( final int nAnyCard) {
List<CardPrinted> temp = new ArrayList<CardPrinted>();
/**
* Gets the singleton booster pack.
*
* @param nAnyCard the n any card
* @return the singleton booster pack
*/
public final List<CardPrinted> getSingletonBoosterPack(final int nAnyCard) {
final List<CardPrinted> temp = new ArrayList<CardPrinted>();
temp.addAll(pickRandomCards(allButLands, nAnyCard, true));
temp.addAll(this.pickRandomCards(this.allButLands, nAnyCard, true));
return temp;
}
/**
* So many parameters are needed for custom limited cardpools,.
*
@@ -233,9 +241,9 @@ public class BoosterGenerator {
final int nRares, final int nMythics, final int nSpecs, final int nDoubls, final int nAnyCard,
final int nLands) {
List<CardPrinted> temp = new ArrayList<CardPrinted>();
final List<CardPrinted> temp = new ArrayList<CardPrinted>();
temp.addAll(pickRandomCards(commons, nCom));
temp.addAll(this.pickRandomCards(this.commons, nCom));
/*
* if( nComCreat > 0 || nComNonCr > 0) { if
* (commonNonCreatures.isEmpty()) {
@@ -245,50 +253,50 @@ public class BoosterGenerator {
* temp.addAll(pickRandomCards(commonNonCreatures, nComNonCr)); }
*/
temp.addAll(pickRandomCards(uncommons, nUnc));
temp.addAll(this.pickRandomCards(this.uncommons, nUnc));
if (nRareSlots > 0) {
temp.addAll(pickRandomRaresOrMythics(rares, mythics, nRareSlots));
temp.addAll(this.pickRandomRaresOrMythics(this.rares, this.mythics, nRareSlots));
}
if (nRares > 0 || nMythics > 0) {
temp.addAll(pickRandomCards(rares, nRares));
temp.addAll(pickRandomCards(mythics, nMythics));
if ((nRares > 0) || (nMythics > 0)) {
temp.addAll(this.pickRandomCards(this.rares, nRares));
temp.addAll(this.pickRandomCards(this.mythics, nMythics));
}
if (nDoubls > 0) {
temp.addAll(pickRandomCards(doubleFaced, nDoubls));
temp.addAll(this.pickRandomCards(this.doubleFaced, nDoubls));
}
temp.addAll(pickRandomCards(specials, nSpecs));
temp.addAll(this.pickRandomCards(this.specials, nSpecs));
temp.addAll(pickRandomCards(allButLands, nAnyCard));
temp.addAll(this.pickRandomCards(this.allButLands, nAnyCard));
temp.addAll(pickRandomCards(basicLands, nLands));
temp.addAll(this.pickRandomCards(this.basicLands, nLands));
return temp;
}
private void addToRarity(final CardPrinted c) {
if(c.isAlternate()) {
if (c.isAlternate()) {
return;
}
if (c.isDoubleFaced() && numDoubleFaced > 0) {
doubleFaced.add(c);
if (c.isDoubleFaced() && (this.numDoubleFaced > 0)) {
this.doubleFaced.add(c);
} else {
switch (c.getRarity()) {
case Common:
commons.add(c);
this.commons.add(c);
break;
case Uncommon:
uncommons.add(c);
this.uncommons.add(c);
break;
case Rare:
rares.add(c);
this.rares.add(c);
break;
case MythicRare:
mythics.add(c);
this.mythics.add(c);
break;
case Special:
specials.add(c);
this.specials.add(c);
break;
default:
break;
@@ -296,9 +304,9 @@ public class BoosterGenerator {
}
if (c.getCard().getType().isBasicLand()) {
basicLands.add(c);
this.basicLands.add(c);
} else {
allButLands.add(c);
this.allButLands.add(c);
}
}

View File

@@ -39,13 +39,13 @@ public final class BoosterUtils {
*/
public static List<CardPrinted> getQuestStarterDeck(final Predicate<CardPrinted> filter, final int numCommon,
final int numUncommon, final int numRare) {
ArrayList<CardPrinted> cards = new ArrayList<CardPrinted>();
final ArrayList<CardPrinted> cards = new ArrayList<CardPrinted>();
// Each color should have around the same amount of monocolored cards
// There should be 3 Colorless cards for every 4 cards in a single color
// There should be 1 Multicolor card for every 4 cards in a single color
List<Predicate<CardRules>> colorFilters = new ArrayList<Predicate<CardRules>>();
final List<Predicate<CardRules>> colorFilters = new ArrayList<Predicate<CardRules>>();
colorFilters.add(CardRules.Predicates.Presets.isMulticolor);
for (int i = 0; i < 4; i++) {
@@ -60,28 +60,29 @@ public final class BoosterUtils {
colorFilters.add(CardRules.Predicates.Presets.isGreen);
}
Iterable<CardPrinted> cardpool = CardDb.instance().getAllUniqueCards();
final Iterable<CardPrinted> cardpool = CardDb.instance().getAllUniqueCards();
cards.addAll(generateDefinetlyColouredCards(cardpool,
cards.addAll(BoosterUtils.generateDefinetlyColouredCards(cardpool,
Predicate.and(filter, CardPrinted.Predicates.Presets.isCommon), numCommon, colorFilters));
cards.addAll(generateDefinetlyColouredCards(cardpool,
cards.addAll(BoosterUtils.generateDefinetlyColouredCards(cardpool,
Predicate.and(filter, CardPrinted.Predicates.Presets.isUncommon), numUncommon, colorFilters));
int nRares = numRare, nMythics = 0;
Predicate<CardPrinted> filterMythics = Predicate.and(filter, CardPrinted.Predicates.Presets.isMythicRare);
boolean haveMythics = filterMythics.any(cardpool);
for (int iSlot = 0; haveMythics && iSlot < numRare; iSlot++) {
if (MyRandom.getRandom().nextInt(7) < 1) { // a bit higher chance to get
// a mythic
final Predicate<CardPrinted> filterMythics = Predicate.and(filter, CardPrinted.Predicates.Presets.isMythicRare);
final boolean haveMythics = filterMythics.any(cardpool);
for (int iSlot = 0; haveMythics && (iSlot < numRare); iSlot++) {
if (MyRandom.getRandom().nextInt(7) < 1) { // a bit higher chance to
// get
// a mythic
nRares--;
nMythics++;
}
}
cards.addAll(generateDefinetlyColouredCards(cardpool,
cards.addAll(BoosterUtils.generateDefinetlyColouredCards(cardpool,
Predicate.and(filter, CardPrinted.Predicates.Presets.isRare), nRares, colorFilters));
if (nMythics > 0) {
cards.addAll(generateDefinetlyColouredCards(cardpool, filterMythics, nMythics, colorFilters));
cards.addAll(BoosterUtils.generateDefinetlyColouredCards(cardpool, filterMythics, nMythics, colorFilters));
}
return cards;
}
@@ -102,9 +103,9 @@ public final class BoosterUtils {
private static ArrayList<CardPrinted> generateDefinetlyColouredCards(final Iterable<CardPrinted> source,
final Predicate<CardPrinted> filter, final int cntNeeded, final List<Predicate<CardRules>> allowedColors) {
// If color is null, use colorOrder progression to grab cards
ArrayList<CardPrinted> result = new ArrayList<CardPrinted>();
final ArrayList<CardPrinted> result = new ArrayList<CardPrinted>();
int size = allowedColors == null ? 0 : allowedColors.size();
final int size = allowedColors == null ? 0 : allowedColors.size();
Collections.shuffle(allowedColors);
int cntMade = 0, iAttempt = 0;
@@ -113,7 +114,7 @@ public final class BoosterUtils {
int allowedMisses = (2 + size + 2) * cntNeeded; // lol, 2+2 is not magic
// constant!
while (cntMade < cntNeeded && allowedMisses > 0) {
while ((cntMade < cntNeeded) && (allowedMisses > 0)) {
CardPrinted card = null;
if (size > 0) {
@@ -128,7 +129,7 @@ public final class BoosterUtils {
card = filter.random(source);
}
if (card != null && !result.contains(card)) {
if ((card != null) && !result.contains(card)) {
result.add(card);
cntMade++;
} else {
@@ -153,8 +154,8 @@ public final class BoosterUtils {
* @return the list
*/
public static List<CardPrinted> generateCards(final int num, final CardRarity rarity, final String color) {
Predicate<CardPrinted> whatYouWant = getPredicateForConditions(rarity, color);
return generateDistinctCards(CardDb.instance().getAllUniqueCards(), whatYouWant, num);
final Predicate<CardPrinted> whatYouWant = BoosterUtils.getPredicateForConditions(rarity, color);
return BoosterUtils.generateDistinctCards(CardDb.instance().getAllUniqueCards(), whatYouWant, num);
}
/**
@@ -170,25 +171,26 @@ public final class BoosterUtils {
* the color
* @return the list
*/
public static List<CardPrinted> generateCards(final Predicate<CardPrinted> filter, final int num, final CardRarity rarity,
final String color) {
Predicate<CardPrinted> whatYouWant = Predicate.and(filter, getPredicateForConditions(rarity, color));
return generateDistinctCards(CardDb.instance().getAllUniqueCards(), whatYouWant, num);
public static List<CardPrinted> generateCards(final Predicate<CardPrinted> filter, final int num,
final CardRarity rarity, final String color) {
final Predicate<CardPrinted> whatYouWant = Predicate.and(filter,
BoosterUtils.getPredicateForConditions(rarity, color));
return BoosterUtils.generateDistinctCards(CardDb.instance().getAllUniqueCards(), whatYouWant, num);
}
private static List<CardPrinted> generateDistinctCards(final Iterable<CardPrinted> source,
final Predicate<CardPrinted> filter, final int cntNeeded) {
ArrayList<CardPrinted> result = new ArrayList<CardPrinted>();
final ArrayList<CardPrinted> result = new ArrayList<CardPrinted>();
int cntMade = 0;
// This will prevent endless loop @ wh
int allowedMisses = (2 + 2) * cntNeeded; // lol, 2+2 is not magic
// constant!
while (cntMade < cntNeeded && allowedMisses > 0) {
CardPrinted card = filter.random(source);
while ((cntMade < cntNeeded) && (allowedMisses > 0)) {
final CardPrinted card = filter.random(source);
if (card != null && !result.contains(card)) {
if ((card != null) && !result.contains(card)) {
result.add(card);
cntMade++;
} else {
@@ -219,7 +221,7 @@ public final class BoosterUtils {
if (StringUtils.isBlank(color)) {
colorFilter = Predicate.getTrue(CardRules.class);
} else {
String col = color.toLowerCase();
final String col = color.toLowerCase();
if (col.startsWith("wh")) {
colorFilter = CardRules.Predicates.Presets.isWhite;
} else if (col.startsWith("bla")) {
@@ -251,18 +253,18 @@ public final class BoosterUtils {
* @return the variety
*/
public static List<CardPrinted> getVariety(final List<CardPrinted> in) {
List<CardPrinted> out = new ArrayList<CardPrinted>();
final List<CardPrinted> out = new ArrayList<CardPrinted>();
Collections.shuffle(in, MyRandom.getRandom());
for (int i = 0; i < Constant.Color.COLORS.length; i++) {
CardPrinted check = findCardOfColor(in, i);
final CardPrinted check = BoosterUtils.findCardOfColor(in, i);
if (check != null) {
out.add(check);
}
}
return out;
}// getVariety()
} // getVariety()
/**
* Find card of color.
@@ -274,7 +276,7 @@ public final class BoosterUtils {
* @return the card printed
*/
public static CardPrinted findCardOfColor(final List<CardPrinted> in, final int color) {
Predicate<CardRules> filter = CardRules.Predicates.Presets.colors.get(color);
final Predicate<CardRules> filter = CardRules.Predicates.Presets.colors.get(color);
if (null == filter) {
return null;
}

View File

@@ -31,10 +31,10 @@ public class CardCharacteristics {
private ArrayList<Trigger> triggers = new ArrayList<Trigger>();
private ArrayList<StaticAbility> staticAbilities = new ArrayList<StaticAbility>();
private ArrayList<String> staticAbilityStrings = new ArrayList<String>();
private String ImageFilename = "";
private String imageFilename = "";
private String imageName = "";
private Map<String, String> sVars = new TreeMap<String, String>();
private ArrayList<SetInfo> Sets = new ArrayList<SetInfo>();
private ArrayList<SetInfo> sets = new ArrayList<SetInfo>();
/**
* Gets the name.
@@ -300,7 +300,7 @@ public class CardCharacteristics {
* @return the imageFilename
*/
public final String getImageFilename() {
return ImageFilename;
return imageFilename;
}
/**
@@ -310,7 +310,7 @@ public class CardCharacteristics {
* the imageFilename to set
*/
public final void setImageFilename(final String imageFilename0) {
ImageFilename = imageFilename0; // TODO: Add 0 to parameter's name.
imageFilename = imageFilename0; // TODO: Add 0 to parameter's name.
}
/**
@@ -339,7 +339,7 @@ public class CardCharacteristics {
* @return the sets
*/
public final ArrayList<SetInfo> getSets() {
return Sets;
return sets;
}
/**
@@ -349,7 +349,7 @@ public class CardCharacteristics {
* the sets to set
*/
public final void setSets(final ArrayList<SetInfo> sets0) {
Sets = new ArrayList<SetInfo>(sets0); // TODO: Add 0 to parameter's
sets = new ArrayList<SetInfo>(sets0); // TODO: Add 0 to parameter's
// name.
}

View File

@@ -49,7 +49,7 @@ public final class CardColor implements Comparable<CardColor> {
}
/** The null color. */
public static CardColor nullColor = new CardColor();
private static CardColor nullColor = new CardColor();
/**
* Checks for any color.
@@ -264,4 +264,22 @@ public final class CardColor implements Comparable<CardColor> {
return "multi";
}
}
/**
* Gets the null color.
*
* @return the nullColor
*/
public static CardColor getNullColor() {
return nullColor;
}
/**
* Sets the null color.
*
* @param nullColor the nullColor to set
*/
public static void setNullColor(CardColor nullColor) {
CardColor.nullColor = nullColor; // TODO: Add 0 to parameter's name.
}
}

View File

@@ -23,7 +23,7 @@ public abstract class PresetColumns {
}
private static CardColor toColor(final InventoryItem i) {
return i instanceof CardPrinted ? ((CardPrinted) i).getCard().getColor() : CardColor.nullColor;
return i instanceof CardPrinted ? ((CardPrinted) i).getCard().getColor() : CardColor.getNullColor();
}
private static String toPTL(final InventoryItem i) {