mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
提供Dict中setFields方法及其子类实现,传入lambda能够设置部分值
This commit is contained in:
parent
23e21f8f9f
commit
b7e3db2ec4
@ -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;
|
||||
@ -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;
|
||||
|
||||
/**
|
||||
@ -143,6 +144,16 @@ public class ActiveEntity extends Entity {
|
||||
return (ActiveEntity) super.addFieldNames(fieldNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过lambda批量设置值
|
||||
* @param fields lambda,不能为空
|
||||
* @return this
|
||||
*/
|
||||
@Override
|
||||
public ActiveEntity setFields(Func0<?>... fields) {
|
||||
return (ActiveEntity) super.setFields(fields);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ActiveEntity parseBean(T bean) {
|
||||
return (ActiveEntity) super.parseBean(bean);
|
||||
|
@ -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…
x
Reference in New Issue
Block a user