mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-07-03 13:57:26 +00:00
[Add] MessageSenderBuilder, MessageSenderFactory 增加用于创建MessageSender而又与平台无关的Builder, 初步增加对应Factory;
This commit is contained in:
@ -0,0 +1,43 @@
|
||||
package net.lamgc.cgj.bot.message;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* 消息发送器构造
|
||||
*/
|
||||
public final class MessageSenderBuilder {
|
||||
|
||||
private final static AtomicReference<MessageSenderFactory> currentFactory = new AtomicReference<>();
|
||||
|
||||
private MessageSenderBuilder() {}
|
||||
|
||||
/**
|
||||
* 获取消息发送器
|
||||
* @param source 消息源类型
|
||||
* @param id 消息源Id
|
||||
* @return 返回新建的发送器
|
||||
*/
|
||||
public static MessageSender getMessageSender(MessageSource source, long id) {
|
||||
MessageSenderFactory messageSenderFactory = currentFactory.get();
|
||||
if(messageSenderFactory == null) {
|
||||
throw new IllegalStateException("The factory is not ready");
|
||||
}
|
||||
try {
|
||||
return messageSenderFactory.createMessageSender(source, id);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置消息发送器工厂
|
||||
* @param factory 工厂对象
|
||||
*/
|
||||
public static void setCurrentMessageSenderFactory(MessageSenderFactory factory) {
|
||||
if(currentFactory.get() != null) {
|
||||
throw new IllegalStateException("Factory already exists");
|
||||
}
|
||||
currentFactory.set(factory);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user