mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
!578 提供Dict中setFields方法及其子类实现,传入lambda能够设置部分值
Merge pull request !578 from 阿超/v5-dev
This commit is contained in:
commit
e00f4ccf86
hutool-core/src
hutool-db/src/main/java/cn/hutool/db
@ -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<String, Object> 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;
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,10 @@ public class LambdaUtil {
|
||||
return _resolve(func);
|
||||
}
|
||||
|
||||
public static <R> SerializedLambda resolve(Func0<R> func) {
|
||||
return _resolve(func);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取lambda表达式函数(方法)名称
|
||||
*
|
||||
@ -40,6 +44,10 @@ public class LambdaUtil {
|
||||
return resolve(func).getImplMethodName();
|
||||
}
|
||||
|
||||
public static <R> String getMethodName(Func0<R> func) {
|
||||
return resolve(func).getImplMethodName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取lambda表达式Getter或Setter函数(方法)对应的字段名称,规则如下:
|
||||
* <ul>
|
||||
@ -66,6 +74,17 @@ public class LambdaUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static <R> String getFieldName(Func0<R> 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表达式,加了缓存。
|
||||
* 该缓存可能会在任意不定的时间被清除
|
||||
|
@ -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(null).build();
|
||||
Dict dict = Dict.create();
|
||||
dict.setFields(user::getNickname, user::getUsername);
|
||||
Assert.assertEquals("hutool", dict.get("username"));
|
||||
Assert.assertNull(dict.get("nickname"));
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ 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;
|
||||
|
||||
/**
|
||||
@ -137,7 +138,17 @@ public class ActiveEntity extends Entity {
|
||||
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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加字段列表
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user