Enhanced ConfigSection

This commit is contained in:
Him188moe 2019-08-28 22:18:22 +08:00
parent 114e652810
commit 4b889141cc
3 changed files with 38 additions and 23 deletions

View File

@ -12,7 +12,7 @@ public class Robot {
private final int qq; private final int qq;
private final String password; private final String password;
private final RobotNetworkHandler handler ; private final RobotNetworkHandler handler;
/** /**
* Ref list * Ref list
@ -20,7 +20,7 @@ public class Robot {
@Getter @Getter
private final List<String> owners; private final List<String> owners;
public boolean isOwnBy(String ownerName){ public boolean isOwnBy(String ownerName) {
return owners.contains(ownerName); return owners.contains(ownerName);
} }
@ -29,28 +29,28 @@ public class Robot {
this( this(
data.getIntOrThrow("account", () -> new Exception("can not parse QQ account")), data.getIntOrThrow("account", () -> new Exception("can not parse QQ account")),
data.getStringOrThrow("password", () -> new Exception("can not parse QQ password")), data.getStringOrThrow("password", () -> new Exception("can not parse QQ password")),
data.getAsOrDefault("owners",new ArrayList<>()) data.getAsOrDefault("owners", ArrayList::new)
); );
} }
public Robot(int qq, String password, List<String> owners){ public Robot(int qq, String password, List<String> owners) {
this.qq = qq; this.qq = qq;
this.password = password; this.password = password;
this.owners = Collections.unmodifiableList(owners); this.owners = Collections.unmodifiableList(owners);
this.handler = new RobotNetworkHandler(this.qq,this.password); this.handler = new RobotNetworkHandler(this.qq, this.password);
} }
public void connect(){ public void connect() {
} }
public void onPacketReceive(){ public void onPacketReceive() {
} }

View File

@ -18,7 +18,7 @@ import java.net.InetSocketAddress
import java.util.* import java.util.*
/** /**
* A robotNetworkHandler account. * A RobotNetworkHandler is used to connect with Tencent servers.
* *
* @author Him188moe * @author Him188moe
*/ */

View File

@ -1,10 +1,11 @@
package net.mamoe.mirai.utils.config; package net.mamoe.mirai.utils.config;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.IllegalFormatException;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.function.Supplier;
public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String, T> { public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String, T> {
@ -12,18 +13,18 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
super(); super();
} }
public MiraiConfigSection(LinkedHashMap<String, T> map){ public MiraiConfigSection(Map<String, T> copyOfMap) {
super(map); super(new LinkedHashMap<>(copyOfMap));
} }
public int getInt(String key){ public int getInt(String key){
return Integer.valueOf(this.get(key).toString()); return Integer.parseInt(this.get(key).toString());
} }
public int getIntOrDefault(String key, int defaultV){ public int getIntOrDefault(String key, int defaultV){
Object result = this.getOrDefault(key, null); Object result = this.getOrDefault(key, null);
try { try {
return result == null ? defaultV : Integer.valueOf(result.toString()); return result == null ? defaultV : Integer.parseInt(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
return defaultV; return defaultV;
} }
@ -35,20 +36,20 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
throw throwableCallable.call(); throw throwableCallable.call();
} }
try { try {
return Integer.valueOf(result.toString()); return Integer.parseInt(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
throw throwableCallable.call(); throw throwableCallable.call();
} }
} }
public double getDouble(String key){ public double getDouble(String key){
return Double.valueOf(this.get(key).toString()); return Double.parseDouble(this.get(key).toString());
} }
public double getDoubleOrDefault(String key, double defaultV){ public double getDoubleOrDefault(String key, double defaultV){
Object result = this.getOrDefault(key, null); Object result = this.getOrDefault(key, null);
try { try {
return result==null?defaultV:Double.valueOf(result.toString()); return result == null ? defaultV : Double.parseDouble(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
return defaultV; return defaultV;
} }
@ -60,20 +61,20 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
throw throwableCallable.call(); throw throwableCallable.call();
} }
try { try {
return Double.valueOf(result.toString()); return Double.parseDouble(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
throw throwableCallable.call(); throw throwableCallable.call();
} }
} }
public float getFloat(String key){ public float getFloat(String key){
return Float.valueOf(this.get(key).toString()); return Float.parseFloat(this.get(key).toString());
} }
public float getFloatOrDefault(String key, float defaultV){ public float getFloatOrDefault(String key, float defaultV){
Object result = this.getOrDefault(key, null); Object result = this.getOrDefault(key, null);
try { try {
return result == null ? defaultV : Float.valueOf(result.toString()); return result == null ? defaultV : Float.parseFloat(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
return defaultV; return defaultV;
} }
@ -85,20 +86,20 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
throw throwableCallable.call(); throw throwableCallable.call();
} }
try { try {
return Float.valueOf(result.toString()); return Float.parseFloat(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
throw throwableCallable.call(); throw throwableCallable.call();
} }
} }
public long getLong(String key){ public long getLong(String key){
return Long.valueOf(this.get(key).toString()); return Long.parseLong(this.get(key).toString());
} }
public long getLongOrDefault(String key, long defaultV){ public long getLongOrDefault(String key, long defaultV){
Object result = this.getOrDefault(key, null); Object result = this.getOrDefault(key, null);
try { try {
return result == null ? defaultV : Long.valueOf(result.toString()); return result == null ? defaultV : Long.parseLong(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
return defaultV; return defaultV;
} }
@ -110,7 +111,7 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
throw throwableCallable.call(); throw throwableCallable.call();
} }
try { try {
return Long.valueOf(result.toString()); return Long.parseLong(result.toString());
}catch (NumberFormatException ignored){ }catch (NumberFormatException ignored){
throw throwableCallable.call(); throw throwableCallable.call();
} }
@ -161,4 +162,18 @@ public class MiraiConfigSection<T> extends MiraiSynchronizedLinkedListMap<String
public <D extends T> D getAsOrDefault(String key, D defaultV){ public <D extends T> D getAsOrDefault(String key, D defaultV){
return (D)this.getOrDefault(key,defaultV); return (D)this.getOrDefault(key,defaultV);
} }
@SuppressWarnings("unchecked")
public <D extends T> D getAsOrDefault(String key, Supplier<D> supplier) {
D d = (D) this.get(key);
if (d != null) {
return d;
}
return supplier.get();
}
@SuppressWarnings("unchecked")
public <D extends T> D getAs(String key) {
return (D) this.get(key);
}
} }