replaced IHasName with Lambda1<String,T> selectors (this allows any field to be used as key. Not just name)

parseSections moved to FileSection
This commit is contained in:
Maxmtg
2012-02-25 07:54:27 +00:00
parent 137d2a63c7
commit 60b74e4d92
17 changed files with 80 additions and 139 deletions

2
.gitattributes vendored
View File

@@ -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

View File

@@ -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;
/**
* <p>
@@ -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)));
}
/**

View File

@@ -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<DeckBase> {
public abstract class DeckBase implements Serializable, Comparable<DeckBase> {
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<Dec
/* (non-Javadoc)
* @see forge.util.IHasName#getName()
*/
@Override
public String getName() {
return this.name;
}

View File

@@ -24,13 +24,12 @@ import net.slightlymagic.braids.util.lambda.Lambda1;
import forge.item.CardPrinted;
import forge.item.ItemPoolView;
import forge.util.IHasName;
/**
* TODO: Write javadoc for this type.
*
*/
public class DeckGroup extends DeckBase implements IHasName {
public class DeckGroup extends DeckBase {
/**
* Instantiates a new deck group.

View File

@@ -40,7 +40,6 @@ import forge.util.FileSection;
import forge.util.FileSectionManual;
import forge.util.FileUtil;
import forge.util.IItemSerializer;
import forge.util.SectionUtil;
import forge.util.StorageReaderFolder;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
@@ -245,7 +244,7 @@ public class DeckSerializer extends StorageReaderFolder<Deck> implements IItemSe
*/
@Override
protected Deck read(final File file) {
final Map<String, List<String>> sections = SectionUtil.parseSections(FileUtil.readFile(file));
final Map<String, List<String>> sections = FileSection.parseSections(FileUtil.readFile(file));
return Deck.fromSections(sections, true);
}

View File

@@ -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<String> fileLines = FileUtil.readFile(f);
final Map<String, List<String>> sections = SectionUtil.parseSections(fileLines);
final Map<String, List<String>> sections = FileSection.parseSections(fileLines);
final DeckFileHeader dh = DeckSerializer.readDeckMetadata(sections, false);
String name = dh.getName();

View File

@@ -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.

View File

@@ -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<String> deckLines = FileUtil.readFile(f);
final Map<String, List<String>> sections = SectionUtil.parseSections(deckLines);
final Map<String, List<String>> sections = FileSection.parseSections(deckLines);
this.deck = Deck.fromSections(sections);
String setProxy = "n/a";

View File

@@ -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;
/**
* <p>
@@ -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<String, List<String>> contents = SectionUtil.parseSections(FileUtil.readFile(f));
final Map<String, List<String>> contents = FileSection.parseSections(FileUtil.readFile(f));
if (contents.containsKey("quest")) {
tempEvent = this.readChallenge(contents.get("quest"));

View File

@@ -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<String, List<String>> parseSections(final List<String> source) {
final Map<String, List<String>> result = new HashMap<String, List<String>>();
String currentSection = "";
List<String> 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<String>) 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<String>();
}
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<String>) oldVal);
}
result.put(currentSection, currentList);
}
return result;
}
}

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
package forge.util;
/**
* TODO: Write javadoc for this type.
*
*/
public interface IHasName {
/**
* Gets the name.
*
* @return the name
*/
String getName();
}

View File

@@ -33,4 +33,6 @@ public interface IItemReader<T> {
*/
Map<String, T> readAll();
// T read(File file);
String getItemKey(T item);
}

View File

@@ -22,7 +22,7 @@ package forge.util;
*
* @param <T> the generic type
*/
public interface IStorage<T extends IHasName> extends IStorageView<T> {
public interface IStorage<T> extends IStorageView<T> {
/**
* <p>

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, List<String>> parseSections(final List<String> source) {
final Map<String, List<String>> result = new HashMap<String, List<String>>();
String currentSection = "";
List<String> 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<String>) 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<String>();
}
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<String>) oldVal);
}
result.put(currentSection, currentList);
}
return result;
}
}

View File

@@ -27,10 +27,9 @@ package forge.util;
* @author Forge
* @version $Id$
*/
public class StorageImmediatelySerialized<T extends IHasName> extends StorageView<T> implements IStorage<T> {
public class StorageImmediatelySerialized<T> extends StorageView<T> implements IStorage<T> {
private final IItemSerializer<T> serializer;
/**
* <p>
* Constructor for DeckManager.
@@ -50,7 +49,8 @@ public class StorageImmediatelySerialized<T extends IHasName> 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);
}

View File

@@ -95,4 +95,9 @@ public abstract class StorageReaderFile<T> implements IItemReader<T> {
return !StringUtils.isBlank(line) && !line.trim().startsWith("#");
}
@Override
public String getItemKey(T item) {
return keySelector.apply(item);
}
}

View File

@@ -144,4 +144,8 @@ public abstract class StorageReaderFolder<T> implements IItemReader<T> {
*/
protected abstract FilenameFilter getFileFilter();
public String getItemKey(T item) {
return keySelector.apply(item);
}
}