mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-10 04:00:08 +08:00
SynchronizedLinkedListMap
This commit is contained in:
parent
155d883b58
commit
c8cb01d46f
@ -25,24 +25,8 @@ public class MiraiConfig extends MiraiConfigSection<Object> {
|
||||
}
|
||||
|
||||
public MiraiConfig(@NotNull File file) {
|
||||
super();
|
||||
Objects.requireNonNull(file);
|
||||
/*if (!file.toURI().getPath().contains(MiraiServer.getInstance().getParentFolder().getPath())) {
|
||||
file = new File(MiraiServer.getInstance().getParentFolder().getPath(), file.getName());
|
||||
}*/
|
||||
|
||||
super(parse(Objects.requireNonNull(file)));
|
||||
this.root = file;
|
||||
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
if (!file.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.parse();
|
||||
}
|
||||
|
||||
public synchronized void save() {
|
||||
@ -58,18 +42,29 @@ public class MiraiConfig extends MiraiConfigSection<Object> {
|
||||
}
|
||||
|
||||
@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.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
Yaml yaml = new Yaml(dumperOptions);
|
||||
try {
|
||||
LinkedHashMap<String, Object> content = yaml.loadAs(Utils.readFile(this.root), LinkedHashMap.class);
|
||||
if (content != null) {
|
||||
setContent(content);
|
||||
}
|
||||
return yaml.loadAs(Utils.readFile(file), LinkedHashMap.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,9 +7,14 @@ import java.util.Map;
|
||||
public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String, T> {
|
||||
|
||||
public MiraiConfigSection(){
|
||||
|
||||
super();
|
||||
}
|
||||
|
||||
public MiraiConfigSection(LinkedHashMap<String, T> map){
|
||||
super(map);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Integer getInt(String key){
|
||||
return Integer.valueOf(this.get(key).toString());
|
||||
@ -41,9 +46,9 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
|
||||
return (MiraiConfigSection<D>) content;
|
||||
}
|
||||
if(content instanceof Map){
|
||||
return new MiraiConfigSection<>(){{
|
||||
setContent((LinkedHashMap<String, D>) content);
|
||||
}};
|
||||
return new MiraiConfigSection<>(
|
||||
(LinkedHashMap<String, D>) content
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package net.mamoe.mirai.utils.config;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ -10,18 +8,25 @@ import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 实现了可以直接被继承的 SynchronizedLinkedListMap<K,V>
|
||||
*
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public class MiraiSynchronizedLinkedListMap<K,V> extends AbstractMap<K,V> {
|
||||
|
||||
public MiraiSynchronizedLinkedListMap(){
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return sortedMap.size();
|
||||
|
Loading…
Reference in New Issue
Block a user