mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-07 14:18:05 +08:00
提供Dict中setFields方法实现,传入lambda设置部分值
This commit is contained in:
parent
f34a3e9c88
commit
e933fb8c2b
@ -10,12 +10,15 @@ import java.math.BigDecimal;
|
|||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import cn.hutool.core.lang.func.Func0;
|
||||||
|
import cn.hutool.core.lang.func.LambdaUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典对象,扩充了HashMap中的方法
|
* 字典对象,扩充了HashMap中的方法
|
||||||
@ -596,4 +599,14 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
|
|||||||
}
|
}
|
||||||
return key;
|
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);
|
return _resolve(func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <R> SerializedLambda resolve(Func0<R> func) {
|
||||||
|
return _resolve(func);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取lambda表达式函数(方法)名称
|
* 获取lambda表达式函数(方法)名称
|
||||||
*
|
*
|
||||||
@ -40,6 +44,10 @@ public class LambdaUtil {
|
|||||||
return resolve(func).getImplMethodName();
|
return resolve(func).getImplMethodName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <R> String getMethodName(Func0<R> func) {
|
||||||
|
return resolve(func).getImplMethodName();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取lambda表达式Getter或Setter函数(方法)对应的字段名称,规则如下:
|
* 获取lambda表达式Getter或Setter函数(方法)对应的字段名称,规则如下:
|
||||||
* <ul>
|
* <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表达式,加了缓存。
|
* 解析lambda表达式,加了缓存。
|
||||||
* 该缓存可能会在任意不定的时间被清除
|
* 该缓存可能会在任意不定的时间被清除
|
||||||
|
@ -3,6 +3,7 @@ package cn.hutool.core.lang;
|
|||||||
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateTime;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import static cn.hutool.core.lang.OptTest.User;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -58,4 +59,13 @@ public class DictTest {
|
|||||||
|
|
||||||
Assert.assertTrue(dict.isEmpty());
|
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"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package cn.hutool.db;
|
|||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.func.Func0;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,6 +139,17 @@ public class ActiveEntity extends Entity {
|
|||||||
return (ActiveEntity) super.setFieldNames(fieldNames);
|
return (ActiveEntity) super.setFieldNames(fieldNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过lambda批量设置值
|
||||||
|
*
|
||||||
|
* @param fields lambda,不能为空
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ActiveEntity setFields(Func0<?>... fields) {
|
||||||
|
return (ActiveEntity) super.setFields(fields);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActiveEntity addFieldNames(String... fieldNames) {
|
public ActiveEntity addFieldNames(String... fieldNames) {
|
||||||
return (ActiveEntity) super.addFieldNames(fieldNames);
|
return (ActiveEntity) super.addFieldNames(fieldNames);
|
||||||
|
@ -2,6 +2,7 @@ package cn.hutool.db;
|
|||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.lang.Dict;
|
import cn.hutool.core.lang.Dict;
|
||||||
|
import cn.hutool.core.lang.func.Func0;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
import cn.hutool.core.util.ReflectUtil;
|
||||||
@ -172,6 +173,16 @@ public class Entity extends Dict {
|
|||||||
return this;
|
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