diff --git a/.gitattributes b/.gitattributes index 33545ed629d..3eb471b3a27 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11300,7 +11300,6 @@ src/main/java/forge/CardCharactersticName.java -text src/main/java/forge/CardColor.java svneol=native#text/plain src/main/java/forge/CardContainer.java svneol=native#text/plain src/main/java/forge/CardDamageHistory.java -text -src/main/java/forge/CardFilter.java svneol=native#text/plain src/main/java/forge/CardKeywords.java -text svneol=native#text/plain src/main/java/forge/CardList.java svneol=native#text/plain src/main/java/forge/CardListFilter.java svneol=native#text/plain @@ -11847,7 +11846,6 @@ src/main/java/net/slightlymagic/braids/util/ClumsyRunnable.java svneol=native#te src/main/java/net/slightlymagic/braids/util/ImmutableIterableFrom.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/util/UtilFunctions.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/util/generator/FindNonDirectoriesSkipDotDirectoriesGenerator.java svneol=native#text/plain -src/main/java/net/slightlymagic/braids/util/generator/GeneratorFromArray.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/util/generator/GeneratorFunctions.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/util/generator/package-info.java svneol=native#text/plain src/main/java/net/slightlymagic/braids/util/package-info.java svneol=native#text/plain @@ -11876,7 +11874,6 @@ src/test/java/forge/PanelTest.java svneol=native#text/plain src/test/java/forge/PhaseHandlerTest.java -text src/test/java/forge/RunTest.java svneol=native#text/plain src/test/java/forge/TinyTest.java svneol=native#text/plain -src/test/java/forge/card/cardFactory/CardFactoryUtilTest.java svneol=native#text/plain src/test/java/forge/card/mana/ManaPartTest.java svneol=native#text/plain src/test/java/forge/deck/generate/Generate2ColorDeckTest.java svneol=native#text/plain src/test/java/forge/deck/generate/Generate3ColorDeckTest.java svneol=native#text/plain diff --git a/src/main/java/forge/CardFilter.java b/src/main/java/forge/CardFilter.java deleted file mode 100644 index 17a559f0a2e..00000000000 --- a/src/main/java/forge/CardFilter.java +++ /dev/null @@ -1,367 +0,0 @@ -/* - * Forge: Play Magic: the Gathering. - * Copyright (C) 2011 Forge Team - * - * This program is free software: you can redistribute it and/or modify - * 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 . - */ -package forge; - -import java.util.List; - -import net.slightlymagic.braids.util.UtilFunctions; -import net.slightlymagic.braids.util.generator.GeneratorFunctions; - -import com.google.code.jyield.Generator; - -import forge.card.EditionInfo; -import forge.util.Lambda1; - -/** - *

- * CardFilter class. - *

- * - * @author Forge - * @version $Id$ - */ -public class CardFilter { - - /** - * Filter a sequence (iterable) of cards to a list of equal or smaller size - * whose names contain the given substring. - * - * We perform the substring search without sensitivity to case. - * - * @param toBeFiltered - * an {@link java.lang.Iterable} of Card instances - * @param substring - * a {@link java.lang.String} object. - * @return a {@link forge.CardList} object. - */ - public final CardList cardListNameFilter(final Iterable toBeFiltered, final String substring) { - String s; - - final CardList listFilter = new CardList(); - for (final Card card : toBeFiltered) { - s = card.getName().toLowerCase(); - - if (s.indexOf(substring.toLowerCase()) >= 0) { - listFilter.add(card); - - } - - } - - return listFilter; - } - - /** - *

- * cardListTextFilter. - *

- * - * @param all - * a {@link forge.CardList} object. - * @param name - * a {@link java.lang.String} object. - * @return a {@link forge.CardList} object. - */ - public final CardList cardListTextFilter(final CardList all, final String name) { - Card cardName; - String s; - s = ""; - final CardList listFilter = new CardList(); - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - s = cardName.getText().toLowerCase(); - - if (s.indexOf(name.toLowerCase()) >= 0) { - listFilter.add(cardName); - - } - - } - - return listFilter; - } - - /** - *

- * cardListColorFilter. - *

- * - * @param all - * a {@link forge.CardList} object. - * @param name - * a {@link java.lang.String} object. - * @return a {@link forge.CardList} object. - */ - public final CardList cardListColorFilter(final CardList all, final String name) { - Card cardName = new Card(); - final CardList listFilter = new CardList(); - - if (name == "black") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!CardUtil.getColors(cardName).contains(Constant.Color.BLACK)) { - listFilter.add(cardName); - } - - } - } - - if (name == "blue") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!CardUtil.getColors(cardName).contains(Constant.Color.BLUE)) { - listFilter.add(cardName); - } - - } - } - - if (name == "green") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!CardUtil.getColors(cardName).contains(Constant.Color.GREEN)) { - listFilter.add(cardName); - } - - } - } - - if (name == "red") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!CardUtil.getColors(cardName).contains(Constant.Color.RED)) { - listFilter.add(cardName); - } - - } - } - - if (name == "white") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!CardUtil.getColors(cardName).contains(Constant.Color.WHITE)) { - listFilter.add(cardName); - } - - } - } - - if (name.equals("colorless")) { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!CardUtil.getColors(cardName).contains(Constant.Color.COLORLESS)) { - listFilter.add(cardName); - } - - } - } - - return listFilter; - } - - /** - *

- * cardListTypeFilter. - *

- * - * @param all - * a {@link forge.CardList} object. - * @param name - * a {@link java.lang.String} object. - * @return a {@link forge.CardList} object. - */ - public final CardList cardListTypeFilter(final CardList all, final String name) { - Card cardName = new Card(); - final CardList listFilter = new CardList(); - - if (name == "artifact") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!cardName.isArtifact()) { - listFilter.add(cardName); - } - - } - } - - if (name == "creature") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!cardName.isCreature()) { - listFilter.add(cardName); - } - - } - } - - if (name == "enchantment") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!cardName.isEnchantment()) { - listFilter.add(cardName); - } - - } - } - - if (name == "instant") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!cardName.isInstant()) { - listFilter.add(cardName); - } - - } - } - - if (name == "land") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!cardName.isLand()) { - listFilter.add(cardName); - } - - } - } - - if (name == "planeswalker") { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!cardName.isPlaneswalker()) { - listFilter.add(cardName); - } - - } - } - - if (name.equals("sorcery")) { - for (int i = 0; i < all.size(); i++) { - cardName = all.getCard(i); - if (!cardName.isSorcery()) { - listFilter.add(cardName); - } - - } - } - - return listFilter; - } - - /** - * Filter an iterable sequence of Cards; note this is a static method that - * is very similar to the non-static one. - * - * @param iterable - * the sequence of cards to examine - * - * @param filt - * determines which cards are present in the resulting list - * - * @return a list of Cards that meet the filtering criteria; may be empty, - * but never null - */ - public static CardList filter(final Iterable iterable, final CardListFilter filt) { - final CardList result = new CardList(); - for (final Card card : iterable) { - if (filt.addCard(card)) { - result.add(card); - } - } - - return result; - } - - /** - * Filter a Generator of Cards based on their colors; this does not cause - * the generator to be evaluated, but rather defers the filtering to when - * the result's generate method is called (e.g., by YieldUtils.toIterable). - * - * @param inputGenerator - * the sequence to filter; must not be null - * - * @param cardColor - * a {@link java.lang.String} object; "Multicolor" is also - * accepted. Must not be null. - * - * @return a new Generator containing cards only of the desired color or - * multicolored cards. - */ - public static Generator getColor(final Generator inputGenerator, final String cardColor) { - UtilFunctions.checkNotNull("inputGenerator", inputGenerator); - UtilFunctions.checkNotNull("cardColor", cardColor); - - final boolean weWantMulticolor = cardColor.equals("Multicolor"); - - final Lambda1 predicate = new Lambda1() { - @Override - public Boolean apply(final Card c) { - if (c == null) { - return false; - } - - if (weWantMulticolor && (c.getColor() != null) && (c.getColor().size() > 1)) { - return true; - } else if (c.isColor(cardColor) && (c.getColor() != null) && (c.getColor().size() == 1)) { - return true; - } - - return false; - } - }; - - return GeneratorFunctions.filterGenerator(predicate, inputGenerator); - } // getColor() - - /** - * Filter a Generator of cards so that it contains only the ones that exist - * in certain sets. - * - * @param inputGenerator - * a sequence Generator of Card instances; must not be null. - * - * @param sets - * an ArrayList of Strings identifying the valid sets; must not - * be null. - * - * @return a {@link forge.CardList} object. - */ - public static Generator getSets(final Generator inputGenerator, final List sets) { - UtilFunctions.checkNotNull("inputGenerator", inputGenerator); - UtilFunctions.checkNotNull("sets", sets); - - final Lambda1 predicate = new Lambda1() { - @Override - public Boolean apply(final Card c) { - if (c == null) { - return false; - } - - for (final EditionInfo set : c.getSets()) { - if ((set != null) && sets.contains(set.toString())) { - return true; - } - } - - return false; - } - }; - - return GeneratorFunctions.filterGenerator(predicate, inputGenerator); - } // getSets(Generator,ArrayList) - -} diff --git a/src/main/java/forge/CardList.java b/src/main/java/forge/CardList.java index af7f3fe22f7..4264754ce4d 100644 --- a/src/main/java/forge/CardList.java +++ b/src/main/java/forge/CardList.java @@ -635,7 +635,13 @@ public class CardList implements Iterable { * criteria; may be empty, but never null. */ public final CardList filter(final CardListFilter filt) { - return CardFilter.filter(this, filt); + final CardList result = new CardList(); + for (final Card card : this) { + if (filt.addCard(card)) { + result.add(card); + } + } + return result; } /** diff --git a/src/main/java/forge/deck/generate/Generate2ColorDeck.java b/src/main/java/forge/deck/generate/Generate2ColorDeck.java index 1da09ab9fae..9f19725bae5 100644 --- a/src/main/java/forge/deck/generate/Generate2ColorDeck.java +++ b/src/main/java/forge/deck/generate/Generate2ColorDeck.java @@ -24,7 +24,6 @@ import java.util.Random; import forge.AllZone; import forge.Card; -import forge.CardFilter; import forge.CardList; import forge.CardListFilter; import forge.Constant; @@ -34,6 +33,7 @@ import forge.error.ErrorViewer; import forge.properties.ForgePreferences.FPref; import forge.properties.ForgeProps; import forge.util.MyRandom; +import forge.util.Predicate; /** *

@@ -134,16 +134,10 @@ public class Generate2ColorDeck { // start with all cards // remove cards that generated decks don't like - final CardList allCards = CardFilter.filter(AllZone.getCardFactory(), new CardListFilter() { - @Override - public boolean addCard(final Card c) { - if (c.getSVar("RemRandomDeck").equals("True")) { - return false; - } - return (!c.getSVar("RemAIDeck").equals("True") || ((pt != null) && pt.equals(PlayerType.HUMAN))); - } - }); - + + Predicate toUse = pt == PlayerType.HUMAN ? GenerateDeckUtil.humanCanPlay : GenerateDeckUtil.aiCanPlay; + CardList allCards = new CardList(toUse.select(AllZone.getCardFactory())); + // reduce to cards that match the colors CardList cl1 = allCards.getColor(this.color1); if (!Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_ARTIFACTS)) { diff --git a/src/main/java/forge/deck/generate/Generate3ColorDeck.java b/src/main/java/forge/deck/generate/Generate3ColorDeck.java index 4e9b9e79501..7397b16fefa 100644 --- a/src/main/java/forge/deck/generate/Generate3ColorDeck.java +++ b/src/main/java/forge/deck/generate/Generate3ColorDeck.java @@ -24,7 +24,6 @@ import java.util.Random; import forge.AllZone; import forge.Card; -import forge.CardFilter; import forge.CardList; import forge.CardListFilter; import forge.Constant; @@ -34,6 +33,7 @@ import forge.error.ErrorViewer; import forge.properties.ForgePreferences.FPref; import forge.properties.ForgeProps; import forge.util.MyRandom; +import forge.util.Predicate; /** *

@@ -146,16 +146,9 @@ public class Generate3ColorDeck { // start with all cards // remove cards that generated decks don't like - final CardList allCards = CardFilter.filter(AllZone.getCardFactory(), new CardListFilter() { - @Override - public boolean addCard(final Card c) { - if (c.getSVar("RemRandomDeck").equals("True")) { - return false; - } - return (!c.getSVar("RemAIDeck").equals("True") || ((pt != null) && pt.equals(PlayerType.HUMAN))); - } - }); - + Predicate toUse = pt == PlayerType.HUMAN ? GenerateDeckUtil.humanCanPlay : GenerateDeckUtil.aiCanPlay; + CardList allCards = new CardList(toUse.select(AllZone.getCardFactory())); + // reduce to cards that match the colors CardList cl1 = allCards.getColor(this.color1); if (!Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_ARTIFACTS)) { diff --git a/src/main/java/forge/deck/generate/Generate5ColorDeck.java b/src/main/java/forge/deck/generate/Generate5ColorDeck.java index b621549cdbd..ff72dbebfd9 100644 --- a/src/main/java/forge/deck/generate/Generate5ColorDeck.java +++ b/src/main/java/forge/deck/generate/Generate5ColorDeck.java @@ -24,7 +24,6 @@ import java.util.Random; import forge.AllZone; import forge.Card; -import forge.CardFilter; import forge.CardList; import forge.CardListFilter; import forge.Constant; @@ -34,6 +33,7 @@ import forge.error.ErrorViewer; import forge.properties.ForgeProps; import forge.properties.ForgePreferences.FPref; import forge.util.MyRandom; +import forge.util.Predicate; /** *

@@ -144,16 +144,9 @@ public class Generate5ColorDeck { // start with all cards // remove cards that generated decks don't like - final CardList allCards = CardFilter.filter(AllZone.getCardFactory(), new CardListFilter() { - @Override - public boolean addCard(final Card c) { - if (c.getSVar("RemRandomDeck").equals("True")) { - return false; - } - return (!c.getSVar("RemAIDeck").equals("True") || ((playerType != null) && playerType.equals(PlayerType.HUMAN))); - } - }); - + Predicate toUse = playerType == PlayerType.HUMAN ? GenerateDeckUtil.humanCanPlay : GenerateDeckUtil.aiCanPlay; + CardList allCards = new CardList(toUse.select(AllZone.getCardFactory())); + // reduce to cards that match the colors CardList cardList1 = allCards.getColor(this.color1); if (!Singletons.getModel().getPreferences().getPrefBoolean(FPref.DECKGEN_ARTIFACTS)) { diff --git a/src/main/java/forge/deck/generate/GenerateDeckUtil.java b/src/main/java/forge/deck/generate/GenerateDeckUtil.java index 64757707f02..02f518ead34 100644 --- a/src/main/java/forge/deck/generate/GenerateDeckUtil.java +++ b/src/main/java/forge/deck/generate/GenerateDeckUtil.java @@ -19,6 +19,9 @@ package forge.deck.generate; import java.util.ArrayList; +import forge.Card; +import forge.util.Predicate; + /** *

* GenerateDeckUtil class. @@ -29,6 +32,19 @@ import java.util.ArrayList; */ public class GenerateDeckUtil { + public static final Predicate aiCanPlay = new Predicate(){ + @Override + public boolean isTrue(Card c) { + return !c.getSVar("RemRandomDeck").equals("True") && !c.getSVar("RemAIDeck").equals("True"); + } + }; + + public static final Predicate humanCanPlay = new Predicate(){ + @Override + public boolean isTrue(Card c) { + return !c.getSVar("RemRandomDeck").equals("True"); + } + }; /** * * Arrays of dual and tri-land cards. diff --git a/src/main/java/net/slightlymagic/braids/util/generator/GeneratorFromArray.java b/src/main/java/net/slightlymagic/braids/util/generator/GeneratorFromArray.java deleted file mode 100644 index e9e4f0a9bec..00000000000 --- a/src/main/java/net/slightlymagic/braids/util/generator/GeneratorFromArray.java +++ /dev/null @@ -1,66 +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.util.generator; - -import com.google.code.jyield.Generator; -import com.google.code.jyield.Yieldable; - -/** - * Creates a Generator from an array; generators are a handy substitute for - * passing around and creating temporary lists, collections, and arrays. - * - * @param - * the generic type {@link com.google.code.jyield.Generator} - */ -public class GeneratorFromArray implements Generator { - private T[] array; - - /** - * Create a Generator from an array. - * - * @param array - * from which to generate items - */ - public GeneratorFromArray(final T[] array) { - this.array = array; - } - - /* - * (non-Javadoc) - * - * @see - * com.google.code.jyield.Generator#generate(com.google.code.jyield.Yieldable - * ) - */ - - /** - * Submits all of the array's elements to the yieldable. - * - * @param yy - * the yieldable which receives the elements - */ - @Override - public final void generate(final Yieldable yy) { - for (T item : array) { - yy.yield(item); - } - } -} diff --git a/src/main/java/net/slightlymagic/braids/util/generator/GeneratorFunctions.java b/src/main/java/net/slightlymagic/braids/util/generator/GeneratorFunctions.java index 8e560312d3a..7110cc04270 100644 --- a/src/main/java/net/slightlymagic/braids/util/generator/GeneratorFunctions.java +++ b/src/main/java/net/slightlymagic/braids/util/generator/GeneratorFunctions.java @@ -70,50 +70,6 @@ public final class GeneratorFunctions { return result; } - /** - * Highly efficient means of filtering a long or infinite sequence. - * - * @param - * any type - * - * @param predicate - * a Lambda (function) whose apply method takes an object of type - * and returns a Boolean. If it returns false or null, the - * item from the inputGenerator is not yielded by this Generator; - * if predicate.apply returns true, then this Generator - * does yield the value. - * - * @param inputGenerator - * the sequence upon which we operate - * - * @return a generator which produces a subset <= the inputGenerator - */ - public static Generator filterGenerator(final Lambda1 predicate, - final Generator inputGenerator) { - Generator result = new Generator() { - - @Override - public void generate(final Yieldable outputYield) { - - Yieldable inputYield = new Yieldable() { - private Boolean pResult; - - @Override - public void yield(final T input) { - pResult = predicate.apply(input); - if (pResult != null && pResult) { - outputYield.yield(input); - } - } - }; - - inputGenerator.generate(inputYield); - } - - }; - - return result; - } /** * Highly efficient means of applying a transform to a long or infinite diff --git a/src/test/java/forge/card/cardFactory/CardFactoryUtilTest.java b/src/test/java/forge/card/cardFactory/CardFactoryUtilTest.java deleted file mode 100644 index 50e43b31bf7..00000000000 --- a/src/test/java/forge/card/cardFactory/CardFactoryUtilTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package forge.card.cardFactory; - -import org.testng.annotations.Test; - -import com.google.code.jyield.Generator; -import com.google.code.jyield.YieldUtils; - -import forge.AllZone; -import forge.Card; -import forge.CardFilter; -import forge.CardList; -import forge.Counters; -import forge.card.cardfactory.CardFactoryUtil; - -/** - *

- * Mana_PartTest class. - *

- * - * @author Forge - * @version $Id$ - */ -@Test(groups = { "UnitTest" }, timeOut = 1000, enabled = false) -public class CardFactoryUtilTest { - - /** - * Card factory test1. - */ - @Test(timeOut = 1000, enabled = false) - public void cardFactoryTest1() { - - Generator in = YieldUtils.toGenerator(AllZone.getCardFactory()); - - in = CardFilter.getColor(in, "black"); - - CardList list = new CardList(in); - list = list.getType("Creature"); - - System.out.println("Most prominent creature type: " + CardFactoryUtil.getMostProminentCreatureType(list)); - - final String manacost = "3 GW W W R B S"; - final String multipliedTwice = CardFactoryUtil.multiplyManaCost(manacost, 2); - final String multipliedThrice = CardFactoryUtil.multiplyManaCost(manacost, 3); - - System.out.println(manacost + " times 2 = " + multipliedTwice); - System.out.println(manacost + " times 3 = " + multipliedThrice); - - if (CardFactoryUtil.isNegativeCounter(Counters.M1M1)) { - System.out.println("M1M1 is a bad counter!"); - } else { - System.out.println("M1M1 is a good counter!"); - } - } -}