[Add] Project, Event, Core, Framework-API 增加 Event 模块;

[Add] EventExecutor, EventObject, EventHandler 添加基本的事件类;
[Add] AbstractEventObject 添加完成了 EventObject 所需方法的抽象类;
[Add] EventHandlerRegistry 添加事件处理注册器接口, 分离与事件处理注册的部分;
[Add] Core/pom.xml, Event/pom.xml, Framework-API/pom.xml, Project/pom.xml 添加 Event 模块并在所需模块中引入;
[Add] Cancelable, SupportedCancel 添加与事件取消有关的接口;
[Add] EventUtils 添加与事件处理有关的工具类;
[Add] EventUtilsTest, TestEvent, StandardHandler, IllegalHandler 添加针对 EventUtils 的完整单元测试;
This commit is contained in:
LamGC 2020-11-04 22:50:46 +08:00
parent 0d80d21144
commit 235c7452b8
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
16 changed files with 633 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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/>.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ContentGrabbingJi</artifactId>
<groupId>net.lamgc</groupId>
<version>3.0.0-alpha-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ContentGrabbingJi-Event</artifactId>
</project>

View File

@ -0,0 +1,36 @@
/*
* 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.event;
import java.util.UUID;
/**
* 抽象事件对象.
* <p> 已完成对 {@link EventObject#getEventId()} 的实现.
* @author LamGC
*/
public abstract class AbstractEventObject implements EventObject {
private final UUID eventId = UUID.randomUUID();
@Override
public UUID getEventId() {
return eventId;
}
}

View File

@ -0,0 +1,53 @@
/*
* 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.event;
import java.util.Observer;
/**
* 可取消接口.
* <p> 实现了该接口的事件可对其处理进行取消.
* <p> 取消状态不可撤回.
* @author LamGC
*/
public interface Cancelable {
/**
* 检查事件是否已被取消.
* @return 如果事件已被取消, 返回 true.
*/
boolean canceled();
/**
* 注册事件取消监听器.
* <p> 注意: 如果发生取消事件, 无论 {@link java.util.Observable} 是否为事件对象本身, 参数 {@code arg} 都必须传递事件对象自身!
* @param cancelObserver 观察者对象.
* @throws UnsupportedOperationException 当该可取消对象不支持 {@link java.util.Observable} 时抛出,
* 既然不支持观察取消事件, 那么 {@link #observableCancel()} 应当返回 {@code false}, 否则该方法不允许抛出该异常.
*/
void registerCancelObserver(Observer cancelObserver) throws UnsupportedOperationException;
/**
* 是否可观察取消事件.
* <p> 如果本方法返回 {@code true},
* 那么 {@link #registerCancelObserver(Observer)} 不允许抛出 {@link UnsupportedOperationException} 异常.
* @return 如果可以, 返回 true.
*/
boolean observableCancel();
}

View File

@ -0,0 +1,38 @@
/*
* 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.event;
/**
* 事件执行器.
* @author LamGC
*/
public interface EventExecutor {
/**
* 执行事件.
* @param event 事件对象.
*/
void execute(EventObject event);
/**
* 是否为异步事件执行器.
* @return 如果为异步事件执行器, 返回 {@code true}, 如果为同步事件执行器, 返回 {@code false}.
*/
boolean isAsync();
}

View File

@ -0,0 +1,45 @@
/*
* 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.event;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 事件处理器注解.
* <p> 标记了该注解的方法, 如符合处理方法条件, 则会被 {@link EventHandlerRegistry} 注册为事件处理方法.
* @author LamGC
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventHandler {
/**
* 是否接受事件对象的子类.
* <p> 当该选项为 {@code true} , 子类事件也会传递到该方法进行处理.
* 例如事件 Parent, 和它的子类 Child, 处理方法: {@code handler(Parent event)};
* 如果该选项为 {@code true}, 那么 Child 也会传递到该方法进行处理, 反之, 如果该选项为 {@code false},
* 那么该方法只会接收 Parent 事件, 而不会接收它的子类 Child.
* <p> 默认值: {@code false}
* @return 返回该方法是否支持继承性.
*/
boolean inheritable() default false;
}

View File

@ -0,0 +1,44 @@
/*
* 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.event;
import java.lang.reflect.Method;
import java.util.Map;
/**
* 事件处理注册器.
* <p> 实现了该接口的类将允许注册事件处理方法.
* @author LamGC
*/
public interface EventHandlerRegistry {
/**
* 注册对象中的事件处理方法.
* @param handlerObject 包含事件处理方法的对象.\
* @return 返回已成功添加的方法数.
*/
int registerHandler(Object handlerObject);
/**
* 获取能处理指定事件的所有方法.
* @param event 待匹配的事件对象.
* @return 返回一个集合, 集合存储了可处理该事件的所有方法.
*/
Map<Method, Object> getMatchedHandlerMethod(EventObject event);
}

View File

@ -0,0 +1,35 @@
/*
* 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.event;
import java.util.UUID;
/**
* 事件对象.
* @author LamGC
*/
public interface EventObject {
/**
* 获取事件 UUID.
* <p> 需保证 UUID 对应了唯一的事件对象, 即使内容重复.
* @return 返回事件Id.
*/
UUID getEventId();
}

View File

@ -0,0 +1,62 @@
/*
* 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.event;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
/**
* 事件处理工具类.
* @author LamGC
*/
public final class EventUtils {
/**
* 检查 {@link EventExecutor} 是否支持取消事件.
* @param executor 事件执行器.
* @return 如果支持, 返回 {@code true}
*/
public static boolean isSupportedCancel(EventExecutor executor) {
return executor instanceof SupportedCancel;
}
/**
* 检查方法是否符合事件处理方法条件.
* @param method 待检查的方法.
* @return 如果符合, 返回 true.
*/
public static boolean checkEventHandlerMethod(Method method) {
int modifiers = method.getModifiers();
// 新版事件系统将不再允许静态方法作为事件处理方法, 以降低管理难度.
if (!Modifier.isPublic(modifiers) || Modifier.isAbstract(modifiers) || Modifier.isStatic(modifiers)) {
return false;
}
if (!method.isAnnotationPresent(EventHandler.class) || method.getParameterCount() != 1) {
return false;
}
Class<?> param = method.getParameterTypes()[0];
if (!EventObject.class.isAssignableFrom(param)) {
return false;
}
return method.getReturnType().equals(Void.TYPE);
}
}

View File

@ -0,0 +1,56 @@
/*
* 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.event;
import java.util.NoSuchElementException;
import java.util.UUID;
/**
* 支持取消接口.
* <p> 实现了该接口的 {@link EventExecutor} 可对已投递执行的事件进行取消, 以阻止事件被执行.
* @author LamGC
*/
public interface SupportedCancel {
/**
* 通过 EventId 取消事件的处理.
* @param eventId 事件Id.
* @return 如果成功, 返回 true, 如果事件已执行完成, 返回 false.
* @throws UnsupportedOperationException 当事件未实现 {@link Cancelable} 接口时抛出.
* @throws NoSuchElementException EventId 所属事件在 {@link SupportedCancel} 中无法找到时抛出.
*/
boolean cancelEvent(UUID eventId) throws UnsupportedOperationException, NoSuchElementException;
/**
* 通过 Event 对象取消事件的处理.
* @param event 事件对象.
* @return 如果成功, 返回 true, 如果事件已执行完成, 返回 false.
* @throws UnsupportedOperationException 当事件未实现 {@link Cancelable} 接口时抛出.
* @throws NoSuchElementException EventId 所属事件在 {@link SupportedCancel} 中无法找到时抛出.
*/
boolean cancelEvent(EventObject event) throws UnsupportedOperationException, NoSuchElementException;
/**
* 对可取消对象执行取消处理操作.
* @param cancelableEvent 可取消对象事件.
* @return 如果成功, 返回 true, 如果事件已执行完成, 返回 false.
* @throws NoSuchElementException EventId 所属事件在 {@link SupportedCancel} 中无法找到时抛出.
*/
boolean cancelEvent(Cancelable cancelableEvent) throws NoSuchElementException;
}

View File

@ -0,0 +1,90 @@
/*
* 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.event;
import net.lamgc.cgj.bot.event.handler.IllegalHandler;
import net.lamgc.cgj.bot.event.handler.StandardHandler;
import net.lamgc.cgj.bot.event.object.TestEvent;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.NoSuchElementException;
import java.util.UUID;
public class EventUtilsTest {
@Test
public void supportedCancelCheckTest() {
class NonSupportedCancelClass implements EventExecutor {
@Override
public void execute(EventObject event) {
}
@Override
public boolean isAsync() {
return false;
}
}
class SupportedCancelClass implements SupportedCancel, EventExecutor {
@Override
public boolean cancelEvent(UUID eventId) throws UnsupportedOperationException, NoSuchElementException {
return false;
}
@Override
public boolean cancelEvent(EventObject event) throws UnsupportedOperationException, NoSuchElementException {
return false;
}
@Override
public boolean cancelEvent(Cancelable cancelableEvent) throws NoSuchElementException {
return false;
}
@Override
public void execute(EventObject event) {
}
@Override
public boolean isAsync() {
return false;
}
}
Assert.assertTrue(EventUtils.isSupportedCancel(new SupportedCancelClass()));
Assert.assertFalse(EventUtils.isSupportedCancel(new NonSupportedCancelClass()));
}
@Test
public void standardHandlerMethodCheckTest() throws NoSuchMethodException {
Method targetMethod = StandardHandler.class.getMethod("standardHandle", TestEvent.class);
Assert.assertTrue(EventUtils.checkEventHandlerMethod(targetMethod));
}
@Test
public void invalidHandlerMethodCheckTest() {
for (Method method : IllegalHandler.class.getDeclaredMethods()) {
Assert.assertFalse(EventUtils.checkEventHandlerMethod(method));
}
}
}

View File

@ -0,0 +1,62 @@
/*
* 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.event.handler;
import net.lamgc.cgj.bot.event.EventHandler;
import net.lamgc.cgj.bot.event.object.TestEvent;
public abstract class IllegalHandler {
@EventHandler
public void nonArgumentHandleMethod() {
}
@EventHandler
public abstract void abstractHandleMethod(TestEvent event);
@EventHandler
public static void staticHandleMethod(TestEvent event) {
}
@EventHandler
private void privateHandleMethod(TestEvent event) {
}
public void nonAnnotationHandle() {
}
@EventHandler
public void multiArgumentsHandleMethod(TestEvent event, Object object) {
}
@EventHandler
public void invalidArgumentMethod(Object object) {
}
@EventHandler
public Object invalidReturnTypeMethod(TestEvent event) {
return null;
}
}

View File

@ -0,0 +1,30 @@
/*
* 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.event.handler;
import net.lamgc.cgj.bot.event.EventHandler;
import net.lamgc.cgj.bot.event.object.TestEvent;
public class StandardHandler {
@EventHandler
public void standardHandle(TestEvent event) {
}
}

View File

@ -0,0 +1,33 @@
/*
* 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.event.object;
import net.lamgc.cgj.bot.event.AbstractEventObject;
public class TestEvent extends AbstractEventObject {
private final String content;
public TestEvent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}

View File

@ -49,6 +49,11 @@
<artifactId>ContentGrabbingJi-pixiv</artifactId>
<version>3.0.0-alpha-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.lamgc</groupId>
<artifactId>ContentGrabbingJi-Event</artifactId>
<version>3.0.0-alpha-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>

View File

@ -39,6 +39,17 @@
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>net.lamgc</groupId>
<artifactId>ContentGrabbingJi-Event</artifactId>
<version>3.0.0-alpha-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>net.lamgc</groupId>
<artifactId>ContentGrabbingJi-common</artifactId>
<version>3.0.0-alpha-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

View File

@ -35,6 +35,7 @@
<module>ContentGrabbingJi-CacheStore-redis</module>
<module>ContentGrabbingJi-CacheStore-local</module>
<module>ContentGrabbingJi-common</module>
<module>ContentGrabbingJi-Event</module>
</modules>
<developers>