From 72d0cfdbbc2018af98dffb235417307df7fbbf5a Mon Sep 17 00:00:00 2001 From: drdev Date: Fri, 22 Nov 2013 02:23:53 +0000 Subject: [PATCH] Whitespace cleanup --- .../src/main/java/forge/StaticData.java | 133 +++++++++--------- .../src/main/java/forge/card/CardDb.java | 70 +++++---- .../src/main/java/forge/card/CardRules.java | 98 ++++++------- .../src/main/java/forge/item/PaperCard.java | 13 +- .../card/cardfactory/CardStorageReader.java | 66 ++++----- 5 files changed, 175 insertions(+), 205 deletions(-) diff --git a/forge-core/src/main/java/forge/StaticData.java b/forge-core/src/main/java/forge/StaticData.java index 84b5bcbd6fd..f30af6b4f6b 100644 --- a/forge-core/src/main/java/forge/StaticData.java +++ b/forge-core/src/main/java/forge/StaticData.java @@ -21,90 +21,83 @@ import forge.util.storage.StorageBase; * @author Max */ public class StaticData { + private final CardDb commonCards; + private final CardDb variantCards; + private final CardEdition.Collection editions; + private final IStorage boosters; + private final IStorage specialBoosters; + private final IStorage tournaments; + private final IStorage fatPacks; + private final IStorage printSheets; - private final CardDb commonCards; - private final CardDb variantCards; - private final CardEdition.Collection editions; - private final IStorage boosters; - private final IStorage specialBoosters; - private final IStorage tournaments; - private final IStorage fatPacks; - private final IStorage printSheets; + private static StaticData lastInstance = null; - private static StaticData lastInstance = null; + public StaticData(ICardStorageReader reader, String editionFolder, String blockDataFolder) { + this.editions = new CardEdition.Collection(new CardEdition.Reader(new File(editionFolder))); + lastInstance = this; + final Map regularCards = new TreeMap(String.CASE_INSENSITIVE_ORDER); + final Map variantsCards = new TreeMap(String.CASE_INSENSITIVE_ORDER); - - public StaticData(ICardStorageReader reader, String editionFolder, String blockDataFolder) { - this.editions = new CardEdition.Collection(new CardEdition.Reader(new File(editionFolder))); - lastInstance = this; + List rules = reader.loadCards(); + for (CardRules card : rules) { + if (null == card) continue; - final Map regularCards = new TreeMap(String.CASE_INSENSITIVE_ORDER); - final Map variantsCards = new TreeMap(String.CASE_INSENSITIVE_ORDER); + final String cardName = card.getName(); + if ( card.isVariant() ) { + variantsCards.put(cardName, card); + } + else { + regularCards.put(cardName, card); + } + } + commonCards = new CardDb(regularCards, editions, false); + variantCards = new CardDb(variantsCards, editions, false); - List rules = reader.loadCards(); - for (CardRules card : rules) { - if (null == card) continue; - - final String cardName = card.getName(); - if ( card.isVariant() ) - variantsCards.put(cardName, card); - else - regularCards.put(cardName, card); - } - - commonCards = new CardDb(regularCards, editions, false); - variantCards = new CardDb(variantsCards, editions, false); + this.boosters = new StorageBase("Boosters", editions.getBoosterGenerator()); + this.specialBoosters = new StorageBase("Special boosters", new SealedProduct.Template.Reader(new File(blockDataFolder, "boosters-special.txt"))); + this.tournaments = new StorageBase("Starter sets", new SealedProduct.Template.Reader(new File(blockDataFolder, "starters.txt"))); + this.fatPacks = new StorageBase("Fat packs", new FatPack.Template.Reader("res/blockdata/fatpacks.txt")); + this.printSheets = new StorageBase("Special print runs", new PrintSheet.Reader(new File(blockDataFolder, "printsheets.txt"))); + } + public final static StaticData instance() { + return lastInstance; + } - this.boosters = new StorageBase("Boosters", editions.getBoosterGenerator()); - this.specialBoosters = new StorageBase("Special boosters", new SealedProduct.Template.Reader(new File(blockDataFolder, "boosters-special.txt"))); - this.tournaments = new StorageBase("Starter sets", new SealedProduct.Template.Reader(new File(blockDataFolder, "starters.txt"))); - this.fatPacks = new StorageBase("Fat packs", new FatPack.Template.Reader("res/blockdata/fatpacks.txt")); - this.printSheets = new StorageBase("Special print runs", new PrintSheet.Reader(new File(blockDataFolder, "printsheets.txt"))); - } - - public final static StaticData instance() { - return lastInstance; - } - - - public final CardEdition.Collection getEditions() { - return this.editions; - } + public final CardEdition.Collection getEditions() { + return this.editions; + } - /** @return {@link forge.util.storage.IStorageView}<{@link forge.item.FatPackTemplate}> */ - public IStorage getFatPacks() { - return fatPacks; - } + /** @return {@link forge.util.storage.IStorageView}<{@link forge.item.FatPackTemplate}> */ + public IStorage getFatPacks() { + return fatPacks; + } - /** @return {@link forge.util.storage.IStorageView}<{@link forge.card.BoosterTemplate}> */ - public final IStorage getTournamentPacks() { - return tournaments; - } + /** @return {@link forge.util.storage.IStorageView}<{@link forge.card.BoosterTemplate}> */ + public final IStorage getTournamentPacks() { + return tournaments; + } - /** @return {@link forge.util.storage.IStorageView}<{@link forge.card.BoosterTemplate}> */ - public final IStorage getBoosters() { - return boosters; - } + /** @return {@link forge.util.storage.IStorageView}<{@link forge.card.BoosterTemplate}> */ + public final IStorage getBoosters() { + return boosters; + } - public final IStorage getSpecialBoosters() { - return specialBoosters; - } + public final IStorage getSpecialBoosters() { + return specialBoosters; + } - public IStorage getPrintSheets() { - return printSheets; - } + public IStorage getPrintSheets() { + return printSheets; + } + public CardDb getCommonCards() { + return commonCards; + } -public CardDb getCommonCards() { - return commonCards; -} - - -public CardDb getVariantCards() { - return variantCards; -} - + public CardDb getVariantCards() { + return variantCards; + } } diff --git a/forge-core/src/main/java/forge/card/CardDb.java b/forge-core/src/main/java/forge/card/CardDb.java index f91b558acc4..a6ad5566e9f 100644 --- a/forge-core/src/main/java/forge/card/CardDb.java +++ b/forge-core/src/main/java/forge/card/CardDb.java @@ -39,7 +39,6 @@ import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import forge.card.CardEdition.CardInSet; -import forge.deck.CardPool; import forge.item.PaperCard; import forge.util.Aggregates; import forge.util.CollectionSuppliers; @@ -54,12 +53,11 @@ public final class CardDb implements ICardDatabase { private final Multimap allCardsByName = Multimaps.newListMultimap(new TreeMap>(String.CASE_INSENSITIVE_ORDER), CollectionSuppliers.arrayLists()); private final Map uniqueCardsByName = new TreeMap(String.CASE_INSENSITIVE_ORDER); private final Map rulesByName; - + private final List allCards = new ArrayList(); - private final List roAllCards = Collections.unmodifiableList(allCards); + private final List roAllCards = Collections.unmodifiableList(allCards); private final Collection roUniqueCards = Collections.unmodifiableCollection(uniqueCardsByName.values()); private final CardEdition.Collection editions; - public CardDb(Map rules, CardEdition.Collection editions0, boolean logMissingCards) { this.rulesByName = rules; @@ -72,14 +70,14 @@ public final class CardDb implements ICardDatabase { String lastCardName = null; int artIdx = 0; for(CardEdition.CardInSet cis : e.getCards()) { - if ( cis.name.equals(lastCardName) ) + if ( cis.name.equals(lastCardName) ) artIdx++; else { artIdx = 0; lastCardName = cis.name; } CardRules cr = rulesByName.get(lastCardName); - if( cr != null ) + if( cr != null ) addCard(new PaperCard(cr, e.getCode(), cis.rarity, artIdx)); else if (worthLogging) missingCards.add(cis.name); @@ -94,7 +92,7 @@ public final class CardDb implements ICardDatabase { missingCards.clear(); } } - + for(CardRules cr : rulesByName.values()) { if( !allCardsByName.containsKey(cr.getName()) ) { @@ -102,7 +100,7 @@ public final class CardDb implements ICardDatabase { addCard(new PaperCard(cr, CardEdition.UNKNOWN.getCode(), CardRarity.Special, 0)); } } - + reIndex(); } @@ -155,7 +153,7 @@ public final class CardDb implements ICardDatabase { public PaperCard tryGetCard(final String cardName0) { return tryGetCard(cardName0, true); } - + @Override public PaperCard tryGetCard(final String cardName0, boolean fromLastSet) { if (null == cardName0) { @@ -164,23 +162,23 @@ public final class CardDb implements ICardDatabase { final boolean isFoil = this.isFoil(cardName0); final String cardName = isFoil ? this.removeFoilSuffix(cardName0) : cardName0; - + final ImmutablePair nameWithSet = CardDb.splitCardName(cardName); - - final PaperCard res = nameWithSet.right == null - ? ( fromLastSet ? this.uniqueCardsByName.get(nameWithSet.left) : Aggregates.random(this.allCardsByName.get(nameWithSet.left)) ) + + final PaperCard res = nameWithSet.right == null + ? ( fromLastSet ? this.uniqueCardsByName.get(nameWithSet.left) : Aggregates.random(this.allCardsByName.get(nameWithSet.left)) ) : tryGetCard(nameWithSet.left, nameWithSet.right); return null != res && isFoil ? getFoiled(res) : res; } - + @Override public PaperCard tryGetCardPrintedByDate(final String name0, final boolean fromLatestSet, final Date printedBefore) { final boolean isFoil = this.isFoil(name0); final String cardName = isFoil ? this.removeFoilSuffix(name0) : name0; final ImmutablePair nameWithSet = CardDb.splitCardName(cardName); - + PaperCard res = null; - if (null != nameWithSet.right) // set explicitly requested, should return card from it and disregard the date + if (null != nameWithSet.right) // set explicitly requested, should return card from it and disregard the date res = tryGetCard(nameWithSet.left, nameWithSet.right); else { Collection cards = this.allCardsByName.get(nameWithSet.left); // cards are sorted by datetime desc @@ -196,13 +194,12 @@ public final class CardDb implements ICardDatabase { return null != res && isFoil ? getFoiled(res) : res; } - @Override public PaperCard tryGetCard(final String cardName, String setName) { return tryGetCard(cardName, setName, -1); } - + @Override public PaperCard tryGetCard(final String cardName0, String setName, int index) { final boolean isFoil = this.isFoil(cardName0); @@ -212,7 +209,7 @@ public final class CardDb implements ICardDatabase { if ( null == cards ) return null; CardEdition edition = editions.get(setName); - if ( null == edition ) + if ( null == edition ) return tryGetCard(cardName, true); // set not found, try to get the same card from just any set. String effectiveSet = edition.getCode(); @@ -227,7 +224,7 @@ public final class CardDb implements ICardDatabase { if (cnt == 0 ) return null; result = cnt == 1 ? candidates[0] : candidates[MyRandom.getRandom().nextInt(cnt)]; - } else + } else for( PaperCard pc : cards ) { if( pc.getEdition().equalsIgnoreCase(effectiveSet) && index == pc.getArtIndex() ) { result = pc; @@ -237,7 +234,7 @@ public final class CardDb implements ICardDatabase { if ( result == null ) return null; return isFoil ? getFoiled(result) : result; } - + public PaperCard getFoiled(PaperCard card0) { // Here - I am still unsure if there should be a cache Card->Card from unfoiled to foiled, to avoid creation of N instances of single plains return new PaperCard(card0.getRules(), card0.getEdition(), card0.getRarity(), card0.getArtIndex(), true); @@ -252,17 +249,17 @@ public final class CardDb implements ICardDatabase { } return cnt; } - + @Override public int getMaxPrintCount(String cardName) { int max = -1; for( PaperCard pc : allCardsByName.get(cardName) ) { - if ( max < pc.getArtIndex() ) + if ( max < pc.getArtIndex() ) max = pc.getArtIndex(); } return max + 1; - } - + } + // Single fetch @Override public PaperCard getCard(final String name) { @@ -279,8 +276,7 @@ public final class CardDb implements ICardDatabase { } return result; } - - + @Override public PaperCard getCardPrintedByDate(final String name0, final boolean fromLatestSet, Date printedBefore ) { // Sometimes they read from decks things like "CardName|Set" - but we @@ -290,8 +286,8 @@ public final class CardDb implements ICardDatabase { throw new NoSuchElementException(String.format("Card '%s' released before %s not found in our database.", name0, printedBefore.toString())); } return result; - } - + } + // Advanced fetch by name+set @Override public PaperCard getCard(final String name, final String set) { @@ -300,7 +296,6 @@ public final class CardDb implements ICardDatabase { @Override public PaperCard getCard(final String name, final String set, final int artIndex) { - final PaperCard result = tryGetCard(name, set, artIndex); if (null == result) { final String message = String.format("Asked for '%s' from '%s' #%d: db didn't find that copy.", name, set, artIndex); @@ -329,7 +324,7 @@ public final class CardDb implements ICardDatabase { public Predicate wasPrintedInSets(List setCodes) { return new PredicateExistsInSets(setCodes); } - + private class PredicateExistsInSets implements Predicate { private final List sets; @@ -340,20 +335,20 @@ public final class CardDb implements ICardDatabase { @Override public boolean apply(final PaperCard subject) { Collection cc = allCardsByName.get(subject.getName()); - for(PaperCard c : cc) - if (sets.contains(c.getEdition())) + for(PaperCard c : cc) + if (sets.contains(c.getEdition())) return true; return false; } } - + private final Editor editor = new Editor(); public Editor getEditor() { return editor; } public class Editor { private boolean immediateReindex = true; public CardRules putCard(CardRules rules) { return putCard(rules, null); /* will use data from editions folder */ } public CardRules putCard(CardRules rules, List> whenItWasPrinted){ // works similarly to Map, returning prev. value - String cardName = rules.getName(); + String cardName = rules.getName(); CardRules result = rulesByName.put(cardName, rules); // 1. generate all paper cards from edition data we have (either explicit, or found in res/editions, or add to unknown edition) List paperCards = new ArrayList(); @@ -389,7 +384,7 @@ public final class CardDb implements ICardDatabase { // 3. reindex can be temporary disabled and run after the whole batch of rules is added to db. if(immediateReindex) reIndex(); - + return result; } public void removeCard(String name) { @@ -404,7 +399,7 @@ public final class CardDb implements ICardDatabase { } } public void rebuildIndex() { reIndex(); } - + public boolean isImmediateReindex() { return immediateReindex; } @@ -412,5 +407,4 @@ public final class CardDb implements ICardDatabase { this.immediateReindex = immediateReindex; } } - } diff --git a/forge-core/src/main/java/forge/card/CardRules.java b/forge-core/src/main/java/forge/card/CardRules.java index 35c7381a500..9925aa2efa8 100644 --- a/forge-core/src/main/java/forge/card/CardRules.java +++ b/forge-core/src/main/java/forge/card/CardRules.java @@ -40,7 +40,7 @@ public final class CardRules implements ICardCharacteristics { //private final Map setsPrinted = new TreeMap(String.CASE_INSENSITIVE_ORDER); private CardAiHints aiHints; - + private ColorSet colorIdentity = null; private CardRules(ICardFace[] faces, CardSplitType altMode, CardAiHints cah) { @@ -48,29 +48,29 @@ public final class CardRules implements ICardCharacteristics { mainPart = faces[0]; otherPart = faces[1]; aiHints = cah; - + //System.out.print(faces[0].getName()); - + // for (Entry cs : sets.entrySet()) { // if( CardRulesReader.editions.get(cs.getKey()) != null ) // setsPrinted.put(cs.getKey(), cs.getValue()); // } // -// if ( setsPrinted.isEmpty() ) { -// System.err.println(getName() + " was not assigned any set."); +// if ( setsPrinted.isEmpty() ) { +// System.err.println(getName() + " was not assigned any set."); // setsPrinted.put(CardEdition.UNKNOWN.getCode(), new CardInSet(CardRarity.Common, 1) ); // } -// +// //Calculate Color Identity byte colMask = calculateColorIdentity(mainPart); - + if(otherPart != null) { colMask |= calculateColorIdentity(otherPart); - } + } colorIdentity = ColorSet.fromMask(colMask); } - + private byte calculateColorIdentity(ICardFace face) { byte res = face.getColor().getColor(); @@ -156,7 +156,7 @@ public final class CardRules implements ICardCharacteristics { public ColorSet getColor() { switch(splitType.getAggregationMethod()) { case COMBINE: - return ColorSet.fromMask(mainPart.getColor().getColor() | otherPart.getColor().getColor()); + return ColorSet.fromMask(mainPart.getColor().getColor() | otherPart.getColor().getColor()); default: return mainPart.getColor(); } @@ -172,7 +172,7 @@ public final class CardRules implements ICardCharacteristics { public String getOracleText() { switch(splitType.getAggregationMethod()) { case COMBINE: - return mainPart.getOracleText() + "\r\n\r\n" + otherPart.getOracleText(); + return mainPart.getOracleText() + "\r\n\r\n" + otherPart.getOracleText(); default: return mainPart.getOracleText(); } @@ -220,12 +220,11 @@ public final class CardRules implements ICardCharacteristics { public final List getAbilities() { return null; } - + public ColorSet getColorIdentity() { return colorIdentity; - } - + /** Instantiates class, reads a card. For batch operations better create you own reader instance. */ public static CardRules fromScript(Iterable script) { Reader crr = new Reader(); @@ -233,24 +232,23 @@ public final class CardRules implements ICardCharacteristics { crr.parseLine(line); } return crr.getCard(); - } + } // Reads cardname.txt public static class Reader { - // fields to build + // fields to build private CardFace[] faces = new CardFace[] { null, null }; private String[] pictureUrl = new String[] { null, null }; private int curFace = 0; private CardSplitType altMode = CardSplitType.None; - private String handLife = null; - + private String handLife = null; + // fields to build CardAiHints private boolean removedFromAIDecks = false; private boolean removedFromRandomDecks = false; private DeckHints hints = null; private DeckHints needs = null; - - + /** * Reset all fields to parse next card (to avoid allocating new CardRulesReader N times) */ @@ -260,16 +258,16 @@ public final class CardRules implements ICardCharacteristics { this.faces[1] = null; this.pictureUrl[0] = null; this.pictureUrl[1] = null; - + this.handLife = null; this.altMode = CardSplitType.None; - + this.removedFromAIDecks = false; this.removedFromRandomDecks = false; this.needs = null; this.hints = null; } - + /** * Gets the card. * @@ -285,7 +283,7 @@ public final class CardRules implements ICardCharacteristics { result.setVanguardProperties(handLife); return result; } - + public final CardRules readCard(final Iterable script) { this.reset(); for (String line : script) { @@ -296,8 +294,7 @@ public final class CardRules implements ICardCharacteristics { } return this.getCard(); } - - + /** * Parses the line. * @@ -308,7 +305,7 @@ public final class CardRules implements ICardCharacteristics { int colonPos = line.indexOf(':'); String key = colonPos > 0 ? line.substring(0, colonPos) : line; String value = colonPos > 0 ? line.substring(1+colonPos).trim() : null; - + switch(key.charAt(0)) { case 'A': if ("A".equals(key)) @@ -320,7 +317,7 @@ public final class CardRules implements ICardCharacteristics { this.curFace = 1; } break; - + case 'C': if ("Colors".equals(key)) { // This is forge.card.CardColor not forge.CardColor. @@ -329,7 +326,7 @@ public final class CardRules implements ICardCharacteristics { this.faces[this.curFace].setColor(newCol); } break; - + case 'D': if ("DeckHints".equals(key)) { hints = new DeckHints(value); @@ -337,67 +334,66 @@ public final class CardRules implements ICardCharacteristics { needs = new DeckHints(value); } break; - + case 'H': if ("HandLifeModifier".equals(key)) { handLife = value; } break; - + case 'K': if ("K".equals(key)) { this.faces[this.curFace].addKeyword(value); } break; - + case 'L': if ("Loyalty".equals(key)) { this.faces[this.curFace].setInitialLoyalty(Integer.valueOf(value)); } break; - + case 'M': if ("ManaCost".equals(key)) { - this.faces[this.curFace].setManaCost("no cost".equals(value) ? ManaCost.NO_COST + this.faces[this.curFace].setManaCost("no cost".equals(value) ? ManaCost.NO_COST : new ManaCost(new ManaCostParser(value))); } break; - + case 'N': if ("Name".equals(key)) { this.faces[this.curFace] = new CardFace(value); } break; - + case 'O': if ("Oracle".equals(key)) { this.faces[this.curFace].setOracleText(value); - } break; - + case 'P': if ("PT".equals(key)) { this.faces[this.curFace].setPtText(value); } break; - + case 'R': if ("R".equals(key)) { this.faces[this.curFace].addReplacementEffect(value); } break; - + case 'S': if ("S".equals(key)) { this.faces[this.curFace].addStaticAbility(value); } else if ( "SVar".equals(key) ) { if ( null == value ) throw new IllegalArgumentException("SVar has no variable name"); - + colonPos = value.indexOf(':'); String variable = colonPos > 0 ? value.substring(0, colonPos) : value; value = colonPos > 0 ? value.substring(1+colonPos) : null; - + if ( "RemAIDeck".equals(variable) ) { this.removedFromAIDecks = "True".equalsIgnoreCase(value); } else if ( "RemRandomDeck".equals(variable) ) { @@ -412,7 +408,7 @@ public final class CardRules implements ICardCharacteristics { // deprecated } break; - + case 'T': if ("T".equals(key)) { this.faces[this.curFace].addTrigger(value); @@ -423,21 +419,20 @@ public final class CardRules implements ICardCharacteristics { } break; } - } /** * The Class ParserCardnameTxtManaCost. */ private static class ManaCostParser implements IParserManaCost { - private final StringTokenizer st; + private final StringTokenizer st; private int colorlessCost; - + public ManaCostParser(final String cost) { st = new StringTokenizer(cost, " "); this.colorlessCost = 0; } - + @Override public final int getTotalColorlessCost() { if (this.hasNext()) { @@ -445,7 +440,7 @@ public final class CardRules implements ICardCharacteristics { } return this.colorlessCost; } - + /* * (non-Javadoc) * @@ -455,7 +450,7 @@ public final class CardRules implements ICardCharacteristics { public final boolean hasNext() { return st.hasMoreTokens(); } - + /* * (non-Javadoc) * @@ -463,7 +458,6 @@ public final class CardRules implements ICardCharacteristics { */ @Override public final ManaCostShard next() { - final String unparsed = st.nextToken(); // System.out.println(unparsed); try { @@ -472,10 +466,10 @@ public final class CardRules implements ICardCharacteristics { return null; } catch (NumberFormatException nex) { } - + return ManaCostShard.parseNonGeneric(unparsed); } - + /* * (non-Javadoc) * diff --git a/forge-core/src/main/java/forge/item/PaperCard.java b/forge-core/src/main/java/forge/item/PaperCard.java index b4e3cb09cc4..f75acaeea9e 100644 --- a/forge-core/src/main/java/forge/item/PaperCard.java +++ b/forge-core/src/main/java/forge/item/PaperCard.java @@ -49,7 +49,7 @@ public final class PaperCard implements Comparable, InventoryItemFro public String getName() { return this.name; } - + @Override public String getEdition() { return this.edition; @@ -80,15 +80,11 @@ public final class PaperCard implements Comparable, InventoryItemFro return this.rarity; } - - - // @Override // public String getImageKey() { // return getImageLocator(getImageName(), getArtIndex(), true, false); // } - - + @Override public String getItemType() { return "Card"; @@ -108,12 +104,12 @@ public final class PaperCard implements Comparable, InventoryItemFro public String apply(final PaperCard from) { return from.getName(); } - }; + }; public PaperCard(final CardRules c, final String edition0, final CardRarity rare, final int index) { this(c, edition0, rare, index, false); } - + public PaperCard(final CardRules c, final String edition0, final CardRarity rare, final int index, final boolean foil) { if ( edition0 == null || c == null || rare == null ) throw new IllegalArgumentException("Cannot create card without rules, edition or rarity"); @@ -178,7 +174,6 @@ public final class PaperCard implements Comparable, InventoryItemFro // return String.format("%s|%s", name, cardSet); } - /* * (non-Javadoc) * diff --git a/forge-gui/src/main/java/forge/card/cardfactory/CardStorageReader.java b/forge-gui/src/main/java/forge/card/cardfactory/CardStorageReader.java index d4f2f1667a3..19a4984c8cc 100644 --- a/forge-gui/src/main/java/forge/card/cardfactory/CardStorageReader.java +++ b/forge-gui/src/main/java/forge/card/cardfactory/CardStorageReader.java @@ -6,12 +6,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -47,7 +47,7 @@ import forge.util.FileUtil; *

* CardReader class. *

- * + * * @author Forge * @version $Id$ */ @@ -61,10 +61,10 @@ public class CardStorageReader implements ICardStorageReader { private final boolean useThreadPool = FThreads.isMultiCoreSystem(); public final static int NUMBER_OF_PARTS = 25; - + private final CountDownLatch cdl = new CountDownLatch(NUMBER_OF_PARTS); private final IProgressObserver progressObserver; - + private transient File cardsfolder; private transient ZipFile zip; @@ -78,7 +78,7 @@ public class CardStorageReader implements ICardStorageReader { *

* Constructor for CardReader. *

- * + * * @param theCardsFolder * indicates location of the cardsFolder * @param useZip @@ -86,9 +86,9 @@ public class CardStorageReader implements ICardStorageReader { * exists. */ public CardStorageReader(String cardDataDir, final boolean useZip, IProgressObserver progressObserver) { - this.progressObserver = progressObserver != null ? progressObserver : IProgressObserver.emptyObserver; + this.progressObserver = progressObserver != null ? progressObserver : IProgressObserver.emptyObserver; this.cardsfolder = new File(cardDataDir); - + // These read data for lightweight classes. if (!cardsfolder.exists()) { throw new RuntimeException("CardReader : constructor error -- " + cardsfolder.getAbsolutePath() + " file/folder not found."); @@ -98,8 +98,6 @@ public class CardStorageReader implements ICardStorageReader { throw new RuntimeException("CardReader : constructor error -- not a directory -- " + cardsfolder.getAbsolutePath()); } - - final File zipFile = new File(cardsfolder, "cardsfolder.zip"); if (useZip && zipFile.exists()) { @@ -115,9 +113,8 @@ public class CardStorageReader implements ICardStorageReader { } // CardReader() private final List loadCardsInRange(final List files, int from, int to) { - CardRules.Reader rulesReader = new CardRules.Reader(); - + List result = new ArrayList(); for(int i = from; i < to; i++) { File cardTxtFile = files.get(i); @@ -125,11 +122,10 @@ public class CardStorageReader implements ICardStorageReader { } return result; } - + private final List loadCardsInRangeFromZip(final List files, int from, int to) { - CardRules.Reader rulesReader = new CardRules.Reader(); - + List result = new ArrayList(); for(int i = from; i < to; i++) { ZipEntry ze = files.get(i); @@ -137,15 +133,14 @@ public class CardStorageReader implements ICardStorageReader { result.add(this.loadCard(rulesReader, ze)); } return result; - } - - + } + /** * Starts reading cards into memory until the given card is found. - * + * * After that, we save our place in the list of cards (on disk) in case we * need to load more. - * + * * @return the Card or null if it was not found. */ public final List loadCards() { @@ -154,7 +149,7 @@ public class CardStorageReader implements ICardStorageReader { final List>> tasks; long estimatedFilesRemaining; - + // Iterate through txt files or zip archive. // Report relevant numbers to progress monitor model. if (this.zip == null) { @@ -162,7 +157,6 @@ public class CardStorageReader implements ICardStorageReader { estimatedFilesRemaining = allFiles.size(); tasks = makeTaskListForFiles(allFiles); } else { - estimatedFilesRemaining = this.zip.size(); ZipEntry entry; List entries = new ArrayList(); @@ -180,9 +174,9 @@ public class CardStorageReader implements ICardStorageReader { StopWatch sw = new StopWatch(); sw.start(); - + List res = executeLoadTask(tasks); - + sw.stop(); final long timeOnParse = sw.getTime(); System.out.printf("Read cards: %s %s in %d ms (%d parts) %s%n", estimatedFilesRemaining, zip == null? "files" : "archived files", timeOnParse, NUMBER_OF_PARTS, useThreadPool ? "using thread pool" : "in same thread"); @@ -215,13 +209,13 @@ public class CardStorageReader implements ICardStorageReader { } catch (Exception e) { // this clause comes from non-threaded branch throw new RuntimeException(e); } - + return result; } - + private List>> makeTaskListForZip(final List entries) { int totalFiles = entries.size(); - int filesPerPart = totalFiles / NUMBER_OF_PARTS; + int filesPerPart = totalFiles / NUMBER_OF_PARTS; final List>> tasks = new ArrayList>>(); for (int iPart = 0; iPart < NUMBER_OF_PARTS; iPart++) { final int from = iPart * filesPerPart; @@ -241,7 +235,7 @@ public class CardStorageReader implements ICardStorageReader { private List>> makeTaskListForFiles(final List allFiles) { int totalFiles = allFiles.size(); - int filesPerPart = totalFiles / NUMBER_OF_PARTS; + int filesPerPart = totalFiles / NUMBER_OF_PARTS; final List>> tasks = new ArrayList>>(); for (int iPart = 0; iPart < NUMBER_OF_PARTS; iPart++) { final int from = iPart * filesPerPart; @@ -257,8 +251,8 @@ public class CardStorageReader implements ICardStorageReader { }); } return tasks; - } - + } + public static List collectCardFiles(List accumulator, File startDir) { String[] list = startDir.list(); for (String filename : list) { @@ -282,10 +276,10 @@ public class CardStorageReader implements ICardStorageReader { *

* load a card. *

- * + * * @param inputStream * the stream from which to load the card's information - * + * * @return the card loaded from the stream */ protected final CardRules loadCard(CardRules.Reader reader, final InputStream inputStream) { @@ -299,10 +293,10 @@ public class CardStorageReader implements ICardStorageReader { /** * Load a card from a txt file. - * + * * @param pathToTxtFile * the full or relative path to the file to load - * + * * @return a new Card instance */ protected final CardRules loadCard(final CardRules.Reader reader, final File file) { @@ -326,10 +320,10 @@ public class CardStorageReader implements ICardStorageReader { /** * Load a card from an entry in a zip file. - * + * * @param entry * to load from - * + * * @return a new Card instance */ protected final CardRules loadCard(final CardRules.Reader rulesReader, final ZipEntry entry) {