From 04298370116d98832dcbfd76fd754065395c2d45 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 11 May 2020 13:22:09 +0800 Subject: [PATCH] Cleanup --- mirai-console/build.gradle.kts | 2 + .../mamoe/mirai/console/utils/CommandArg.kt | 70 +++++++------------ .../net/mamoe/mirai/console/utils/Utils.java | 23 +++--- .../net/mamoe/mirai/console/utils/Value.kt | 27 ++++--- 4 files changed, 51 insertions(+), 71 deletions(-) diff --git a/mirai-console/build.gradle.kts b/mirai-console/build.gradle.kts index dea1f41a2..8a1eb1389 100644 --- a/mirai-console/build.gradle.kts +++ b/mirai-console/build.gradle.kts @@ -33,6 +33,8 @@ dependencies { api(group = "com.moandjiezana.toml", name = "toml4j", version = "0.7.2") api("org.jsoup:jsoup:1.12.1") + api("org.jetbrains:annotations:19.0.0") + testApi("net.mamoe:mirai-core-qqandroid:${Versions.Mirai.core}") testApi(kotlin("stdlib")) } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt index 8a121d908..d335f662a 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/CommandArg.kt @@ -9,19 +9,18 @@ import net.mamoe.mirai.contact.Group * this output type of that arg * input is always String */ -interface CommandArg{ - operator fun invoke():T = get() +interface CommandArg { + operator fun invoke(): T = get() - fun get():T + fun get(): T - fun read(s:String, commandSender: CommandSender) + fun read(s: String, commandSender: CommandSender) } -abstract class CommandArgImpl( -):CommandArg{ +abstract class CommandArgImpl : CommandArg { - lateinit var value:T + lateinit var value: T override fun get(): T = value @@ -29,44 +28,24 @@ abstract class CommandArgImpl( value = parse(s, commandSender) } - abstract fun parse(s:String, commandSender: CommandSender):T + abstract fun parse(s: String, commandSender: CommandSender): T } -class IntArg:CommandArgImpl(){ - override fun parse(s: String, commandSender: CommandSender): Int { - return try{ - s.toInt() - }catch (e:Exception){ - error("无法识别整数$s") - } - } +class IntArg : CommandArgImpl() { + override fun parse(s: String, commandSender: CommandSender): Int = s.toInt() } -class LongArg:CommandArgImpl(){ - override fun parse(s: String, commandSender: CommandSender): Long { - return try{ - s.toLong() - }catch (e:Exception){ - error("无法识别长整数$s") - } - } +class LongArg : CommandArgImpl() { + override fun parse(s: String, commandSender: CommandSender): Long = s.toLong() } -class DoubleArg:CommandArgImpl(){ - override fun parse(s: String, commandSender: CommandSender): Double { - return try{ - s.toDouble() - }catch (e:Exception){ - error("无法识别小数$s") - } - } +class DoubleArg : CommandArgImpl() { + override fun parse(s: String, commandSender: CommandSender): Double = s.toDouble() } -class StringArg:CommandArgImpl(){ - override fun parse(s: String, commandSender: CommandSender): String { - return s - } +class StringArg : CommandArgImpl() { + override fun parse(s: String, commandSender: CommandSender): String = s } /** @@ -76,33 +55,32 @@ class StringArg:CommandArgImpl(){ * errors: String->Int convert, Bot Not Exist */ -class ExistBotArg:CommandArgImpl(){ +class ExistBotArg : CommandArgImpl() { override fun parse(s: String, commandSender: CommandSender): Bot { - val uin = try{ + val uin = try { s.toLong() - }catch (e:Exception){ + } catch (e: Exception) { error("无法识别QQ UIN$s") } - return try{ + return try { Bot.getInstance(uin) - }catch (e:NoSuchElementException){ + } catch (e: NoSuchElementException) { error("无法找到Bot $uin") } } } - -class ExistGroupArg:CommandArgImpl(){ +class ExistGroupArg : CommandArgImpl() { override fun parse(s: String, commandSender: CommandSender): Group { - if((s === "" || s === "~") && commandSender is GroupContactCommandSender){ + if ((s === "" || s === "~") && commandSender is GroupContactCommandSender) { return commandSender.contact as Group } - val code = try{ + val code = try { s.toLong() - }catch (e:Exception){ + } catch (e: Exception) { error("无法识别Group Code$s") } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java index 72f9d3f2a..6f2c654db 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java @@ -1,22 +1,18 @@ package net.mamoe.mirai.console.utils; +import org.jetbrains.annotations.Range; + import java.util.concurrent.Callable; -public class Utils { +public final class Utils { /** * 执行N次 callable * 成功一次就会结束 * 否则就会throw */ - public static T tryNTimes( - /*@Range(from=1, to=Integer.MAX_VALUE)*/ - int n, - Callable callable - ) throws Exception { - if (n < 0) { - throw new IllegalArgumentException("Must be executed at least once."); - } + public static T tryNTimes(@Range(from = 1, to = Integer.MAX_VALUE) int n, + Callable callable) throws Exception { Exception last = null; while (n-- > 0) { @@ -26,15 +22,14 @@ public class Utils { if (last == null) { last = e; } else { - last.addSuppressed(e); + try { + last.addSuppressed(e); + } catch (Throwable ignored) { + } } } } - if (last == null) { - throw new Exception("unknown error"); - } - throw last; } } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt index 81e5296ba..85a3ba822 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Value.kt @@ -1,22 +1,25 @@ package net.mamoe.mirai.console.utils +import net.mamoe.mirai.utils.MiraiExperimentalAPI + /** * A Value * the input type of this Value is T while the output is V */ -abstract class Value{ - operator fun invoke():V = get() +@MiraiExperimentalAPI +abstract class Value { + operator fun invoke(): V = get() - abstract fun get():V + abstract fun get(): V - abstract fun set(t:T) + abstract fun set(t: T) } - /** * This value can be used as a Config Value */ +@MiraiExperimentalAPI interface ConfigValue @@ -25,9 +28,10 @@ interface ConfigValue * the input type is same as output value */ +@MiraiExperimentalAPI open class SimpleValue( - var value:T -):Value() { + var value: T +) : Value() { override fun get() = this.value override fun set(t: T) { @@ -35,11 +39,12 @@ open class SimpleValue( } } +@MiraiExperimentalAPI open class NullableSimpleValue( - value:T? = null -):SimpleValue( + value: T? = null +) : SimpleValue( value -){ - fun isNull() = value == null +) { + fun isNull() = value == null }