YAML supported

This commit is contained in:
liujiahua123123 2019-08-21 11:48:37 +08:00
parent d27f6b8574
commit 23663e5823
4 changed files with 114 additions and 24 deletions

View File

@ -114,6 +114,24 @@ public class MiraiServer {
getLogger().info("ready to connect");
/*
this.qqs.put("test",new MiraiConfigSection<String>(){{
put("1","2");
put("11","2");
put("111","2");
put("1111","2");
}});
this.qqs.save();
*/
System.out.println(this.qqs.get());
/*
System.out.println(v);
System.out.println(v.get("1111"));
*/
System.exit(0);
Robot robot = new Robot(1994701021, "xiaoqqq");
try {
//System.out.println(Protocol.Companion.getSERVER_IP().get(3));

View File

@ -0,0 +1,82 @@
package net.mamoe.mirai.utils.config;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class MiraiAbstractConfigSection<K,V> implements Map<K,V> {
private SortedMap<K, V> sortedMap;
protected void setContent(SortedMap<K,V> map){
this.sortedMap = map;
}
@Override
public int size() {
return sortedMap.size();
}
@Override
public boolean isEmpty() {
return sortedMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return sortedMap.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return sortedMap.containsValue(value);
}
@Override
public V get(Object key) {
return sortedMap.get(key);
}
@Nullable
@Override
public V put(K key, V value) {
return sortedMap.put(key,value);
}
@Override
public V remove(Object key) {
return sortedMap.remove(key);
}
@Override
public void putAll(@NotNull Map<? extends K, ? extends V> m) {
sortedMap.putAll(m);
}
@Override
public void clear() {
sortedMap.clear();
}
@NotNull
@Override
public Set<K> keySet() {
return sortedMap.keySet();
}
@NotNull
@Override
public Collection<V> values() {
return sortedMap.values();
}
@NotNull
@Override
public Set<Entry<K, V>> entrySet() {
return sortedMap.entrySet();
}
}

View File

@ -69,6 +69,7 @@ public class MiraiConfig extends MiraiConfigSection<Object> {
Map<String, Object> content = yaml.loadAs(Utils.readFile(this.root), LinkedHashMap.class);
if (content != null) {
this.putAll(content);
System.out.println(this.keySet().toString());
}
} catch (IOException e) {
e.printStackTrace();

View File

@ -1,31 +1,15 @@
package net.mamoe.mirai.utils.config;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class MiraiConfigSection<T> extends ConcurrentSkipListMap<String, T> {
public class MiraiConfigSection<T> extends MiraiAbstractConfigSection<String, T>{
public MiraiConfigSection(){
/*
* Ensure the key will be in order
* */
super((a,b) -> 1);
}
@SuppressWarnings("unchecked")
public <D extends T> D getAs(String key){
return (D)this.get(key);
}
@SuppressWarnings("unchecked")
public <D extends T> D getAs(String key, D defaultV){
return (D)(this.getOrDefault(key,defaultV));
}
public Integer getInt(String key){
return Integer.valueOf(this.get(key).toString());
@ -45,13 +29,18 @@ public class MiraiConfigSection<T> extends ConcurrentSkipListMap<String, T> {
@SuppressWarnings("unchecked")
public <D extends T> MiraiConfigSection<D> getTypedSection(String key){
return (MiraiConfigSection<D>) this.getAs(key);
var content = (SortedMap<String,D>) this.get(key);
return new MiraiConfigSection<>(){{
setContent(Collections.synchronizedSortedMap(content));
}};
}
@SuppressWarnings("unchecked")
public MiraiConfigSection<Object> getSection(String key){
return (MiraiConfigSection<Object>) this.getAs(key);
var content = (SortedMap<String,Object>) this.get(key);
return new MiraiConfigSection<>(){{
setContent(Collections.synchronizedSortedMap(content));
}};
}
}