From 60b74e4d92130ae715f21dbd5d86f8827b71853b Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Sat, 25 Feb 2012 07:54:27 +0000 Subject: [PATCH] replaced IHasName with Lambda1 selectors (this allows any field to be used as key. Not just name) parseSections moved to FileSection --- .gitattributes | 2 - src/main/java/forge/deck/Deck.java | 7 +- src/main/java/forge/deck/DeckBase.java | 4 +- src/main/java/forge/deck/DeckGroup.java | 3 +- .../java/forge/deck/io/DeckSerializer.java | 3 +- .../java/forge/deck/io/OldDeckParser.java | 4 +- src/main/java/forge/item/InventoryItem.java | 4 +- src/main/java/forge/item/PreconDeck.java | 3 +- .../forge/quest/data/QuestEventManager.java | 4 +- src/main/java/forge/util/FileSection.java | 53 ++++++++++++ src/main/java/forge/util/IHasName.java | 32 -------- src/main/java/forge/util/IItemReader.java | 2 + src/main/java/forge/util/IStorage.java | 2 +- src/main/java/forge/util/SectionUtil.java | 81 ------------------- .../util/StorageImmediatelySerialized.java | 6 +- .../java/forge/util/StorageReaderFile.java | 5 ++ .../java/forge/util/StorageReaderFolder.java | 4 + 17 files changed, 80 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/forge/util/IHasName.java delete mode 100644 src/main/java/forge/util/SectionUtil.java diff --git a/.gitattributes b/.gitattributes index c596e5dca0f..5326208f0b5 100644 --- a/.gitattributes +++ b/.gitattributes @@ -11437,13 +11437,11 @@ src/main/java/forge/util/FileSection.java -text src/main/java/forge/util/FileSectionManual.java -text src/main/java/forge/util/FileUtil.java svneol=native#text/plain src/main/java/forge/util/HttpUtil.java svneol=native#text/plain -src/main/java/forge/util/IHasName.java -text src/main/java/forge/util/IItemReader.java -text src/main/java/forge/util/IItemSerializer.java -text src/main/java/forge/util/IStorage.java -text src/main/java/forge/util/IStorageView.java -text src/main/java/forge/util/MyRandom.java svneol=native#text/plain -src/main/java/forge/util/SectionUtil.java -text src/main/java/forge/util/StorageImmediatelySerialized.java svneol=native#text/plain src/main/java/forge/util/StorageReaderFile.java -text src/main/java/forge/util/StorageReaderFolder.java -text diff --git a/src/main/java/forge/deck/Deck.java b/src/main/java/forge/deck/Deck.java index 896766a75a0..abc85896fa9 100644 --- a/src/main/java/forge/deck/Deck.java +++ b/src/main/java/forge/deck/Deck.java @@ -37,9 +37,8 @@ import forge.deck.io.DeckSerializer; import forge.gui.deckeditor.elements.TableSorter; import forge.item.CardPrinted; import forge.item.ItemPoolView; +import forge.util.FileSection; import forge.util.FileUtil; -import forge.util.IHasName; -import forge.util.SectionUtil; /** *

@@ -53,7 +52,7 @@ import forge.util.SectionUtil; * @author Forge * @version $Id$ */ -public class Deck extends DeckBase implements Serializable, IHasName { +public class Deck extends DeckBase implements Serializable { /** * */ @@ -161,7 +160,7 @@ public class Deck extends DeckBase implements Serializable, IHasName { * @return the deck */ public static Deck fromFile(final File deckFile) { - return Deck.fromSections(SectionUtil.parseSections(FileUtil.readFile(deckFile))); + return Deck.fromSections(FileSection.parseSections(FileUtil.readFile(deckFile))); } /** diff --git a/src/main/java/forge/deck/DeckBase.java b/src/main/java/forge/deck/DeckBase.java index 51c4797c399..e6e553f30f1 100644 --- a/src/main/java/forge/deck/DeckBase.java +++ b/src/main/java/forge/deck/DeckBase.java @@ -21,13 +21,12 @@ import java.io.Serializable; import forge.item.CardPrinted; import forge.item.ItemPoolView; -import forge.util.IHasName; /** * TODO: Write javadoc for this type. * */ -public abstract class DeckBase implements IHasName, Serializable, Comparable { +public abstract class DeckBase implements Serializable, Comparable { private static final long serialVersionUID = -7538150536939660052L; // gameType is from Constant.GameType, like GameType.Regular @@ -74,7 +73,6 @@ public abstract class DeckBase implements IHasName, Serializable, Comparable implements IItemSe */ @Override protected Deck read(final File file) { - final Map> sections = SectionUtil.parseSections(FileUtil.readFile(file)); + final Map> sections = FileSection.parseSections(FileUtil.readFile(file)); return Deck.fromSections(sections, true); } diff --git a/src/main/java/forge/deck/io/OldDeckParser.java b/src/main/java/forge/deck/io/OldDeckParser.java index 92f0a768bfe..af1e7baba5a 100644 --- a/src/main/java/forge/deck/io/OldDeckParser.java +++ b/src/main/java/forge/deck/io/OldDeckParser.java @@ -33,9 +33,9 @@ import org.apache.commons.lang3.tuple.Pair; import forge.PlayerType; import forge.deck.Deck; import forge.deck.DeckGroup; +import forge.util.FileSection; import forge.util.FileUtil; import forge.util.IStorage; -import forge.util.SectionUtil; /** * TODO: Write javadoc for this type. @@ -172,7 +172,7 @@ public class OldDeckParser { boolean importedOk = false; final List fileLines = FileUtil.readFile(f); - final Map> sections = SectionUtil.parseSections(fileLines); + final Map> sections = FileSection.parseSections(fileLines); final DeckFileHeader dh = DeckSerializer.readDeckMetadata(sections, false); String name = dh.getName(); diff --git a/src/main/java/forge/item/InventoryItem.java b/src/main/java/forge/item/InventoryItem.java index 836a1d7d5e6..92d1b991fcd 100644 --- a/src/main/java/forge/item/InventoryItem.java +++ b/src/main/java/forge/item/InventoryItem.java @@ -17,13 +17,11 @@ */ package forge.item; -import forge.util.IHasName; - /** * Interface to define a player's inventory may hold. Should include * CardPrinted, Booster, Pets, Plants... etc */ -public interface InventoryItem extends IHasName { +public interface InventoryItem { /** * An inventory item has to provide a name. diff --git a/src/main/java/forge/item/PreconDeck.java b/src/main/java/forge/item/PreconDeck.java index 463300067a3..3634e3b219b 100644 --- a/src/main/java/forge/item/PreconDeck.java +++ b/src/main/java/forge/item/PreconDeck.java @@ -28,7 +28,6 @@ import forge.deck.Deck; import forge.quest.SellRules; import forge.util.FileSection; import forge.util.FileUtil; -import forge.util.SectionUtil; /** * TODO: Write javadoc for this type. @@ -81,7 +80,7 @@ public class PreconDeck implements InventoryItemFromSet { */ public PreconDeck(final File f) { final List deckLines = FileUtil.readFile(f); - final Map> sections = SectionUtil.parseSections(deckLines); + final Map> sections = FileSection.parseSections(deckLines); this.deck = Deck.fromSections(sections); String setProxy = "n/a"; diff --git a/src/main/java/forge/quest/data/QuestEventManager.java b/src/main/java/forge/quest/data/QuestEventManager.java index 82c3d469e55..ae2b07a2ff7 100644 --- a/src/main/java/forge/quest/data/QuestEventManager.java +++ b/src/main/java/forge/quest/data/QuestEventManager.java @@ -34,8 +34,8 @@ import forge.properties.ForgeProps; import forge.properties.NewConstants; import forge.quest.BoosterUtils; import forge.quest.data.QuestPreferences.QPref; +import forge.util.FileSection; import forge.util.FileUtil; -import forge.util.SectionUtil; /** *

@@ -82,7 +82,7 @@ public class QuestEventManager { final File[] allFiles = ForgeProps.getFile(NewConstants.Quest.DECKS).listFiles(DeckSerializer.DCK_FILE_FILTER); for (final File f : allFiles) { - final Map> contents = SectionUtil.parseSections(FileUtil.readFile(f)); + final Map> contents = FileSection.parseSections(FileUtil.readFile(f)); if (contents.containsKey("quest")) { tempEvent = this.readChallenge(contents.get("quest")); diff --git a/src/main/java/forge/util/FileSection.java b/src/main/java/forge/util/FileSection.java index de1033ad9e1..72f26f5d94c 100644 --- a/src/main/java/forge/util/FileSection.java +++ b/src/main/java/forge/util/FileSection.java @@ -17,6 +17,9 @@ */ package forge.util; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.regex.Pattern; @@ -144,4 +147,54 @@ public class FileSection { return "true".equalsIgnoreCase(s); } + /** + * Parses the sections. + * + * @param source + * the source + * @return the map + */ + @SuppressWarnings("unchecked") + public static Map> parseSections(final List source) { + final Map> result = new HashMap>(); + String currentSection = ""; + List currentList = null; + + for (final String s : source) { + final String st = s.trim(); + if (st.length() == 0) { + continue; + } + if (st.startsWith("[") && st.endsWith("]")) { + if ((currentList != null) && (currentList.size() > 0)) { + final Object oldVal = result.get(currentSection); + if ((oldVal != null) && (oldVal instanceof List)) { + currentList.addAll((List) oldVal); + } + result.put(currentSection, currentList); + } + + final String newSection = st.substring(1, st.length() - 1); + currentSection = newSection; + currentList = null; + } else { + if (currentList == null) { + currentList = new ArrayList(); + } + currentList.add(st); + } + } + + // save final block + if ((currentList != null) && (currentList.size() > 0)) { + final Object oldVal = result.get(currentSection); + if ((oldVal != null) && (oldVal instanceof List)) { + currentList.addAll((List) oldVal); + } + result.put(currentSection, currentList); + } + + return result; + } + } diff --git a/src/main/java/forge/util/IHasName.java b/src/main/java/forge/util/IHasName.java deleted file mode 100644 index b8d3415ed42..00000000000 --- a/src/main/java/forge/util/IHasName.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Forge: Play Magic: the Gathering. - * Copyright (C) 2011 Nate - * - * 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.util; - -/** - * TODO: Write javadoc for this type. - * - */ -public interface IHasName { - - /** - * Gets the name. - * - * @return the name - */ - String getName(); -} diff --git a/src/main/java/forge/util/IItemReader.java b/src/main/java/forge/util/IItemReader.java index a840f3d1bce..6f9e293c5bb 100644 --- a/src/main/java/forge/util/IItemReader.java +++ b/src/main/java/forge/util/IItemReader.java @@ -33,4 +33,6 @@ public interface IItemReader { */ Map readAll(); // T read(File file); + + String getItemKey(T item); } diff --git a/src/main/java/forge/util/IStorage.java b/src/main/java/forge/util/IStorage.java index 20954f41473..9cde7ad7bd0 100644 --- a/src/main/java/forge/util/IStorage.java +++ b/src/main/java/forge/util/IStorage.java @@ -22,7 +22,7 @@ package forge.util; * * @param the generic type */ -public interface IStorage extends IStorageView { +public interface IStorage extends IStorageView { /** *

diff --git a/src/main/java/forge/util/SectionUtil.java b/src/main/java/forge/util/SectionUtil.java deleted file mode 100644 index becba42624a..00000000000 --- a/src/main/java/forge/util/SectionUtil.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Forge: Play Magic: the Gathering. - * Copyright (C) 2011 Nate - * - * 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.util; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Divides file into sections and joins them back to stringlist to save. - * - */ -public class SectionUtil { - - /** - * Parses the sections. - * - * @param source - * the source - * @return the map - */ - @SuppressWarnings("unchecked") - public static Map> parseSections(final List source) { - final Map> result = new HashMap>(); - String currentSection = ""; - List currentList = null; - - for (final String s : source) { - final String st = s.trim(); - if (st.length() == 0) { - continue; - } - if (st.startsWith("[") && st.endsWith("]")) { - if ((currentList != null) && (currentList.size() > 0)) { - final Object oldVal = result.get(currentSection); - if ((oldVal != null) && (oldVal instanceof List)) { - currentList.addAll((List) oldVal); - } - result.put(currentSection, currentList); - } - - final String newSection = st.substring(1, st.length() - 1); - currentSection = newSection; - currentList = null; - } else { - if (currentList == null) { - currentList = new ArrayList(); - } - currentList.add(st); - } - } - - // save final block - if ((currentList != null) && (currentList.size() > 0)) { - final Object oldVal = result.get(currentSection); - if ((oldVal != null) && (oldVal instanceof List)) { - currentList.addAll((List) oldVal); - } - result.put(currentSection, currentList); - } - - return result; - } - -} diff --git a/src/main/java/forge/util/StorageImmediatelySerialized.java b/src/main/java/forge/util/StorageImmediatelySerialized.java index 29a73dce367..02a67e3f304 100644 --- a/src/main/java/forge/util/StorageImmediatelySerialized.java +++ b/src/main/java/forge/util/StorageImmediatelySerialized.java @@ -27,10 +27,9 @@ package forge.util; * @author Forge * @version $Id$ */ -public class StorageImmediatelySerialized extends StorageView implements IStorage { +public class StorageImmediatelySerialized extends StorageView implements IStorage { private final IItemSerializer serializer; - /** *

* Constructor for DeckManager. @@ -50,7 +49,8 @@ public class StorageImmediatelySerialized extends StorageVie */ @Override public final void add(final T deck) { - this.getMap().put(deck.getName(), deck); + String name = serializer.getItemKey(deck); + this.getMap().put(name, deck); this.serializer.save(deck); } diff --git a/src/main/java/forge/util/StorageReaderFile.java b/src/main/java/forge/util/StorageReaderFile.java index 0275c05e64e..720faf8295d 100644 --- a/src/main/java/forge/util/StorageReaderFile.java +++ b/src/main/java/forge/util/StorageReaderFile.java @@ -94,5 +94,10 @@ public abstract class StorageReaderFile implements IItemReader { protected boolean lineContainsObject(final String line) { return !StringUtils.isBlank(line) && !line.trim().startsWith("#"); } + + @Override + public String getItemKey(T item) { + return keySelector.apply(item); + } } diff --git a/src/main/java/forge/util/StorageReaderFolder.java b/src/main/java/forge/util/StorageReaderFolder.java index ba92009f9e7..f3f6c5f9b62 100644 --- a/src/main/java/forge/util/StorageReaderFolder.java +++ b/src/main/java/forge/util/StorageReaderFolder.java @@ -143,5 +143,9 @@ public abstract class StorageReaderFolder implements IItemReader { * @return FilenameFilter to pick only relevant objects for deserialization */ protected abstract FilenameFilter getFileFilter(); + + public String getItemKey(T item) { + return keySelector.apply(item); + } }