Commit 222d1cf69b02f46f7580ec08c7f547776d41fab4
1 parent
5e2fa48f
增加对比新老数组的 C/U/D 对象
Showing
3 changed files
with
113 additions
and
5 deletions
yudao-framework/yudao-common/pom.xml
| @@ -133,6 +133,11 @@ | @@ -133,6 +133,11 @@ | ||
| 133 | <artifactId>transmittable-thread-local</artifactId> | 133 | <artifactId>transmittable-thread-local</artifactId> |
| 134 | </dependency> | 134 | </dependency> |
| 135 | 135 | ||
| 136 | + <!-- Test 测试相关 --> | ||
| 137 | + <dependency> | ||
| 138 | + <groupId>org.springframework.boot</groupId> | ||
| 139 | + <artifactId>spring-boot-starter-test</artifactId> | ||
| 140 | + </dependency> | ||
| 136 | </dependencies> | 141 | </dependencies> |
| 137 | 142 | ||
| 138 | </project> | 143 | </project> |
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java
| @@ -6,12 +6,11 @@ import cn.hutool.core.map.MapUtil; | @@ -6,12 +6,11 @@ import cn.hutool.core.map.MapUtil; | ||
| 6 | import com.google.common.collect.ImmutableMap; | 6 | import com.google.common.collect.ImmutableMap; |
| 7 | 7 | ||
| 8 | import java.util.*; | 8 | import java.util.*; |
| 9 | -import java.util.function.BinaryOperator; | ||
| 10 | -import java.util.function.Function; | ||
| 11 | -import java.util.function.Predicate; | ||
| 12 | -import java.util.function.Supplier; | 9 | +import java.util.function.*; |
| 13 | import java.util.stream.Collectors; | 10 | import java.util.stream.Collectors; |
| 14 | 11 | ||
| 12 | +import static java.util.Arrays.asList; | ||
| 13 | + | ||
| 15 | /** | 14 | /** |
| 16 | * Collection 工具类 | 15 | * Collection 工具类 |
| 17 | * | 16 | * |
| @@ -20,7 +19,7 @@ import java.util.stream.Collectors; | @@ -20,7 +19,7 @@ import java.util.stream.Collectors; | ||
| 20 | public class CollectionUtils { | 19 | public class CollectionUtils { |
| 21 | 20 | ||
| 22 | public static boolean containsAny(Object source, Object... targets) { | 21 | public static boolean containsAny(Object source, Object... targets) { |
| 23 | - return Arrays.asList(targets).contains(source); | 22 | + return asList(targets).contains(source); |
| 24 | } | 23 | } |
| 25 | 24 | ||
| 26 | public static boolean isAnyEmpty(Collection<?>... collections) { | 25 | public static boolean isAnyEmpty(Collection<?>... collections) { |
| @@ -190,6 +189,46 @@ public class CollectionUtils { | @@ -190,6 +189,46 @@ public class CollectionUtils { | ||
| 190 | return func.apply(mapData); | 189 | return func.apply(mapData); |
| 191 | } | 190 | } |
| 192 | 191 | ||
| 192 | + /** | ||
| 193 | + * 对比老、新两个列表,找出新增、修改、删除的数据 | ||
| 194 | + * | ||
| 195 | + * @param oldList 老列表 | ||
| 196 | + * @param newList 新列表 | ||
| 197 | + * @param sameFunc 对比函数,返回 true 表示相同,返回 false 表示不同 | ||
| 198 | + * 注意,same 是通过每个元素的“标识”,判断它们是不是同一个数据 | ||
| 199 | + * @return [新增列表、修改列表、删除列表] | ||
| 200 | + */ | ||
| 201 | + public static <T> List<List<T>> diffList(Collection<T> oldList, Collection<T> newList, | ||
| 202 | + BiFunction<T, T, Boolean> sameFunc) { | ||
| 203 | + List<T> createList = new LinkedList<>(newList); // 默认都认为是新增的,后续会进行移除 | ||
| 204 | + List<T> updateList = new ArrayList<>(); | ||
| 205 | + List<T> deleteList = new ArrayList<>(); | ||
| 206 | + | ||
| 207 | + // 通过以 oldList 为主遍历,找出 updateList 和 deleteList | ||
| 208 | + for (T oldObj : oldList) { | ||
| 209 | + // 1. 寻找是否有匹配的 | ||
| 210 | + T foundObj = null; | ||
| 211 | + for (Iterator<T> iterator = createList.iterator(); iterator.hasNext(); ) { | ||
| 212 | + T newObj = iterator.next(); | ||
| 213 | + // 1.1 不匹配,则直接跳过 | ||
| 214 | + if (!sameFunc.apply(oldObj, newObj)) { | ||
| 215 | + continue; | ||
| 216 | + } | ||
| 217 | + // 1.2 匹配,则移除,并结束寻找 | ||
| 218 | + iterator.remove(); | ||
| 219 | + foundObj = newObj; | ||
| 220 | + break; | ||
| 221 | + } | ||
| 222 | + // 2. 匹配添加到 updateList;不匹配则添加到 deleteList 中 | ||
| 223 | + if (foundObj != null) { | ||
| 224 | + updateList.add(foundObj); | ||
| 225 | + } else { | ||
| 226 | + deleteList.add(oldObj); | ||
| 227 | + } | ||
| 228 | + } | ||
| 229 | + return asList(createList, updateList, deleteList); | ||
| 230 | + } | ||
| 231 | + | ||
| 193 | public static boolean containsAny(Collection<?> source, Collection<?> candidates) { | 232 | public static boolean containsAny(Collection<?> source, Collection<?> candidates) { |
| 194 | return org.springframework.util.CollectionUtils.containsAny(source, candidates); | 233 | return org.springframework.util.CollectionUtils.containsAny(source, candidates); |
| 195 | } | 234 | } |
yudao-framework/yudao-common/src/test/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtilsTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.common.util.collection; | ||
| 2 | + | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 4 | +import lombok.Data; | ||
| 5 | +import org.junit.jupiter.api.Test; | ||
| 6 | + | ||
| 7 | +import java.util.Arrays; | ||
| 8 | +import java.util.Collection; | ||
| 9 | +import java.util.List; | ||
| 10 | +import java.util.function.BiFunction; | ||
| 11 | + | ||
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 13 | + | ||
| 14 | +/** | ||
| 15 | + * {@link CollectionUtils} 的单元测试 | ||
| 16 | + */ | ||
| 17 | +public class CollectionUtilsTest { | ||
| 18 | + | ||
| 19 | + @Data | ||
| 20 | + @AllArgsConstructor | ||
| 21 | + private static class Dog { | ||
| 22 | + | ||
| 23 | + private Integer id; | ||
| 24 | + private String name; | ||
| 25 | + private String code; | ||
| 26 | + | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + @Test | ||
| 30 | + public void testDiffList() { | ||
| 31 | + // 准备参数 | ||
| 32 | + Collection<Dog> oldList = Arrays.asList( | ||
| 33 | + new Dog(1, "花花", "hh"), | ||
| 34 | + new Dog(2, "旺财", "wc") | ||
| 35 | + ); | ||
| 36 | + Collection<Dog> newList = Arrays.asList( | ||
| 37 | + new Dog(null, "花花2", "hh"), | ||
| 38 | + new Dog(null, "小白", "xb") | ||
| 39 | + ); | ||
| 40 | + BiFunction<Dog, Dog, Boolean> sameFunc = (oldObj, newObj) -> { | ||
| 41 | + boolean same = oldObj.getCode().equals(newObj.getCode()); | ||
| 42 | + // 如果相等的情况下,需要设置下 id,后续好更新 | ||
| 43 | + if (same) { | ||
| 44 | + newObj.setId(oldObj.getId()); | ||
| 45 | + } | ||
| 46 | + return same; | ||
| 47 | + }; | ||
| 48 | + | ||
| 49 | + // 调用 | ||
| 50 | + List<List<Dog>> result = CollectionUtils.diffList(oldList, newList, sameFunc); | ||
| 51 | + // 断言 | ||
| 52 | + assertEquals(result.size(), 3); | ||
| 53 | + // 断言 create | ||
| 54 | + assertEquals(result.get(0).size(), 1); | ||
| 55 | + assertEquals(result.get(0).get(0), new Dog(null, "小白", "xb")); | ||
| 56 | + // 断言 update | ||
| 57 | + assertEquals(result.get(1).size(), 1); | ||
| 58 | + assertEquals(result.get(1).get(0), new Dog(1, "花花2", "hh")); | ||
| 59 | + // 断言 delete | ||
| 60 | + assertEquals(result.get(2).size(), 1); | ||
| 61 | + assertEquals(result.get(2).get(0), new Dog(2, "旺财", "wc")); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | +} |