From e933fb8c2bbdba47f73bdb3e2bf40ba548d11b6c Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 16 Mar 2022 13:19:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9BDict=E4=B8=ADsetFields?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=AE=9E=E7=8E=B0=EF=BC=8C=E4=BC=A0=E5=85=A5?= =?UTF-8?q?lambda=E8=AE=BE=E7=BD=AE=E9=83=A8=E5=88=86=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/lang/Dict.java | 13 +++++ .../cn/hutool/core/lang/func/LambdaUtil.java | 19 ++++++ .../java/cn/hutool/core/lang/DictTest.java | 12 +++- .../main/java/cn/hutool/db/ActiveEntity.java | 58 +++++++++++-------- .../src/main/java/cn/hutool/db/Entity.java | 11 ++++ 5 files changed, 89 insertions(+), 24 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java b/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java index 168dfba4a..9a5310e29 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java @@ -10,12 +10,15 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Time; import java.sql.Timestamp; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; +import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.LambdaUtil; /** * 字典对象,扩充了HashMap中的方法 @@ -596,4 +599,14 @@ public class Dict extends LinkedHashMap implements BasicTypeGett } return key; } + + /** + * 通过lambda批量设置值 + * @param fields lambda,不能为空 + * @return this + */ + public Dict setFields(Func0... fields) { + Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.callWithRuntimeException())); + return this; + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java index f604ebb00..c204775f2 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java @@ -29,6 +29,10 @@ public class LambdaUtil { return _resolve(func); } + public static SerializedLambda resolve(Func0 func) { + return _resolve(func); + } + /** * 获取lambda表达式函数(方法)名称 * @@ -40,6 +44,10 @@ public class LambdaUtil { return resolve(func).getImplMethodName(); } + public static String getMethodName(Func0 func) { + return resolve(func).getImplMethodName(); + } + /** * 获取lambda表达式Getter或Setter函数(方法)对应的字段名称,规则如下: *
    @@ -66,6 +74,17 @@ public class LambdaUtil { } } + public static String getFieldName(Func0 func) throws IllegalArgumentException { + final String methodName = getMethodName(func); + if (methodName.startsWith("get") || methodName.startsWith("set")) { + return StrUtil.removePreAndLowerFirst(methodName, 3); + } else if (methodName.startsWith("is")) { + return StrUtil.removePreAndLowerFirst(methodName, 2); + } else { + throw new IllegalArgumentException("Invalid Getter or Setter name: " + methodName); + } + } + /** * 解析lambda表达式,加了缓存。 * 该缓存可能会在任意不定的时间被清除 diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java index 81762662d..665944c9c 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java @@ -3,6 +3,7 @@ package cn.hutool.core.lang; import cn.hutool.core.date.DateTime; import org.junit.Assert; import org.junit.Test; +import static cn.hutool.core.lang.OptTest.User; import java.util.HashMap; import java.util.Map; @@ -14,7 +15,7 @@ public class DictTest { .set("key1", 1)//int .set("key2", 1000L)//long .set("key3", DateTime.now());//Date - + Long v2 = dict.getLong("key2"); Assert.assertEquals(Long.valueOf(1000L), v2); } @@ -58,4 +59,13 @@ public class DictTest { Assert.assertTrue(dict.isEmpty()); } + + @Test + public void setFieldsTest() { + User user = User.builder().username("hutool").nickname("Hutool").build(); + Dict dict = Dict.create(); + dict.setFields(user::getNickname, user::getUsername); + Assert.assertEquals("Hutool", dict.get("nickname")); + Assert.assertEquals("hutool", dict.get("username")); + } } diff --git a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java index c0d179270..c5b3a60ab 100644 --- a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java +++ b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java @@ -3,12 +3,13 @@ package cn.hutool.db; import java.sql.SQLException; import java.util.Collection; +import cn.hutool.core.lang.func.Func0; import cn.hutool.core.map.MapUtil; /** * 动态实体类
    * 提供了针对自身实体的增删改方法 - * + * * @author Looly * */ @@ -20,7 +21,7 @@ public class ActiveEntity extends Entity { // --------------------------------------------------------------- Static method start /** * 创建ActiveEntity - * + * * @return ActiveEntity */ public static ActiveEntity create() { @@ -29,7 +30,7 @@ public class ActiveEntity extends Entity { /** * 创建ActiveEntity - * + * * @param tableName 表名 * @return ActiveEntity */ @@ -39,7 +40,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为Entity - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -50,7 +51,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity - * + * * @param Bean对象类型 * @param bean Bean对象 * @param isToUnderlineCase 是否转换为下划线模式 @@ -63,7 +64,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity,并采用下划线法转换字段 - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -83,7 +84,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param tableName 表名 */ public ActiveEntity(String tableName) { @@ -92,7 +93,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param entity 非动态实体 */ public ActiveEntity(Entity entity) { @@ -101,7 +102,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param tableName 表名 */ @@ -112,7 +113,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param entity 非动态实体 */ @@ -122,47 +123,58 @@ public class ActiveEntity extends Entity { this.db = db; } // -------------------------------------------------------------------------- Constructor end - + @Override public ActiveEntity setTableName(String tableName) { return (ActiveEntity) super.setTableName(tableName); } - + @Override public ActiveEntity setFieldNames(Collection fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + @Override public ActiveEntity setFieldNames(String... fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + + /** + * 通过lambda批量设置值 + * + * @param fields lambda,不能为空 + * @return this + */ + @Override + public ActiveEntity setFields(Func0... fields) { + return (ActiveEntity) super.setFields(fields); + } + @Override public ActiveEntity addFieldNames(String... fieldNames) { return (ActiveEntity) super.addFieldNames(fieldNames); } - + @Override public ActiveEntity parseBean(T bean) { return (ActiveEntity) super.parseBean(bean); } - + @Override public ActiveEntity parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue) { return (ActiveEntity) super.parseBean(bean, isToUnderlineCase, ignoreNullValue); } - + @Override public ActiveEntity set(String field, Object value) { return (ActiveEntity) super.set(field, value); } - + @Override public ActiveEntity setIgnoreNull(String field, Object value) { return (ActiveEntity) super.setIgnoreNull(field, value); } - + @Override public ActiveEntity clone() { return (ActiveEntity) super.clone(); @@ -171,7 +183,7 @@ public class ActiveEntity extends Entity { // -------------------------------------------------------------------------- CRUD start /** * 根据Entity中现有字段条件从数据库中增加一条数据 - * + * * @return this */ public ActiveEntity add() { @@ -185,7 +197,7 @@ public class ActiveEntity extends Entity { /** * 根据Entity中现有字段条件从数据库中加载一个Entity对象 - * + * * @return this */ public ActiveEntity load() { @@ -202,7 +214,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @return this */ public ActiveEntity del() { @@ -216,7 +228,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @param primaryKey 主键名 * @return this */ diff --git a/hutool-db/src/main/java/cn/hutool/db/Entity.java b/hutool-db/src/main/java/cn/hutool/db/Entity.java index c3dbeb533..5afd8ca10 100644 --- a/hutool-db/src/main/java/cn/hutool/db/Entity.java +++ b/hutool-db/src/main/java/cn/hutool/db/Entity.java @@ -2,6 +2,7 @@ package cn.hutool.db; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.lang.Dict; +import cn.hutool.core.lang.func.Func0; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ReflectUtil; @@ -172,6 +173,16 @@ public class Entity extends Dict { return this; } + /** + * 通过lambda批量设置值 + * @param fields lambda,不能为空 + * @return this + */ + @Override + public Entity setFields(Func0... fields) { + return (Entity) super.setFields(fields); + } + /** * 添加字段列表 *