From 2520c79c504a4397cac5d4e74229df8baaa1f241 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 21 Nov 2020 09:13:17 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20Framework-API=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=20AbstractBotCode=20=E4=B8=AD=20'toString()'=20=E7=9A=84?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=BB=86=E8=8A=82,=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=A3=80=E6=9F=A5;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Change] AbstractBotCode 调整 'toString()', 整理实现细节并修复格式错误, 在构造方法中添加对 functionName 的非空检查, 补充对 functionProperties 的非空判断条件, 补充文档; --- .../framework/message/AbstractBotCode.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/AbstractBotCode.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/AbstractBotCode.java index 0548f69..c6ab104 100644 --- a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/AbstractBotCode.java +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/AbstractBotCode.java @@ -17,6 +17,7 @@ package net.lamgc.cgj.bot.framework.message; +import com.google.common.base.Strings; import net.lamgc.cgj.bot.framework.util.CollectionUtils; import java.util.*; @@ -32,7 +33,7 @@ public abstract class AbstractBotCode implements BotCode { private final Map functionProperties = new Hashtable<>(); public AbstractBotCode(String functionName) { - this.functionName = functionName; + this(functionName, null); } /** @@ -49,20 +50,31 @@ public abstract class AbstractBotCode implements BotCode { * @param functionProperties 参数集 Map. 如果不需要可传入 null. */ public AbstractBotCode(String functionName, Map functionProperties) { + if (Strings.isNullOrEmpty(functionName)) { + throw new IllegalArgumentException("functionName is null or empty"); + } this.functionName = functionName; - if(functionProperties != null) { + if(functionProperties != null && !functionProperties.isEmpty()) { this.functionProperties.putAll(functionProperties); } } @Override public String toString() { - StringBuilder mapString = new StringBuilder(functionProperties.getClass().getSimpleName() + "{"); - functionProperties.forEach((key, value) -> mapString.append(key).append("='").append(value).append("', ")); - return "AbstractBotCode{" + + StringBuilder mapString = new StringBuilder(functionProperties.getClass().getSimpleName()); + if (!functionProperties.isEmpty()) { + mapString.append("{"); + functionProperties.forEach((key, value) -> { + // "key"="value" + mapString.append('"').append(key).append("\"='").append(value).append("', "); + + }); + mapString.delete(mapString.length() - 2, mapString.length()); + } + return this.getClass().getSimpleName() + '@' + Integer.toHexString(this.hashCode()) + '{' + "Platform=" + getPlatform() + ", " + "functionName='" + functionName + '\'' + - ", functionProperties=" + mapString.substring(0, mapString.length() - 2) + + ", functionProperties={" + mapString.toString() + '}' + '}'; } @@ -111,6 +123,11 @@ public abstract class AbstractBotCode implements BotCode { return functionProperties.get(Objects.requireNonNull(key)); } + /** + * 获取参数 Keys 集合. + *

注意: 通过本方法返回的 Set 不可进行修改. + * @return 返回包含所有参数 Keys 的 Set 对象. + */ @Override public Set getPropertiesKeys() { return Collections.unmodifiableSet(functionProperties.keySet());