From b2fc1968304f62d256c18fae3b07a8a8e14e14bf Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 19 Nov 2020 22:12:43 +0800 Subject: [PATCH] =?UTF-8?q?[Add][Change]=20Framework-API,=20Core=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Message=20=E7=B1=BB=E5=9E=8B=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E6=B6=88=E6=81=AF=E5=85=83=E7=B4=A0=E7=9A=84=E5=9F=BA?= =?UTF-8?q?=E7=B1=BB,=20BotCode=20=E7=BB=A7=E6=89=BF=20Message,=20Abstract?= =?UTF-8?q?BotCode=20=E5=AE=9E=E7=8E=B0=20BotCode=20=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=96=B9=E6=B3=95=E7=BB=86=E8=8A=82;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Add] Message, MessageChain 添加 Message 接口作为消息元素的基类, 添加 MessageChain 用于链接消息元素; [Change] BotCode, AbstractBotCode BotCode 继承 Message 接口以作为消息的一部分, AbstractBotCode 实现部分方法细节; [Add] BasicBotCode 适配调整, 添加新的构造方法; --- .../cgj/bot/framework/base/BasicBotCode.java | 14 +++ .../framework/message/AbstractBotCode.java | 15 +++ .../cgj/bot/framework/message/BotCode.java | 3 +- .../cgj/bot/framework/message/Message.java | 32 ++++++ .../bot/framework/message/MessageChain.java | 103 ++++++++++++++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/Message.java create mode 100644 ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageChain.java diff --git a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/base/BasicBotCode.java b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/base/BasicBotCode.java index f2ca3a3..e578b1a 100644 --- a/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/base/BasicBotCode.java +++ b/ContentGrabbingJi-core/src/main/java/net/lamgc/cgj/bot/framework/base/BasicBotCode.java @@ -19,9 +19,11 @@ package net.lamgc.cgj.bot.framework.base; import net.lamgc.cgj.bot.framework.Platform; import net.lamgc.cgj.bot.framework.message.AbstractBotCode; +import net.lamgc.cgj.bot.framework.message.BotCode; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.util.Map; /** * ContentGrabbingJi 内部 BotCode 实现. @@ -32,6 +34,18 @@ public class BasicBotCode extends AbstractBotCode { private final static Platform PLATFORM = new Platform("ContentGrabbingJi", "CGJ"); + public BasicBotCode(String functionName) { + super(functionName); + } + + public BasicBotCode(BotCode botCode) { + super(botCode); + } + + public BasicBotCode(String functionName, Map functionProperties) { + super(functionName, functionProperties); + } + @Override public Platform getPlatform() { return PLATFORM; 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..4d34adb 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 @@ -116,4 +116,19 @@ public abstract class AbstractBotCode implements BotCode { return Collections.unmodifiableSet(functionProperties.keySet()); } + @Override + public int length() { + return contentToString().length(); + } + + @Override + public char charAt(int index) { + return contentToString().charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return contentToString().subSequence(start, end); + } + } 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 275bf24..415afc1 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 @@ -25,7 +25,8 @@ import java.util.Set; * 功能码接口. * @author LamGC */ -public interface BotCode { +@SuppressWarnings("AlibabaAbstractMethodOrInterfaceMethodMustUseJavadoc") +public interface BotCode extends Message { /** * 获取 BotCode 实现所属平台. diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/Message.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/Message.java new file mode 100644 index 0000000..9493cb9 --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/Message.java @@ -0,0 +1,32 @@ +/* + * 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; + +/** + * 消息接口. + * @author LamGC + */ +public interface Message extends CharSequence { + + /** + * 消息内容到文本. + * @return 返回内容字符串. + */ + String contentToString(); + +} diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageChain.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageChain.java new file mode 100644 index 0000000..fdbc076 --- /dev/null +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/message/MessageChain.java @@ -0,0 +1,103 @@ +/* + * 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 java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * 消息链. + *

消息链代表了由一组元素组成的消息, 是一个消息内容的整体. + * @author LamGC + */ +public final class MessageChain implements Message { + + private final List contents = new CopyOnWriteArrayList<>(); + + public MessageChain() {} + + public MessageChain(CharSequence... contents) { + plus(contents); + } + + public void plus(CharSequence... content) { + addMultiContents(content); + } + + public void plus(Message... message) { + addMultiContents(message); + } + + public void plus(BotCode... botCode) { + addMultiContents(botCode); + } + + /** + * 添加多个内容. + * @param contents 内容数组. + * @see #addContent(Message) 底层实现. + */ + private void addMultiContents(CharSequence[] contents) { + for (CharSequence content : contents) { + if (content instanceof Message) { + addContent((Message) content); + } else { + addContent(new CharSequenceMessage(content)); + } + } + } + + /** + * 添加消息内容. + *

plus 方法必须且只能通过该方法将内容段添加到 {@link #contents}, + * 因为需要防止 MessageChain 自己添加自己. + * @param content 消息内容. + * @throws IllegalArgumentException 不允许 MessageChain 添加本身. + */ + private void addContent(Message content) { + if (this.equals(content)) { + throw new IllegalArgumentException("Adding the MessageChain itself is not allowed"); + } + contents.add(content); + } + + @Override + public String contentToString() { + StringBuilder builder = new StringBuilder(); + for (Message messageElement : contents) { + builder.append(messageElement.contentToString()); + } + return builder.toString(); + } + + @Override + public int length() { + return contentToString().length(); + } + + @Override + public char charAt(int index) { + return contentToString().charAt(index); + } + + @Override + public CharSequence subSequence(int start, int end) { + return contentToString().subSequence(start, end); + } + +}