carving away some code we won't ever need.

This commit is contained in:
Maxmtg
2012-03-26 21:16:55 +00:00
parent 4f001f636a
commit c0b13ba381
14 changed files with 49 additions and 408 deletions

2
.gitattributes vendored
View File

@@ -11842,7 +11842,6 @@ src/main/java/forge/view/match/ViewWinLose.java -text
src/main/java/forge/view/match/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/LICENSE.txt svneol=native#text/plain
src/main/java/net/slightlymagic/braids/util/ClumsyRunnable.java svneol=native#text/plain
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
@@ -11883,5 +11882,6 @@ src/test/java/forge/gui/game/CardDetailPanelTest.java svneol=native#text/plain
src/test/java/forge/model/BuildInfoTest.java -text
src/test/java/forge/model/FModelTest.java -text
src/test/java/net/slightlymagic/braids/util/testng/BraidsAssertFunctions.java svneol=native#text/plain
src/test/java/net/slightlymagic/braids/util/testng/ClumsyRunnable.java svneol=native#text/plain
src/test/java/net/slightlymagic/braids/util/testng/LICENSE.txt svneol=native#text/plain
src/test/java/net/slightlymagic/braids/util/testng/package-info.java -text

View File

@@ -20,7 +20,6 @@ package forge;
import java.util.Arrays;
import java.util.List;
import net.slightlymagic.braids.util.UtilFunctions;
import forge.Constant.Zone;
import forge.card.cardfactory.CardFactoryInterface;
import forge.card.cardfactory.PreloadingCardFactory;
@@ -239,8 +238,7 @@ public final class AllZone {
* @param factory
* the factory to set
*/
public static void setCardFactory(final CardFactoryInterface factory) {
UtilFunctions.checkNotNull("factory", factory);
private static void setCardFactory(final CardFactoryInterface factory) {
AllZone.cardFactory = factory;
}

View File

@@ -35,7 +35,6 @@ import java.util.zip.ZipFile;
import javax.swing.SwingUtilities;
import net.slightlymagic.braids.util.UtilFunctions;
import net.slightlymagic.braids.util.generator.FindNonDirectoriesSkipDotDirectoriesGenerator;
import net.slightlymagic.braids.util.generator.GeneratorFunctions;
@@ -622,42 +621,4 @@ public class CardReader implements Runnable {
return buf.toString();
}
/**
* Attempt to load a card by its canonical ASCII name.
*
* @param canonicalASCIIName
* the canonical ASCII name of the card
*
* @return a new Card instance having that name, or null if not found
*/
public final Card findCard(final String canonicalASCIIName) {
// Braids on
// 8/18/11
// 11:08 PM
UtilFunctions.checkNotNull("canonicalASCIIName", canonicalASCIIName);
final String cardFilePath = this.toMostLikelyPath(canonicalASCIIName);
Card result = null;
if (this.zip != null) {
final ZipEntry entry = this.zip.getEntry(cardFilePath);
if (entry != null) {
result = this.loadCard(entry);
}
}
if (result == null) {
result = this.loadCard(new File(this.cardsfolder, cardFilePath));
}
if ((result == null) || !(result.getName().equals(canonicalASCIIName))) {
// System.err.println(":Could not find \"" + cardFilePath + "\".");
result = this.loadCardsUntilYouFind(canonicalASCIIName);
}
return result;
}
}

View File

@@ -14,7 +14,11 @@ public class BoosterData {
public final String getEdition() {
return edition;
}
private final String landEdition;
public final String getLandEdition() {
return landEdition;
}
private final int nCommon;
private final int nUncommon;
private final int nRare;
@@ -39,10 +43,10 @@ public class BoosterData {
* @param nDF
* the n df
*/
public BoosterData(final String edition, final int nC, final int nU, final int nR, final int nS, final int nDF) {
public BoosterData(final String edition, final String editionLand, final int nC, final int nU, final int nR, final int nS, final int nDF) {
// if this booster has more that 10 cards, there must be a land in
// 15th slot unless it's already taken
this(edition, nC, nU, nR, nS, nDF, (nC + nR + nU + nS + nDF) > 10 ? BoosterData.CARDS_PER_BOOSTER - nC - nR - nU
this(edition, editionLand, nC, nU, nR, nS, nDF, (nC + nR + nU + nS + nDF) > 10 ? BoosterData.CARDS_PER_BOOSTER - nC - nR - nU
- nS - nDF : 0, 68);
}
@@ -64,7 +68,7 @@ public class BoosterData {
* @param oneFoilPer
* the one foil per
*/
public BoosterData(final String edition0, final int nC, final int nU, final int nR, final int nS, final int nDF, final int nL,
public BoosterData(final String edition0, final String editionLand, final int nC, final int nU, final int nR, final int nS, final int nDF, final int nL,
final int oneFoilPer) {
this.nCommon = nC;
this.nUncommon = nU;
@@ -74,6 +78,7 @@ public class BoosterData {
this.nLand = nL > 0 ? nL : 0;
this.foilRate = oneFoilPer;
this.edition = edition0;
this.landEdition = editionLand;
}
/**
@@ -88,7 +93,9 @@ public class BoosterData {
public final Predicate<CardPrinted> getEditionFilter() {
return CardPrinted.Predicates.printedInSets(edition);
}
public final Predicate<CardPrinted> getLandEditionFilter() {
return CardPrinted.Predicates.printedInSets(landEdition);
}
/**
* Gets the uncommon.
*
@@ -179,7 +186,11 @@ public class BoosterData {
int nDf = section.getInt("DoubleFaced", 0);
int nLand = section.getInt("BasicLands", 0);
int nFoilRate = section.getInt("FoilRate", 68);
return new BoosterData(section.get("Set"), nC, nU, nR, nS, nDf, nLand, nFoilRate);
String edition = section.get("Set");
String editionLand = section.get("LandSet");
if( editionLand == null )
editionLand = edition;
return new BoosterData(edition, editionLand, nC, nU, nR, nS, nDf, nLand, nFoilRate);
}
}
}

View File

@@ -28,7 +28,6 @@ import java.util.TreeMap;
import javax.swing.JOptionPane;
import net.slightlymagic.braids.util.UtilFunctions;
import forge.Card;
import forge.CardList;
import forge.Constant;
@@ -93,9 +92,12 @@ public final class BoosterDraft implements IBoosterDraft {
case Block: // Draft from cards by block or set
final Object o = GuiUtils.chooseOne("Choose Block", UtilFunctions.iteratorToArray(Singletons.getModel().getBlocks().iterator(), new CardBlock[]{}));
final CardBlock block = (CardBlock) o;
List<CardBlock> blocks = new ArrayList<CardBlock>();
for(CardBlock b : Singletons.getModel().getBlocks()) {
blocks.add(b);
}
final CardBlock block = GuiUtils.chooseOne("Choose Block", blocks);
final CardEdition[] cardSets = block.getSets();
final String[] sets = new String[cardSets.length];
for (int k = cardSets.length - 1; k >= 0; --k) {

View File

@@ -23,7 +23,6 @@ import java.util.List;
import javax.swing.JOptionPane;
import net.slightlymagic.braids.util.UtilFunctions;
import forge.AllZone;
import forge.Card;
import forge.CardList;
@@ -79,8 +78,11 @@ public class SealedDeck {
this.getLandSetCode()[0] = CardDb.instance().getCard("Plains").getEdition();
} else if (sealedType.equals("Block")) {
final Object o = GuiUtils.chooseOne("Choose Block", UtilFunctions.iteratorToArray(Singletons.getModel().getBlocks().iterator(), new CardBlock[]{}));
final CardBlock block = (CardBlock) o;
List<CardBlock> blocks = new ArrayList<CardBlock>();
for(CardBlock b : Singletons.getModel().getBlocks()) {
blocks.add(b);
}
final CardBlock block = GuiUtils.chooseOne("Choose Block", blocks);
final CardEdition[] cardSets = block.getSets();
final String[] sets = new String[cardSets.length];

View File

@@ -71,8 +71,7 @@ public class FatPack extends OpenablePack {
for (int i = 0; i < fpData.getCntBoosters(); i++) {
result.addAll(super.generate());
}
CardEdition landEdition = Singletons.getModel().getEditions().get(fpData.getLandsEdition());
result.addAll(getRandomBasicLands(landEdition, fpData.getCntLands()));
result.addAll(getRandomBasicLands(fpData.getLandsEdition(), fpData.getCntLands()));
return result;
}

View File

@@ -17,14 +17,10 @@
*/
package forge.item;
import java.util.Arrays;
import java.util.List;
import net.slightlymagic.braids.util.UtilFunctions;
import forge.Singletons;
import forge.card.BoosterData;
import forge.card.BoosterGenerator;
import forge.card.CardEdition;
import forge.card.CardRules;
import forge.util.Predicate;
@@ -148,24 +144,11 @@ public abstract class OpenablePack implements InventoryItemFromSet {
final int cntLands = this.contents.getLand();
if (cntLands > 0) {
myCards.add(this.getLandFromNearestSet());
myCards.add(this.getRandomBasicLand(this.contents.getLandEdition()));
}
return myCards;
}
private CardPrinted getLandFromNearestSet() {
final CardEdition[] editions = UtilFunctions.iteratorToArray(Singletons.getModel().getEditions().iterator(),
new CardEdition[] {});
final int iThisSet = Arrays.binarySearch(editions, this.contents);
for (int iSet = iThisSet; iSet < editions.length; iSet++) {
final CardPrinted land = this.getRandomBasicLand(editions[iSet]);
if (null != land) {
return land;
}
}
// if not found (though that's impossible)
return this.getRandomBasicLand(Singletons.getModel().getEditions().get("M12"));
}
/**
* Gets the random basic land.
@@ -173,8 +156,8 @@ public abstract class OpenablePack implements InventoryItemFromSet {
* @param set the set
* @return the random basic land
*/
protected CardPrinted getRandomBasicLand(final CardEdition set) {
return this.getRandomBasicLands(set, 1).get(0);
protected CardPrinted getRandomBasicLand(final String setCode) {
return this.getRandomBasicLands(setCode, 1).get(0);
}
/**
@@ -184,8 +167,8 @@ public abstract class OpenablePack implements InventoryItemFromSet {
* @param count the count
* @return the random basic lands
*/
protected List<CardPrinted> getRandomBasicLands(final CardEdition set, final int count) {
return Predicate.and(CardPrinted.Predicates.printedInSets(set.getCode()),
protected List<CardPrinted> getRandomBasicLands(final String setCode, final int count) {
return Predicate.and(CardPrinted.Predicates.printedInSets(setCode),
CardRules.Predicates.Presets.IS_BASIC_LAND, CardPrinted.FN_GET_RULES).random(
CardDb.instance().getAllCards(), count);
}

View File

@@ -49,8 +49,6 @@ import forge.quest.bazaar.QuestItemType;
import forge.quest.data.QuestAssets;
import forge.quest.data.QuestPreferences.QPref;
import forge.util.MyRandom;
import net.slightlymagic.braids.util.UtilFunctions;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
@@ -534,18 +532,18 @@ public class QuestWinLoseHandler extends ControlWinLose {
*
*/
private void awardBooster() {
final GameFormat[] formats = UtilFunctions.iteratorToArray(Singletons.getModel().getFormats().iterator(), new GameFormat[]{});
final ListChooser<GameFormat> ch = new ListChooser<GameFormat>("Choose bonus booster format", 1, formats);
final List<GameFormat> formats = new ArrayList<GameFormat>();
String prefferedFormat = Singletons.getModel().getQuestPreferences().getPreference(QPref.BOOSTER_FORMAT);
int index = 0;
for (int i = 0; i < formats.length; i++) {
if (formats[i].toString().equals(prefferedFormat)) {
int index = 0, i = 0;
for (GameFormat f : Singletons.getModel().getFormats()) {
formats.add(f);
if (f.toString().equals(prefferedFormat))
index = i;
break;
}
i++;
}
final ListChooser<GameFormat> ch = new ListChooser<GameFormat>("Choose bonus booster format", 1, formats);
ch.show(index);
final GameFormat selected = ch.getSelectedValue();

View File

@@ -21,10 +21,6 @@
package net.slightlymagic.braids.util;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.SwingUtilities;
/**
@@ -39,40 +35,6 @@ public final class UtilFunctions {
// empty
}
/**
* Throws a NullPointerException if param is null.
*
* @param paramName
* the name of the parameter; may be null
* @param param
* the parameter to test
*/
public static void checkNotNull(final String paramName, final Object param) {
if (param != null) {
return;
}
NullPointerException exn = null;
if (paramName == null) {
exn = new NullPointerException();
// 11:19 PM
} else {
exn = new NullPointerException(paramName + " must not be null");
// by
// Braids
// on
// 8/18/11
// 11:19
// PM
}
// Doctor the exception to appear to come from the caller.
final StackTraceElement[] trace = exn.getStackTrace();
final int len = UtilFunctions.getSliceLength(trace, 1);
exn.setStackTrace(UtilFunctions.slice(new StackTraceElement[len], trace, 1));
throw exn;
}
/**
* Invoke the given Runnable in an Event Dispatch Thread and wait for it to
@@ -109,184 +71,7 @@ public final class UtilFunctions {
}
}
/**
* Create an array from the (rest of) an iterator's output; this function is
* horribly inefficient.
*
* Please, only use it on small iterators.
*
* @param <T>
* (inferred automatically)
*
* @param iter
* the iterator to traverse
*
* @return an array of (the rest of) the iterator's values
*/
public static <T> T[] iteratorToArray(final Iterator<T> iter, T[] destArray) {
final ArrayList<T> list = new ArrayList<T>();
T item;
while (iter.hasNext()) {
item = iter.next();
list.add(item);
}
final T[] result = list.toArray(destArray);
return result;
}
/**
* Returns the rightmost portion of an array, Python-style.
*
* @param <T>
* (inferred automatically)
*
* @param dstArray
* the array in which to place new items
*
* @param srcArray
* the array to copy (shallowly)
*
* @param startIndexIn
* if positive, the index (from the left) at which to start
* copying; if negative, we treat this as the index from the
* right. For example, calling this with startIndex = -2 returns
* the last two items in the array, if it has that many.
*
* @return a shallow copy of array starting at startIndex; this may return
* an empty array if the startIndex is out of bounds.
*/
public static <T extends Object> T[] slice(final T[] dstArray, final T[] srcArray, final int startIndexIn) {
int startIndex = startIndexIn;
if (startIndex < 0) {
startIndex = srcArray.length + startIndex;
if (startIndex < 0) {
startIndex = 0;
}
}
if (dstArray == null) {
throw new NullPointerException();
// 11:19 PM
}
if (srcArray == null) {
throw new NullPointerException();
// 11:19 PM
}
final int resultLength = UtilFunctions.getSliceLength(srcArray, startIndex);
if (dstArray.length != resultLength) {
throw new ArrayIndexOutOfBoundsException("First parameter must have length " + resultLength
+ ", but length is " + dstArray.length + ".");
}
int srcIx = startIndex;
for (int dstIx = 0; (dstIx < resultLength) && (srcIx < srcArray.length); dstIx++, srcIx++) {
dstArray[dstIx] = srcArray[srcIx];
}
return dstArray;
}
/**
* Get a slice's length in preparation for taking a slice.
*
* I do not like the fact that I have to use this function, but Java left me
* with little choice.
*
* @param <T>
* (inferred automatically)
* @param srcArray
* the array that would be copied (shallowly)
* @param startIndexIn
* if positive, the index (from the left) at which copying would
* start; if negative, we treat this as the index from the right.
* For example, calling this with startIndex = -2 computes the
* length if slice would return the last two items in the array,
* if it has that many.
* @return the length of the array that would result from calling
* slice(Object[], Object[], int) with the given srcArray and
* startIndex.
* @see #slice(Object[], Object[], int)
*/
public static <T> int getSliceLength(final T[] srcArray, final int startIndexIn) {
int startIndex = startIndexIn;
if (startIndex < 0) {
startIndex = srcArray.length + startIndex;
if (startIndex < 0) {
startIndex = 0;
}
}
final int resultLength = srcArray.length - startIndex;
return resultLength;
}
/**
* Handles the boilerplate null and isinstance check for an equals method.
*
* Example:
*
* <pre>
* public boolean equals(Object obj) {
* MyClassName that = checkNullOrNotInstance(this, obj);
* if (that == null) {
* return false;
* }
* // ...
* }
* </pre>
*
* @param <T>
* (inferred automatically)
*
* @param goodInstance
* a non-null instance of type T; looks neater than passing in
* goodInstance.getClass()
*
* @param obj
* the object to test
*
* @return null if obj is null or not an instance of goodInstance's class;
* otherwise, we return obj cast to goodInstance's type
*/
public static <T> T checkNullOrNotInstance(final T goodInstance, final Object obj) {
if (goodInstance == null) {
throw new NullPointerException("first parameter must not be null");
// by
// Braids
// on
// 8/18/11
// 11:19
// PM
}
@SuppressWarnings("unchecked")
final Class<T> classT = (Class<T>) goodInstance.getClass();
boolean viable = true;
if (obj == null) {
viable = false;
} else if (!(classT.isInstance(obj))) {
viable = false;
}
T result;
if (viable) {
result = classT.cast(obj);
} else {
result = null;
}
return result;
}
/**
* Safely converts an object to a String.
*
@@ -307,68 +92,5 @@ public final class UtilFunctions {
return result;
}
/**
* Remove nulls and duplicate items from the list.
*
* This may change the list's ordering. It uses the items' equals methods to
* determine equality.
*
* Advantages over HashSet: This consumes no unnecessary heap-memory, nor
* does it require objects to implement hashCode. It is OK if (o1.equals(o2)
* does not imply o1.hashCode() == o2.hashCode()).
*
* Advantages over TreeSet: This does not require a comparator.
*
* Disadvantages over HashSet and TreeSet: This runs in O(n*n) time.
*
* @param <T>
* (inferred automatically)
*
* @param list
* the list to modify; this is fastest with ArrayList.
*/
public static <T> void smartRemoveDuplicatesAndNulls(final List<T> list) {
// Get rid of pesky leading nulls.
UtilFunctions.smartRemoveDuplicatesAndNullsHelper(list, 0, null);
for (int earlierIx = 0; earlierIx < list.size(); earlierIx++) {
for (int laterIx = earlierIx + 1; laterIx < list.size(); laterIx++) {
final T itemAtEarlierIx = list.get(earlierIx);
UtilFunctions.smartRemoveDuplicatesAndNullsHelper(list, laterIx, itemAtEarlierIx);
}
}
}
/**
* Helper method for smartRemoveDuplicatesAndNulls that is subject to
* change; if you call this directly, you do so at your own risk!.
*
* @param <T>
* (inferred automatically)
* @param list
* the list to modify; if all items from startIx to the end are
* either null or equal to objSeenPreviously, then we truncate
* the list just before startIx.
* @param startIx
* the index to examine; we only move items within the range of
* [startIx, list.size()-1].
* @param objSeenPreviously
* the object with which to compare list[startIx]; may be null.
*/
public static <T> void smartRemoveDuplicatesAndNullsHelper(final List<T> list, final int startIx,
final T objSeenPreviously) {
while ((startIx < list.size())
&& ((list.get(startIx) == null) || (list.get(startIx) == objSeenPreviously) || list.get(startIx)
.equals(objSeenPreviously))) {
final int lastItemIx = list.size() - 1;
// Overwrite the item at laterIx with the one at the end,
// then delete the one at the end.
list.set(startIx, list.get(lastItemIx));
list.remove(lastItemIx);
}
}
}

View File

@@ -5,8 +5,8 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.slightlymagic.braids.util.ClumsyRunnable;
import net.slightlymagic.braids.util.testng.BraidsAssertFunctions;
import net.slightlymagic.braids.util.testng.ClumsyRunnable;
import org.testng.Assert;
import org.testng.annotations.Test;
@@ -99,40 +99,6 @@ public class CardReaderTest {
BraidsAssertFunctions.assertThrowsException(RuntimeException.class, withScissors);
}
/**
* Test_ read card_find card_zip.
*/
@Test(groups = { "UnitTest", "fast" }, timeOut = CardReaderTest.TEST_TIMEOUT, enabled = false)
public final void test_ReadCard_findCard_zip() {
final Map<String, Card> map = new HashMap<String, Card>();
final File cardsfolder = ForgeProps.getFile(NewConstants.CARDSFOLDER);
final CardReader cardReader = new CardReader(cardsfolder, map);
final File zipFile = new File(cardsfolder, "cardsfolder.zip");
Assert.assertTrue(zipFile.exists(), "zip file exists");
final Card elvishWarrior = cardReader.findCard("Elvish Warrior");
Assert.assertNotNull(elvishWarrior);
Assert.assertEquals(elvishWarrior.getName(), "Elvish Warrior", "name is correct");
}
/**
* Test_ read card_find card_nonzip.
*/
@Test(groups = { "UnitTest", "fast" }, timeOut = CardReaderTest.TEST_TIMEOUT)
public final void test_ReadCard_findCard_nonzip() {
final Map<String, Card> map = new HashMap<String, Card>();
final File cardsfolder = ForgeProps.getFile(NewConstants.CARDSFOLDER);
final CardReader cardReader = new CardReader(cardsfolder, map, null, false);
final Card savannahLions = cardReader.findCard("Savannah Lions");
Assert.assertNotNull(savannahLions);
Assert.assertEquals(savannahLions.getName(), "Savannah Lions", "name is correct");
}
/**
* Test_ read card_run_nonzip.
*/

View File

@@ -6,8 +6,8 @@ import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.slightlymagic.braids.util.ClumsyRunnable;
import net.slightlymagic.braids.util.testng.BraidsAssertFunctions;
import net.slightlymagic.braids.util.testng.ClumsyRunnable;
import org.testng.Assert;
import org.testng.annotations.Test;

View File

@@ -1,6 +1,5 @@
package net.slightlymagic.braids.util.testng;
import net.slightlymagic.braids.util.ClumsyRunnable;
import org.testng.Assert;

View File

@@ -18,7 +18,7 @@
* limitations under the Apache License.
*
*/
package net.slightlymagic.braids.util;
package net.slightlymagic.braids.util.testng;
/**
* Like Runnable, but it can throw any Exception.