From c898e1321fa97b51e82f3252e59ce4fa1310c1e8 Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Wed, 27 Nov 2013 07:46:21 +0000 Subject: [PATCH] cardreader fix --- .../main/java/forge/CardStorageReader.java | 63 +++++++++---------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/forge-core/src/main/java/forge/CardStorageReader.java b/forge-core/src/main/java/forge/CardStorageReader.java index cbf9dd08655..cb3911172a0 100644 --- a/forge-core/src/main/java/forge/CardStorageReader.java +++ b/forge-core/src/main/java/forge/CardStorageReader.java @@ -164,22 +164,38 @@ public class CardStorageReader { public final Iterable loadCards() { progressObserver.setOperationName("Loading cards, examining folder", true); - long estimatedFilesRemaining; // Iterate through txt files or zip archive. // Report relevant numbers to progress monitor model. - final List allFiles = collectCardFiles(new ArrayList(), this.cardsfolder); - estimatedFilesRemaining = allFiles.size(); - int fileParts = zip == null ? NUMBER_OF_PARTS : 1 + NUMBER_OF_PARTS / 3; - if( allFiles.size() < fileParts * 100) - fileParts = allFiles.size() / 100; // to avoid creation of many threads for a dozen of files - final CountDownLatch cdlFiles = new CountDownLatch(fileParts); - List>> taskFiles = makeTaskListForFiles(allFiles, cdlFiles); - final CountDownLatch cdlZip = new CountDownLatch(NUMBER_OF_PARTS); - List>> taskZip = new ArrayList<>(); + Set result = new TreeSet(new Comparator() { + @Override + public int compare(CardRules o1, CardRules o2) { + return String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName()); + } + }); + + final List allFiles = collectCardFiles(new ArrayList(), this.cardsfolder); + if(!allFiles.isEmpty()) { + int fileParts = zip == null ? NUMBER_OF_PARTS : 1 + NUMBER_OF_PARTS / 3; + if( allFiles.size() < fileParts * 100) + fileParts = allFiles.size() / 100; // to avoid creation of many threads for a dozen of files + final CountDownLatch cdlFiles = new CountDownLatch(fileParts); + List>> taskFiles = makeTaskListForFiles(allFiles, cdlFiles); + progressObserver.setOperationName("Loading cards from folders", true); + progressObserver.report(0, taskFiles.size()); + StopWatch sw = new StopWatch(); + sw.start(); + executeLoadTask(result, taskFiles, cdlFiles); + sw.stop(); + final long timeOnParse = sw.getTime(); + System.out.printf("Read cards: %s files in %d ms (%d parts) %s%n", allFiles.size(), timeOnParse, taskFiles.size(), useThreadPool ? "using thread pool" : "in same thread"); + } + if( this.zip != null ) { - estimatedFilesRemaining = this.zip.size(); + final CountDownLatch cdlZip = new CountDownLatch(NUMBER_OF_PARTS); + List>> taskZip = new ArrayList<>(); + ZipEntry entry; List entries = new ArrayList(); // zipEnum was initialized in the constructor. @@ -192,27 +208,6 @@ public class CardStorageReader { } taskZip = makeTaskListForZip(entries, cdlZip); - } - - Set result = new TreeSet(new Comparator() { - @Override - public int compare(CardRules o1, CardRules o2) { - return String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName()); - } - }); - - if( taskFiles.size() > 0 ) { - progressObserver.setOperationName("Loading cards from folders", true); - progressObserver.report(0, taskFiles.size()); - StopWatch sw = new StopWatch(); - sw.start(); - executeLoadTask(result, taskFiles, cdlFiles); - sw.stop(); - final long timeOnParse = sw.getTime(); - System.out.printf("Read cards: %s files in %d ms (%d parts) %s%n", estimatedFilesRemaining, timeOnParse, taskFiles.size(), useThreadPool ? "using thread pool" : "in same thread"); - } - - if( taskZip.size() > 0 ) { progressObserver.setOperationName("Loading cards from archive", true); progressObserver.report(0, taskZip.size()); StopWatch sw = new StopWatch(); @@ -220,9 +215,9 @@ public class CardStorageReader { executeLoadTask(result, taskZip, cdlZip); sw.stop(); final long timeOnParse = sw.getTime(); - System.out.printf("Read cards: %s archived files in %d ms (%d parts) %s%n", estimatedFilesRemaining, timeOnParse, taskZip.size(), useThreadPool ? "using thread pool" : "in same thread"); + System.out.printf("Read cards: %s archived files in %d ms (%d parts) %s%n", this.zip.size(), timeOnParse, taskZip.size(), useThreadPool ? "using thread pool" : "in same thread"); } - + return result; } // loadCardsUntilYouFind(String)