simple-utils-common
# 简介
提供常用及通用的工具类
# 将列表转换为树形结构工具类
# TreeListUtils
将原始列表转换为树形结构
/**
* 调用转换树型结构测试方法
*
* @param list 原始数据
* @return 树形结构数据
*/
private List<Demo> performConversionTest(List<Demo> list) {
String childrenFieldName = "children";
Predicate<Integer> isRootPredicate = parentId -> parentId == null || -1 == parentId;
return TreeListUtils.convert(list, childrenFieldName, isRootPredicate, Demo::getId, Demo::getPid);
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
将原始列表转换为树形结构-并将关联对象添加进去树形结构
/**
* 调用转换树型结构测试方法-并将关联对象添加进去树形结构
*
* @param list 原始数据
* @return 树形结构数据
*/
private List<Demo> performConversionTest(List<Demo> list) {
Map<Integer, Data> idData = new HashMap<>();
idData.put(0, new Data(0, "ce0"));
idData.put(1, new Data(1, "ce1"));
idData.put(10, new Data(10, "ce10"));
idData.put(20, new Data(20, "ce20"));
String childrenFieldName = "children";
Predicate<Integer> isRootPredicate = parentId -> parentId == null || parentId == -1;
String dataFieldName = "data";
return TreeListUtils.convert(list, childrenFieldName, isRootPredicate, Demo::getId, Demo::getPid, idData, dataFieldName, Demo::getId);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 唯一主键工厂及默认实现
# IdFactory
默认的 IdGenerate 实现类 获取主键
@Test
public void testGenerate() {
Long generatedId = IdFactory.generate();
log.info("IdFactory.generate():{}", generatedId);
assertNotNull(generatedId);
String generatedIdStr = IdFactory.generateStr();
log.info("IdFactory.generateStr():{}", generatedIdStr);
assertNotNull(generatedIdStr);
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
自定义实现IdGenerate,初始化工厂使用; 完成后清除自定义实现,重新调用默认实现
注:在IdFactory.initialize前若已经使用初始化工厂应先清除IdFactory.clear()
public class IdGenerateCustomImpl implements IdGenerate {
@Override
public Long generate() {
return 1L;
}
@Override
public String generateStr() {
return "test";
}
}
@Test
public void testGenerateCustomImpl() {
Supplier<IdGenerate> impl = IdGenerateCustomImpl::new;
IdFactory.initialize(impl);
assertEquals(IdFactory.generateStr(), "test");
assertEquals(IdFactory.generate(), 1L);
IdFactory.clear();
testGenerate();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
上次更新: 2024/03/25, 02:28:08