From c8ef160f3af3a669ff1a052911f5364d1d15888a Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 16 Oct 2020 09:54:23 +0800 Subject: [PATCH] =?UTF-8?q?[Add][Change]=20Framework-API=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=20BotCode=20=E6=8E=A5=E5=8F=A3,=20=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E6=8A=BD=E5=87=BA=E8=BD=AC=E6=8D=A2=E7=9B=B8=E5=85=B3=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=88=B0=E8=BD=AC=E6=8D=A2=E5=99=A8=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=AD,=20=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA=20BotCode=20?= =?UTF-8?q?=E6=8A=BD=E8=B1=A1=E7=B1=BB;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] BotCodeConverter 添加 BotCode 转换类(后续可能就不需要分化 BotCode 实现了?); [Change] BotCode 抽出 BotCode 转换相关代码到 BotCodeConverter; [Add] AbstractBotCode 添加一个实现了相关细节的 BotCode 抽象类; --- .../framework/message/AbstractBotCode.java | 117 ++++++++++++++++++ .../cgj/bot/framework/message/BotCode.java | 20 --- .../framework/message/BotCodeConverter.java | 48 +++++++ 3 files changed, 165 insertions(+), 20 deletions(-) create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/AbstractBotCode.java create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCodeConverter.java 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 new file mode 100644 index 0000000..37b7e39 --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/AbstractBotCode.java @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2020 LamGC + * + * ContentGrabbingJi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ContentGrabbingJi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.lamgc.cgj.bot.framework.message; + +import net.lamgc.cgj.bot.framework.util.CollectionUtils; + +import java.util.*; + +/** + * 可用于快速实现的抽象功能码. + * @author LamGC + * @see BotCode + */ +public abstract class AbstractBotCode implements BotCode { + + private String functionName; + private final Map functionProperties = new Hashtable<>(); + + public AbstractBotCode() {} + + /** + * 将其他实现的 BotCode 转换成该实现的 BotCode. + * @param botCode 待转换的 BotCode. + */ + public AbstractBotCode(BotCode botCode) { + this(botCode.getFunctionName(), CollectionUtils.toMap(botCode.getPropertiesKeys(), botCode::getProperty)); + } + + /** + * 根据给定的功能名和参数 Map 构造 BotCode. + * @param functionName 功能名 + * @param functionProperties 参数集 Map. 如果不需要可传入 null. + */ + public AbstractBotCode(String functionName, Map functionProperties) { + this.functionName = functionName; + if(functionProperties != null) { + 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{" + + "Platform=" + getPlatform() + ", " + + "functionName='" + functionName + '\'' + + ", functionProperties=" + mapString.substring(0, mapString.length() - 2) + + '}'; + } + + /** + * 取功能函数名. + * @return 返回功能函数名. + */ + @Override + public String getFunctionName() { + return functionName; + } + + /** + * 设置功能函数名 + * @param functionName 新的功能函数名. + */ + @Override + public void setFunctionName(String functionName) { + this.functionName = functionName; + } + + /** + * 设置功能参数 + * @param key 参数名 + * @param value 参数值, 如果参数值为 {@code null} 则删除该参数. + * @throws NullPointerException 当 key 为 null 时抛出. + */ + @Override + public void setProperty(String key, String value) { + Objects.requireNonNull(key); + if(value == null) { + functionProperties.remove(key); + return; + } + functionProperties.put(key, value); + } + + /** + * 获取功能参数 + * @param key 参数名 + * @return 如果存在, 返回参数名, 否则返回 {@code null}. + * @throws NullPointerException 当 key 为 null 时抛出. + */ + @Override + public String getProperty(String key) { + return functionProperties.get(Objects.requireNonNull(key)); + } + + @Override + public Set getPropertiesKeys() { + return Collections.unmodifiableSet(functionProperties.keySet()); + } + +} diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCode.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCode.java index f3db467..275bf24 100644 --- a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCode.java +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCode.java @@ -18,9 +18,6 @@ package net.lamgc.cgj.bot.framework.message; import net.lamgc.cgj.bot.framework.Platform; -import net.lamgc.cgj.bot.framework.message.exception.BuildBotCodeException; -import net.lamgc.cgj.bot.framework.message.exception.InvalidBotCodeException; -import net.lamgc.cgj.bot.framework.message.exception.UnsupportedBotCodeException; import java.util.Set; @@ -36,23 +33,6 @@ public interface BotCode { */ Platform getPlatform(); - /** - * 转换为平台或框架可识别并处理的BotCode字符串形式. - * @return 返回转换后的结果. - * @throws UnsupportedBotCodeException 当框架不支持该 BotCode 时抛出. - * @throws BuildBotCodeException 当 BotCode 无法构造出字符串形式时抛出, 包含原因. - */ - String toBotCodeString() throws UnsupportedBotCodeException, BuildBotCodeException; - - /** - * 从 BotCode 字符串转换成 BotCode 对象. - * @param botCodeString 传入的 BotCode 字符串. - * @throws InvalidBotCodeException 当传入的 BotCode 字符串无法转换成该实现对应的 BotCode 对象时可抛出该异常, - * 务必在异常中清晰说明异常原因. - * @throws UnsupportedBotCodeException 当框架不支持该 BotCode 时抛出. - */ - void fromBotCodeString(String botCodeString) throws InvalidBotCodeException, UnsupportedBotCodeException; - /** * 取功能函数名. * @return 返回功能函数名. diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCodeConverter.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCodeConverter.java new file mode 100644 index 0000000..7d41633 --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/BotCodeConverter.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 LamGC + * + * ContentGrabbingJi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * ContentGrabbingJi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package net.lamgc.cgj.bot.framework.message; + +import net.lamgc.cgj.bot.framework.message.exception.BuildBotCodeException; +import net.lamgc.cgj.bot.framework.message.exception.InvalidBotCodeException; +import net.lamgc.cgj.bot.framework.message.exception.UnsupportedBotCodeException; + +/** + * BotCode 转换器. + * @author LamGC + */ +public interface BotCodeConverter { + + /** + * 转换为平台或框架可识别并处理的 BotCode 字符串形式. + * @param botCode BotCode 对象. + * @return 返回转换后的结果. + * @throws UnsupportedBotCodeException 当框架不支持该 BotCode 时抛出. + * @throws BuildBotCodeException 当 BotCode 无法构造出字符串形式时抛出, 包含原因. + */ + String toBotCodeString(BotCode botCode) throws UnsupportedBotCodeException, BuildBotCodeException; + + /** + * 从 BotCode 字符串转换成 BotCode 对象. + * @param botCodeString 传入的 BotCode 字符串. + * @throws InvalidBotCodeException 当传入的 BotCode 字符串无法转换成该实现对应的 BotCode 对象时可抛出该异常, + * 务必在异常中清晰说明异常原因. + * @throws UnsupportedBotCodeException 当框架不支持该 BotCode 时抛出. + */ + void fromBotCodeString(String botCodeString) throws InvalidBotCodeException, UnsupportedBotCodeException; + +}