diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java index ee4ad4fe..915507d8 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java @@ -17,6 +17,7 @@ package com.mybatisflex.core; import com.mybatisflex.annotation.InsertListener; import com.mybatisflex.annotation.KeyType; +import com.mybatisflex.annotation.Listener; import com.mybatisflex.annotation.SetListener; import com.mybatisflex.annotation.UpdateListener; import com.mybatisflex.core.datasource.FlexDataSource; @@ -26,11 +27,10 @@ import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import javax.sql.DataSource; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /** * 全局配置文件 @@ -200,44 +200,28 @@ public class FlexGlobalConfig { * @param entityClass 实体class * @return UpdateListener */ - public List getSupportedSetListener(Class entityClass, boolean interfaceOnly) { - - Map, SetListener> map = new HashMap<>(); - if (!interfaceOnly) { - doGetSupportedSetListener(entityClass, map); - } - - while (entityClass.getSuperclass() != null) { - Class[] interfaces = entityClass.getInterfaces(); - for (Class interfaceClass : interfaces) { - doGetSupportedSetListener(interfaceClass, map); - } - entityClass = entityClass.getSuperclass(); - } - - return new ArrayList<>(map.values()); + public List getSupportedSetListener(Class entityClass) { + return this.findSupportedListeners(entityClass, this.entitySetListeners); } - - private void doGetSupportedSetListener(Class childClass, Map, SetListener> listeners) { - SetListener setListener = null; - Class listenersMapKey = null; - while (setListener == null && childClass != null) { - setListener = entitySetListeners.get(childClass); - listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass(); - childClass = childClass.getSuperclass(); - } - - if (setListener != null) { - listeners.put(listenersMapKey, setListener); - } - } - - public UpdateListener getUpdateListener(Class entityClass) { return entityUpdateListeners.get(entityClass); } + /** + * 查找支持该 {@code entityClass} 的监听器 + * @param entityClass 实体class + * @param listenerMap 监听器map + * @return 符合条件的监听器 + * @param 监听器类型 + */ + public List findSupportedListeners(Class entityClass, Map, T> listenerMap) { + return listenerMap.entrySet().stream() + .filter(entry -> entry.getKey().isAssignableFrom(entityClass)) + .map(Map.Entry::getValue) + .collect(Collectors.toList()); + } + /** * 获取支持该 {@code entityClass} 的update监听器 *

当registerClass是entityClass的本身或其超类时,则视为支持

@@ -245,37 +229,8 @@ public class FlexGlobalConfig { * @param entityClass 实体class * @return UpdateListener */ - public List getSupportedUpdateListener(Class entityClass, boolean interfaceOnly) { - - Map, UpdateListener> map = new HashMap<>(); - if (!interfaceOnly) { - doGetSupportedUpdateListener(entityClass, map); - } - - while (entityClass.getSuperclass() != null) { - Class[] interfaces = entityClass.getInterfaces(); - for (Class interfaceClass : interfaces) { - doGetSupportedUpdateListener(interfaceClass, map); - } - entityClass = entityClass.getSuperclass(); - } - - return new ArrayList<>(map.values()); - } - - - private void doGetSupportedUpdateListener(Class childClass, Map, UpdateListener> listeners) { - UpdateListener updateListener = null; - Class listenersMapKey = null; - while (updateListener == null && childClass != null) { - updateListener = entityUpdateListeners.get(childClass); - listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass(); - childClass = childClass.getSuperclass(); - } - - if (updateListener != null) { - listeners.put(listenersMapKey, updateListener); - } + public List getSupportedUpdateListener(Class entityClass) { + return this.findSupportedListeners(entityClass, this.entityUpdateListeners); } @@ -290,40 +245,10 @@ public class FlexGlobalConfig { * @param entityClass 实体class * @return InsertListener */ - public List getSupportedInsertListener(Class entityClass, boolean interfaceOnly) { - - Map, InsertListener> map = new HashMap<>(); - if (!interfaceOnly) { - doGetSupportedInsertListener(entityClass, map); - } - - while (entityClass.getSuperclass() != null) { - Class[] interfaces = entityClass.getInterfaces(); - for (Class interfaceClass : interfaces) { - doGetSupportedInsertListener(interfaceClass, map); - } - entityClass = entityClass.getSuperclass(); - } - - return new ArrayList<>(map.values()); + public List getSupportedInsertListener(Class entityClass) { + return this.findSupportedListeners(entityClass, this.entityInsertListeners); } - - private void doGetSupportedInsertListener(Class childClass, Map, InsertListener> listeners) { - InsertListener insertListener = null; - Class listenersMapKey = null; - while (insertListener == null && childClass != null) { - insertListener = entityInsertListeners.get(childClass); - listenersMapKey = childClass.getSuperclass() == null ? childClass : childClass.getSuperclass(); - childClass = childClass.getSuperclass(); - } - - if (insertListener != null) { - listeners.put(listenersMapKey, insertListener); - } - } - - public Object getNormalValueOfLogicDelete() { return normalValueOfLogicDelete; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index a188fd7b..069bd0c1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -1372,7 +1372,7 @@ public class TableInfo { public void invokeOnInsertListener(Object entity) { List listeners = MapUtil.computeIfAbsent(insertListenerCache, entityClass, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() - .getSupportedInsertListener(entityClass, CollectionUtil.isNotEmpty(onInsertListeners)); + .getSupportedInsertListener(entityClass); List allListeners = CollectionUtil.merge(onInsertListeners, globalListeners); Collections.sort(allListeners); return allListeners; @@ -1386,7 +1386,7 @@ public class TableInfo { public void invokeOnUpdateListener(Object entity) { List listeners = MapUtil.computeIfAbsent(updateListenerCache, entityClass, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() - .getSupportedUpdateListener(entityClass, CollectionUtil.isNotEmpty(onUpdateListeners)); + .getSupportedUpdateListener(entityClass); List allListeners = CollectionUtil.merge(onUpdateListeners, globalListeners); Collections.sort(allListeners); return allListeners; @@ -1400,7 +1400,7 @@ public class TableInfo { public Object invokeOnSetListener(Object entity, String property, Object value) { List listeners = MapUtil.computeIfAbsent(setListenerCache, entityClass, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() - .getSupportedSetListener(entityClass, CollectionUtil.isNotEmpty(onSetListeners)); + .getSupportedSetListener(entityClass); List allListeners = CollectionUtil.merge(onSetListeners, globalListeners); Collections.sort(allListeners); return allListeners; diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/core/FlexGlobalConfigTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/core/FlexGlobalConfigTest.java new file mode 100644 index 00000000..f2941376 --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/core/FlexGlobalConfigTest.java @@ -0,0 +1,77 @@ +package com.mybatisflex.core; + +import com.mybatisflex.annotation.UpdateListener; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +/** + * 全局配置测试用例 + * + * @author 阮胜 + * @since 2024-02-19 + */ +public class FlexGlobalConfigTest { + + @Test + public void testGetSupportedUpdateListener1() { + FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig(); + flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), Staff.class); + List supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Staff.class); + Assert.assertEquals(supportedUpdateListener.size(), 1); + } + + @Test + public void testGetSupportedUpdateListener2() { + FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig(); + flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), IBaseEntity.class); + List supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Staff.class); + Assert.assertEquals(supportedUpdateListener.size(), 1); + } + + @Test + public void testGetSupportedUpdateListener3() { + FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig(); + flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), Dept.class); + List supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Dept.class); + Assert.assertEquals(supportedUpdateListener.size(), 1); + } + + @Test + public void testGetSupportedUpdateListener4() { + FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig(); + flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), IGeneralEntity.class); + List supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Dept.class); + Assert.assertEquals(supportedUpdateListener.size(), 1); + } + + @Test + public void testGetSupportedUpdateListener5() { + FlexGlobalConfig flexGlobalConfig = new FlexGlobalConfig(); + flexGlobalConfig.registerUpdateListener(new CustomEntityListener(), IBaseEntity.class); + List supportedUpdateListener = flexGlobalConfig.getSupportedUpdateListener(Dept.class); + Assert.assertEquals(supportedUpdateListener.size(), 1); + } + + public static class CustomEntityListener implements UpdateListener { + @Override + public void onUpdate(Object entity) { + } + } + + public interface IBaseEntity { + } + + public interface IGeneralEntity extends IBaseEntity { + } + + public static class Staff implements IBaseEntity { + + } + + public static class Dept implements IGeneralEntity { + } + + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java index 028128a6..675a87d0 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/ListenerTest.java @@ -47,7 +47,7 @@ class ListenerTest { List insertListeners = MapUtil.computeIfAbsent(tempOnInsertListenerMap, AccountMissingListenerTestModel.class, aClass -> { List globalListeners = FlexGlobalConfig.getDefaultConfig() - .getSupportedInsertListener(AccountMissingListenerTestModel.class, CollectionUtil.isNotEmpty(tableInfo.getOnInsertListeners())); + .getSupportedInsertListener(AccountMissingListenerTestModel.class); List allListeners = CollectionUtil.merge(tableInfo.getOnInsertListeners(), globalListeners); Collections.sort(allListeners); return allListeners;