Ensure order is kept with maps & yaml (as well as concurrency with maps)

This commit is contained in:
Myles 2016-10-02 20:50:34 +01:00
parent 7d23b12f85
commit 64cde13ea7
3 changed files with 51 additions and 6 deletions

View File

@ -63,8 +63,12 @@ public class CommentStore {
public void storeComments(InputStream inputStream) throws IOException {
InputStreamReader reader = new InputStreamReader(inputStream);
String contents = CharStreams.toString(reader);
String contents;
try {
contents = CharStreams.toString(reader);
} finally {
reader.close();
}
StringBuilder memoryData = new StringBuilder();
// Parse headers
final String pathSeparator = Character.toString(this.pathSeperator);

View File

@ -2,6 +2,7 @@ package us.myles.ViaVersion.util;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
import us.myles.ViaVersion.api.configuration.ConfigurationProvider;
import java.io.*;
@ -10,7 +11,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
public abstract class Config implements ConfigurationProvider {
private static ThreadLocal<Yaml> yaml = new ThreadLocal<Yaml>() {
@ -18,12 +19,14 @@ public abstract class Config implements ConfigurationProvider {
protected Yaml initialValue() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
return new Yaml(options);
options.setPrettyFlow(false);
options.setIndent(2);
return new Yaml(new YamlConstructor(), new Representer(), options);
}
};
private CommentStore commentStore = new CommentStore('.', 2);
private final File configFile;
private ConcurrentHashMap<String, Object> config;
private ConcurrentSkipListMap<String, Object> config;
public Config(File configFile) {
this.configFile = configFile;
@ -108,7 +111,7 @@ public abstract class Config implements ConfigurationProvider {
@Override
public void reloadConfig() {
this.configFile.getParentFile().mkdirs();
this.config = new ConcurrentHashMap<>(loadConfig(this.configFile));
this.config = new ConcurrentSkipListMap<>(loadConfig(this.configFile));
}
@Override

View File

@ -0,0 +1,38 @@
package us.myles.ViaVersion.util;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeId;
import org.yaml.snakeyaml.nodes.Tag;
import java.util.concurrent.ConcurrentSkipListMap;
public class YamlConstructor extends SafeConstructor {
public YamlConstructor() {
super();
yamlClassConstructors.put(NodeId.mapping, new YamlConstructor.ConstructYamlMap());
yamlConstructors.put(Tag.OMAP, new YamlConstructor.ConstructYamlOmap());
}
class Map extends Constructor.ConstructYamlMap {
@Override
public Object construct(Node node) {
Object o = super.construct(node);
if (o instanceof Map && !(o instanceof ConcurrentSkipListMap)) {
return new ConcurrentSkipListMap<>((java.util.Map<?, ?>) o);
}
return o;
}
}
class ConstructYamlOmap extends Constructor.ConstructYamlOmap {
public Object construct(Node node) {
Object o = super.construct(node);
if (o instanceof Map && !(o instanceof ConcurrentSkipListMap)) {
return new ConcurrentSkipListMap<>((java.util.Map<?, ?>) o);
}
return o;
}
}
}