暮冬时烤雪,迟夏写长信。——陈鸿宇
现已并入hutool
5.8.0,PR
链接地址:https://gitee.com/dromara/hutool/pulls/583
This is a story about pull request
getFieldBy
我们在使用枚举的过程中,经常会遇到字段相互之间映射的场景:
例如此处我编写一个GenderEnum
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Getter @AllArgsConstructor public enum GenderEnum {
FEMALE("男"), MALE("女"), UNKNOWN("未知");
private final String desc;
}
|
然后对其进行使用,我们前端传入可能是0/1/2
这样的数字,我们就可以通过EnumUtil#getFieldBy
,来通过ordinal
,也就是我们枚举的序号,获取对应的汉字返回
1 2 3
| String desc = EnumUtil.getFieldBy(GenderEnum::getDesc, GenderEnum::ordinal, 0); Assert.assertEquals("男", desc);
|
有木有体会到声明式编程的好处?就像我们写sql
一样,很流利顺畅的就写出来了这样优雅的代码
如果还有小伙伴不熟悉lambda
,赶紧戳这里来学习!开启声明式编程的时代!
getBy
这时候有朋友就要问了,阿超那你这个EnumUtil.getBy
不是多余的吗?容我娓娓道来
我们如果遇到了这种场景:
需要根据枚举对应值执行不同的逻辑:此处用一个switch case
举个栗子:
(jdk8
开始允许枚举作为switch case
的条件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| GenderEnum genderEnum = EnumUtil.getBy(GenderEnum::ordinal, RandomUtil.randomInt(0, 3)); if (genderEnum == null) { return; } switch (genderEnum) { case FEMALE: Console.log("这是一位帅帅的小哥哥"); break; case MALE: Console.log("这是一位美美的小姐姐"); break; case UNKNOWN: Console.log("这位更是重量级,Ta不愿意透露性别"); break; default: break; }
|
让我们一起减少遍布在项目各处的魔法值吧! ✌️
大家用枚举去管理,也能方便统一修改和厘清逻辑
MapUtil#change
这里简单介绍一个基本使用和一个复杂场景:
1 2 3 4 5 6 7 8 9 10 11 12
| Map<Integer, String> adjectivesMap = MapUtil.<Integer, String>builder() .put(0, "lovely") .put(1, "friendly") .put(2, "happily") .build();
Map<Integer, String> resultMap = MapUtil.change(adjectivesMap, (k, v) -> v + " " + PeopleEnum.values()[k].name().toLowerCase());
Assert.assertEquals("lovely girl", resultMap.get(0)); Assert.assertEquals("friendly boy", resultMap.get(1)); Assert.assertEquals("happily child", resultMap.get(2));
|
复杂场景:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| Queue<String> customers = new ArrayDeque<>(Arrays.asList("刑部尚书手工耿", "木瓜大盗大漠叔", "竹鼠发烧找华农", "朴实无华朱一旦"));
List<Group> groups = Stream.iterate(0L, i -> ++i).limit(4).map(i -> Group.builder().id(i).build()).collect(Collectors.toList());
Map<Long, User> idUserMap = Stream.iterate(0L, i -> ++i).limit(4).map(i -> User.builder().id(i).name(customers.poll()).build()).collect(Collectors.toMap(User::getId, Function.identity()));
Map<Long, List<Long>> groupIdUserIdsMap = groups.stream().flatMap(group -> idUserMap.keySet().stream().map(userId -> UserGroup.builder().groupId(group.getId()).userId(userId).build())).collect(Collectors.groupingBy(UserGroup::getUserId, Collectors.mapping(UserGroup::getGroupId, Collectors.toList())));
Map<Long, List<User>> groupIdUserMap = MapUtil.change(groupIdUserIdsMap, (groupId, userIds) -> userIds.stream().map(idUserMap::get).collect(Collectors.toList()));
groups.forEach(group -> Opt.ofNullable(group.getId()).map(groupIdUserMap::get).ifPresent(group::setUsers));
groups.forEach(group -> { List<User> users = group.getUsers(); Assert.assertEquals("刑部尚书手工耿", users.get(0).getName()); Assert.assertEquals("木瓜大盗大漠叔", users.get(1).getName()); Assert.assertEquals("竹鼠发烧找华农", users.get(2).getName()); Assert.assertEquals("朴实无华朱一旦", users.get(3).getName()); });
|
用到的一些自定义的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| enum PeopleEnum {GIRL, BOY, CHILD}
@Data @Builder public static class User { private Long id; private String name; }
@Data @Builder public static class Group { private Long id; private List<User> users; }
@Data @Builder public static class UserGroup { private Long userId; private Long groupId; }
|