diff --git a/.gitattributes b/.gitattributes index 4b65d4b9868..22a8d61c0c1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12410,8 +12410,6 @@ src/main/java/forge/view/bazaar/package-info.java svneol=native#text/plain src/main/java/forge/view/package-info.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/BaseProgressMonitor.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/BraidsProgressMonitor.java svneol=native#text/plain -src/main/java/net/slightlymagic/braids/FindNonDirectoriesSkipDotDirectoriesGenerator.java svneol=native#text/plain -src/main/java/net/slightlymagic/braids/GeneratorFunctions.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/LICENSE.txt svneol=native#text/plain src/main/java/net/slightlymagic/braids/package-info.java svneol=native#text/plain src/main/java/tree/properties/PropertyElement.java svneol=native#text/plain diff --git a/src/main/java/forge/CardReader.java b/src/main/java/forge/CardReader.java index cf670ce1ae8..4e75cf6662f 100644 --- a/src/main/java/forge/CardReader.java +++ b/src/main/java/forge/CardReader.java @@ -35,11 +35,6 @@ import java.util.zip.ZipFile; import javax.swing.SwingUtilities; -import net.slightlymagic.braids.FindNonDirectoriesSkipDotDirectoriesGenerator; -import net.slightlymagic.braids.GeneratorFunctions; - -import com.google.code.jyield.Generator; -import com.google.code.jyield.YieldUtils; import forge.card.CardManaCost; import forge.card.CardRules; @@ -119,7 +114,6 @@ public class CardReader implements Runnable { // PM UNKNOWN_NUMBER_OF_FILES_REMAINING; - private transient Iterable findNonDirsIterable; // 8/18/11 10:56 PM @@ -222,27 +216,6 @@ public class CardReader implements Runnable { } // CardReader() - /** - * This finalizer helps assure there is no memory or thread leak with - * findNonDirsIterable, which was created with YieldUtils.toIterable. - * - * @throws Throwable - * indirectly - */ - @Override - protected final void finalize() throws Throwable { - try { - if (this.findNonDirsIterable != null) { - for (@SuppressWarnings("unused") - final// Do nothing; just exercising the Iterable. - File ignored : this.findNonDirsIterable) { - } - } - } finally { - super.finalize(); - } - } - /** * Reads the rest of ALL the cards into memory. This is not lazy. */ @@ -269,11 +242,11 @@ public class CardReader implements Runnable { // Iterate through txt files or zip archive. // Report relevant numbers to progress monitor model. if (this.zip == null) { + List allFiles = new ArrayList(); if (this.estimatedFilesRemaining == CardReader.UNKNOWN_NUMBER_OF_FILES_REMAINING) { - final Generator findNonDirsGen = new FindNonDirectoriesSkipDotDirectoriesGenerator( - this.cardsfolder); - this.estimatedFilesRemaining = GeneratorFunctions.estimateSize(findNonDirsGen); - this.findNonDirsIterable = YieldUtils.toIterable(findNonDirsGen); + fillFilesArray(allFiles, this.cardsfolder); + this.estimatedFilesRemaining = allFiles.size(); + } if (barProgress != null) { @@ -286,7 +259,7 @@ public class CardReader implements Runnable { }); } - for (final File cardTxtFile : this.findNonDirsIterable) { + for (final File cardTxtFile : allFiles) { if (!cardTxtFile.getName().endsWith(CardReader.CARD_FILE_DOT_EXTENSION)) { barProgress.increment(); continue; @@ -326,6 +299,26 @@ public class CardReader implements Runnable { return result; } // loadCardsUntilYouFind(String) + /** + * TODO: Write javadoc for this method. + * @param allFiles + * @param cardsfolder2 + */ + private void fillFilesArray(List allFiles, File startDir) { + String[] list = startDir.list(); + for (String filename : list) { + File entry = new File(startDir, filename); + + if (!entry.isDirectory()) { + allFiles.add(entry); + continue; + } + if (filename.startsWith(".")) continue; + + fillFilesArray(allFiles, entry); + } + } + /** *

* addTypes to an existing card. diff --git a/src/main/java/forge/NameChanger.java b/src/main/java/forge/NameChanger.java index 2863b01909a..35d468c7d87 100644 --- a/src/main/java/forge/NameChanger.java +++ b/src/main/java/forge/NameChanger.java @@ -24,10 +24,9 @@ import java.util.Map.Entry; import java.util.StringTokenizer; import java.util.TreeMap; -import net.slightlymagic.braids.GeneratorFunctions; - import com.google.code.jyield.Generator; import com.google.code.jyield.YieldUtils; +import com.google.code.jyield.Yieldable; import forge.card.spellability.SpellAbility; import forge.error.ErrorViewer; @@ -106,8 +105,30 @@ public class NameChanger { }; }; - return GeneratorFunctions.transformGenerator(transform, inputGenerator); + return transformGenerator(transform, inputGenerator); } + + public static Generator transformGenerator(final Lambda1 transform, final Generator inputGenerator) + { + Generator result = new Generator() { + + @Override + public void generate(final Yieldable outputYield) { + + Yieldable inputYield = new Yieldable() { + @Override + public void yield(final T input) { + outputYield.yield(transform.apply(input)); + } + }; + + inputGenerator.generate(inputYield); + } + + }; + + return result; + } // changes card name, getText(), and all SpellAbility getStackDescription() // and toString() diff --git a/src/main/java/net/slightlymagic/braids/FindNonDirectoriesSkipDotDirectoriesGenerator.java b/src/main/java/net/slightlymagic/braids/FindNonDirectoriesSkipDotDirectoriesGenerator.java deleted file mode 100644 index 1aa44a19d03..00000000000 --- a/src/main/java/net/slightlymagic/braids/FindNonDirectoriesSkipDotDirectoriesGenerator.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * The files in the directory "net/slightlymagic/braids" and in all subdirectories of it (the "Files") are - * Copyright 2011 Braids Cabal-Conjurer. They are available under either Forge's - * main license (the GNU Public License; see LICENSE.txt in Forge's top directory) - * or under the Apache License, as explained below. - * - * The Files are additionally licensed under the Apache License, Version 2.0 (the - * "Apache License"); you may not use the files in this directory except in - * compliance with one of its two licenses. You may obtain a copy of the Apache - * License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Apache License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Apache License for the specific language governing permissions and - * limitations under the Apache License. - * - */ -package net.slightlymagic.braids; - -import java.io.File; - -import com.google.code.jyield.Generator; -import com.google.code.jyield.Yieldable; - -/** - * This is a generator over all of the non-directories residing in a given - * starting directory and all subdirectories of it that do NOT start with a dot; - * this prevents the code from descending into .svn directories. - * - * For documentation on Java-Yield and its generators, see - * {@link com.google.code.jyield.Generator} - */ -public class FindNonDirectoriesSkipDotDirectoriesGenerator implements Generator { - private File startDir; - - /** - * Create a generator at a given starting directory. - * - * One can invoke this generator more than once by calling its generate - * method. - * - * @param startDir - * the directory to start in; we ignore this directory's name, so - * if it starts with a dot, we treat it as if it didn't. - */ - public FindNonDirectoriesSkipDotDirectoriesGenerator(final File startDir) { - this.startDir = startDir; - } - - /** - * Standard generate method. - * - *

- * Yields results to the given Yieldable. Convert Generator instances to - * Iterables with YieldUtils.toIterable. - *

- * - * See - * - * @param yy the yy - * {@link com.google.code.jyield.YieldUtils#toIterable(com.google.code.jyield.Generator)} - */ - public final void generate(final Yieldable yy) { - String[] list = startDir.list(); - - for (String filename : list) { - File entry = new File(startDir, filename); - - if (entry.isDirectory()) { - if (!filename.startsWith(".")) { - FindNonDirectoriesSkipDotDirectoriesGenerator child = - new FindNonDirectoriesSkipDotDirectoriesGenerator( - entry); - child.generate(yy); - child = null; - } - // else do nothing, because it's a dot directory - } else { - // Use this instead of a return statement. - yy.yield(entry); - } - } - } -} diff --git a/src/main/java/net/slightlymagic/braids/GeneratorFunctions.java b/src/main/java/net/slightlymagic/braids/GeneratorFunctions.java deleted file mode 100644 index 7e956a2f1f5..00000000000 --- a/src/main/java/net/slightlymagic/braids/GeneratorFunctions.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * The files in the directory "net/slightlymagic/braids" and in all subdirectories of it (the "Files") are - * Copyright 2011 Braids Cabal-Conjurer. They are available under either Forge's - * main license (the GNU Public License; see LICENSE.txt in Forge's top directory) - * or under the Apache License, as explained below. - * - * The Files are additionally licensed under the Apache License, Version 2.0 (the - * "Apache License"); you may not use the files in this directory except in - * compliance with one of its two licenses. You may obtain a copy of the Apache - * License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Apache License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Apache License for the specific language governing permissions and - * limitations under the Apache License. - * - */ -package net.slightlymagic.braids; - -import java.util.ArrayList; -import java.util.NoSuchElementException; - - -import com.google.code.jyield.Generator; -import com.google.code.jyield.YieldUtils; -import com.google.code.jyield.Yieldable; - -import forge.util.closures.Lambda1; - -/** - * For documentation on Java-Yield and its generators, see. - * - * {@link com.google.code.jyield.Generator}. - */ -public final class GeneratorFunctions { - - /** - * Do not instantiate. - */ - private GeneratorFunctions() { - } - - /** - * Estimate the number of items in this generator by traversing all of its - * elements. - * - * Note this only works on a generator that can be reinstantiated once it - * has been traversed. This is only an estimate, because a generator's size - * may vary been traversals. This is especially true if the generator relies - * on external resources, such as a file system. - * - * If you call this on an infinite generator, this method will never return. - * - * @param - * the generic type - * @param gen - * the gen - * @return the estimated number of items provided by this generator - */ - public static long estimateSize(final Generator gen) { - long result = 0; - for (@SuppressWarnings("unused") - T ignored : YieldUtils.toIterable(gen)) { - result++; - } - - return result; - } - - - /** - * Highly efficient means of applying a transform to a long or infinite - * sequence. - * - * @param - * any type - * - * @param transform - * a Lambda (function) whose apply method takes an object of type - * and returns an object of the same type. This transforms - * the values from the inputGenerator into this Generator. - * - * @param inputGenerator - * the sequence upon which we operate - * - * @return a generator that yields transform.apply's return value for each - * item in the inputGenerator - */ - public static Generator transformGenerator(final Lambda1 transform, final Generator inputGenerator) - { - Generator result = new Generator() { - - @Override - public void generate(final Yieldable outputYield) { - - Yieldable inputYield = new Yieldable() { - @Override - public void yield(final T input) { - outputYield.yield(transform.apply(input)); - } - }; - - inputGenerator.generate(inputYield); - } - - }; - - return result; - } - - /** - * Forces a generator to be completely evaluated into a temporary data - * structure, then returns the generator over that same structure. - * - * This effectively returns the same Generator, but it is a faster one. This - * trades away heap space for reduced CPU intensity. This is particuarly - * helpful if you know that a Generator is going to be totally evaluated - * more than once in the near future. - * - * @param - * inferred automatically - * - * @param unevaluated - * a Generator of T instances - * - * @return the equivalent Generator, except that the result's generate - * method can be invoked multiple times for fast results. - */ - public static Generator solidify(final Generator unevaluated) { - ArrayList solidTmp = YieldUtils.toArrayList(unevaluated); - solidTmp.trimToSize(); - return YieldUtils.toGenerator(solidTmp); - } - - /** - * Select an item at random from a Generator; this causes the entire - * Generator to be evaluated once, but only once. - * - * @param - * the generic type - * @param generator - * the generator from which to select a random item - * @return an item chosen at random from the generator; this may be null, if - * the generator contains null items. - * if the generator has no contents - */ - public static T selectRandom(final Generator generator) { - /* - * This algorithm requires some explanation. Each time we encounter a - * new item from the generator, we determine via random chance if the - * item is the one we select. At the end of each iteration, we have a - * candidate, and we have a count of the number of items encountered so - * far. Each iteration has a 1/n chance of replacing the candidate with - * the current item, where n is the number of items encountered so far. - * This allows us to randomly select an item from the generated contents - * with an equal distribution; and we don't have to count the number of - * items first! - */ - - int n = 0; - T candidate = null; - - for (T item : YieldUtils.toIterable(generator)) { - n++; - int rand = (int) (Math.random() * n); - // At this point, 0 <= rand < n. - rand++; // Now, 1 <= rand <= n. - - if (rand == 1) { - // We rolled a 1 on an n-sided die. We have a new candidate! - // Note that on the first iteration, this always happens, - // because n = 1. - candidate = item; - } - } - - if (n == 0) { - // There were no items in the generator! - throw new NoSuchElementException("generator is empty"); - } - - return candidate; - } -}