mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
提供Dict中setFields方法实现,传入lambda设置部分值
This commit is contained in:
@@ -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("Hutool").build();
|
||||
Dict dict = Dict.create();
|
||||
dict.setFields(user::getNickname, user::getUsername);
|
||||
Assert.assertEquals("Hutool", dict.get("nickname"));
|
||||
Assert.assertEquals("hutool", dict.get("username"));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user