Commit c8cb01d4 authored by liujiahua123123's avatar liujiahua123123

SynchronizedLinkedListMap

parent 155d883b
...@@ -25,24 +25,8 @@ public class MiraiConfig extends MiraiConfigSection<Object> { ...@@ -25,24 +25,8 @@ public class MiraiConfig extends MiraiConfigSection<Object> {
} }
public MiraiConfig(@NotNull File file) { public MiraiConfig(@NotNull File file) {
super(); super(parse(Objects.requireNonNull(file)));
Objects.requireNonNull(file);
/*if (!file.toURI().getPath().contains(MiraiServer.getInstance().getParentFolder().getPath())) {
file = new File(MiraiServer.getInstance().getParentFolder().getPath(), file.getName());
}*/
this.root = file; this.root = file;
if (!file.exists()) {
try {
if (!file.createNewFile()) {
return;
}
} catch (IOException e) {
e.printStackTrace();
}
}
this.parse();
} }
public synchronized void save() { public synchronized void save() {
...@@ -58,18 +42,29 @@ public class MiraiConfig extends MiraiConfigSection<Object> { ...@@ -58,18 +42,29 @@ public class MiraiConfig extends MiraiConfigSection<Object> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void parse() { private static LinkedHashMap<String,Object> parse(File file) {
if (!file.toURI().getPath().contains(MiraiServer.getInstance().getParentFolder().getPath())) {
file = new File(MiraiServer.getInstance().getParentFolder().getPath(), file.getName());
}
if (!file.exists()) {
try {
if (!file.createNewFile()) {
return new LinkedHashMap<>();
}
} catch (IOException e) {
e.printStackTrace();
return new LinkedHashMap<>();
}
}
DumperOptions dumperOptions = new DumperOptions(); DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(dumperOptions); Yaml yaml = new Yaml(dumperOptions);
try { try {
LinkedHashMap<String, Object> content = yaml.loadAs(Utils.readFile(this.root), LinkedHashMap.class); return yaml.loadAs(Utils.readFile(file), LinkedHashMap.class);
if (content != null) {
setContent(content);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
return new LinkedHashMap<>();
} }
......
...@@ -7,10 +7,15 @@ import java.util.Map; ...@@ -7,10 +7,15 @@ import java.util.Map;
public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String, T> { public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String, T> {
public MiraiConfigSection(){ public MiraiConfigSection(){
super();
}
public MiraiConfigSection(LinkedHashMap<String, T> map){
super(map);
} }
public Integer getInt(String key){ public Integer getInt(String key){
return Integer.valueOf(this.get(key).toString()); return Integer.valueOf(this.get(key).toString());
} }
...@@ -41,9 +46,9 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String ...@@ -41,9 +46,9 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
return (MiraiConfigSection<D>) content; return (MiraiConfigSection<D>) content;
} }
if(content instanceof Map){ if(content instanceof Map){
return new MiraiConfigSection<>(){{ return new MiraiConfigSection<>(
setContent((LinkedHashMap<String, D>) content); (LinkedHashMap<String, D>) content
}}; );
} }
return null; return null;
} }
......
package net.mamoe.mirai.utils.config; package net.mamoe.mirai.utils.config;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
...@@ -10,18 +8,25 @@ import java.util.function.BiConsumer; ...@@ -10,18 +8,25 @@ import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
/**
* 实现了可以直接被继承的 SynchronizedLinkedListMap<K,V>
*
* @param <K>
* @param <V>
*/
public class MiraiSynchronizedLinkedListMap<K,V> extends AbstractMap<K,V> { public class MiraiSynchronizedLinkedListMap<K,V> extends AbstractMap<K,V> {
public MiraiSynchronizedLinkedListMap(){ public MiraiSynchronizedLinkedListMap(){
this.sortedMap = Collections.synchronizedMap(new LinkedHashMap<>()); this.sortedMap = Collections.synchronizedMap(new LinkedHashMap<>());
} }
protected Map<K, V> sortedMap; protected final Map<K, V> sortedMap;
protected void setContent(LinkedHashMap<K,V> map){ public MiraiSynchronizedLinkedListMap(LinkedHashMap<K,V> map){
this.sortedMap = Collections.synchronizedMap(map); this.sortedMap = Collections.synchronizedMap(map);
} }
@Override @Override
public int size() { public int size() {
return sortedMap.size(); return sortedMap.size();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment