mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Add][Change] Framework-API 调整 BotCode 接口, 单独抽出转换相关方法到转换器接口中, 添加一个 BotCode 抽象类;
[Add] BotCodeConverter 添加 BotCode 转换类(后续可能就不需要分化 BotCode 实现了?); [Change] BotCode 抽出 BotCode 转换相关代码到 BotCodeConverter; [Add] AbstractBotCode 添加一个实现了相关细节的 BotCode 抽象类;
This commit is contained in:
parent
f1df151c89
commit
c8ef160f3a
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<String, String> 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<String, String> 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<String> getPropertiesKeys() {
|
||||
return Collections.unmodifiableSet(functionProperties.keySet());
|
||||
}
|
||||
|
||||
}
|
@ -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 返回功能函数名.
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user