= Updated localizer code
This commit is contained in:
@@ -3,6 +3,8 @@ package dataStructures;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
// This is a class designed to parse ini inspired key-value pairs that are sectioned off.
|
||||
// The difference here is that this is more permissive than an ini file, and only accepts
|
||||
@@ -13,20 +15,27 @@ import java.util.Map;
|
||||
// Valid Ridiculous Key&\n\t_.:;foo =Some Value
|
||||
// Key=
|
||||
//
|
||||
public class SectionedKeyValue
|
||||
// Note that the sections are NOT designed to allow for duplicate keys across them. This is a restriction
|
||||
// of the structure, but can be changed later potentially.
|
||||
public class SectionedKeyValueStore
|
||||
{
|
||||
// Variables
|
||||
public final char SectionStart = '[';
|
||||
public final char SectionEnd = ']';
|
||||
public final char KeyValueLineSeparator = '\n';
|
||||
public final String KeyValueSplit = "=";
|
||||
|
||||
// [Key: SectionName, [Key: KeyString, Value: ValueString]]
|
||||
private HashMap<String, HashMap<String, String>> sectionKeyValue;
|
||||
|
||||
// [Key: KeyString, Value: ValueString]]
|
||||
private HashMap<String, String> keyValue;
|
||||
|
||||
// String constructor that parses the input string into the object
|
||||
public SectionedKeyValue(String input)
|
||||
public SectionedKeyValueStore(String input)
|
||||
{
|
||||
sectionKeyValue = new HashMap<String, HashMap<String, String>>();
|
||||
|
||||
keyValue = new HashMap<String, String>();
|
||||
Parse(input);
|
||||
}
|
||||
|
||||
@@ -36,18 +45,59 @@ public class SectionedKeyValue
|
||||
{
|
||||
AddSection(sectionName);
|
||||
sectionKeyValue.get(sectionName).putIfAbsent(key, value);
|
||||
keyValue.putIfAbsent(key, value);
|
||||
}
|
||||
|
||||
// Returns a HashMap of Keys to Values for a given section
|
||||
@SuppressWarnings("unchecked")
|
||||
public HashMap<String, String> GetSection(String sectionName)
|
||||
{
|
||||
return sectionKeyValue.get(sectionName);
|
||||
return (HashMap<String, String>) sectionKeyValue.get(sectionName).clone();
|
||||
}
|
||||
|
||||
// Look up a global key
|
||||
public String GetKey(String key)
|
||||
{
|
||||
if(keyValue.containsKey(key))
|
||||
return keyValue.get(key);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Calls back on each item in the entire structure. Provides section, then a pair of the keyString and valueString.
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public void ForEach(BiConsumer<? super String, Pair<? super String, ? super String>> action)
|
||||
{
|
||||
Iterator it = sectionKeyValue.entrySet().iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
Iterator internal = ((HashMap<String, String>)pair.getValue()).entrySet().iterator();
|
||||
|
||||
while(internal.hasNext())
|
||||
{
|
||||
Map.Entry internalPair = (Map.Entry)internal.next();
|
||||
action.accept((String)pair.getKey(), new Pair<String, String>((String)internalPair.getKey(), (String)internalPair.getValue()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Calls back each item in the structure but does not priv
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public void ForEach(Consumer<Pair<? super String, ? super String>> action)
|
||||
{
|
||||
Iterator it = keyValue.entrySet().iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
action.accept(new Pair<String, String>((String)pair.getKey(), (String)pair.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
// Stores the internal hashmap as a string then reutrns it.
|
||||
// Places 1 newline between the sections.
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public String ToString()
|
||||
public String toString()
|
||||
{
|
||||
String out = "";
|
||||
|
||||
@@ -55,7 +105,7 @@ public class SectionedKeyValue
|
||||
while (it.hasNext())
|
||||
{
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
out += ("" + SectionStart + pair.getKey() + SectionEnd);
|
||||
out += ("" + SectionStart + pair.getKey() + SectionEnd + "\n");
|
||||
|
||||
Iterator internal = ((HashMap<String, String>)pair.getValue()).entrySet().iterator();
|
||||
|
||||
@@ -115,6 +165,7 @@ public class SectionedKeyValue
|
||||
String key = line.substring(0, splitPos);
|
||||
String value = line.substring(splitPos + KeyValueSplit.length());
|
||||
sectionKeyValue.get(sectionName).putIfAbsent(key, value);
|
||||
keyValue.putIfAbsent(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user