This commit is contained in:
Looly 2020-03-10 01:12:19 +08:00
parent 0781be4e1c
commit a6869ae721
4 changed files with 79 additions and 98 deletions

View File

@ -8,6 +8,7 @@
### 新特性 ### 新特性
* 【core 】 修改FastDateParser策略与JDK保持一致issue#I1AXIN@Gitee * 【core 】 修改FastDateParser策略与JDK保持一致issue#I1AXIN@Gitee
* 【core 】 增加tree树状结构pr#100@Gitee * 【core 】 增加tree树状结构pr#100@Gitee
* 【core 】 增加randomEleListpr#764@Github
### Bug修复 ### Bug修复
* 【setting】 修复Props.toBean方法null的问题 * 【setting】 修复Props.toBean方法null的问题
* 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题issue#I1B18H@Gitee * 【core 】 修复DataUtil.parseLocalDateTime无时间部分报错问题issue#I1B18H@Gitee

View File

@ -1,13 +1,12 @@
/** /**
* 提供通用树生成特点 * 提供通用树生成特点<p>
* <pre> * 1每个字段可自定义<br>
* 1每个字段可自定义 * 2支持排序 树深度配置,自定义转换器等<br>
* 2支持排序 树深度配置,自定义转换器等 * 3支持额外属性扩展<br>
* 3支持额外属性扩展 * 4贴心 许多属性,特性都有默认值处理<br>
* 4贴心 许多属性,特性都有默认值处理 * 5使用简单 可一行代码生成树<br>
* 5使用简单 可一行代码生成树
* 6代码简洁轻量无额外依赖 * 6代码简洁轻量无额外依赖
* <pre/> * </p>
* *
* @author liangbaikaihttps://gitee.com/liangbaikai00/ * @author liangbaikaihttps://gitee.com/liangbaikai00/
* @since 5.2.1 * @since 5.2.1

View File

@ -351,6 +351,28 @@ public class RandomUtil {
return result; return result;
} }
/**
* 随机获得列表中的一定量的元素返回List<br>
* 此方法与{@link #randomEles(List, int)} 不同点在于不会获取重复位置的元素
*
* @param source 列表
* @param count 随机取出的个数
* @param <T> 元素类型
* @return 随机列表
* @since 5.2.1
*/
public static <T> List<T> randomEleList(List<T> source, int count) {
if (count >= source.size()) {
return source;
}
final int[] randomList = ArrayUtil.sub(randomInts(source.size()), 0, count);
List<T> result = new ArrayList<>();
for (int e : randomList) {
result.add(source.get(e));
}
return result;
}
/** /**
* 随机获得列表中的一定量的不重复元素返回Set * 随机获得列表中的一定量的不重复元素返回Set
* *
@ -374,6 +396,7 @@ public class RandomUtil {
return result; return result;
} }
/** /**
* 创建指定长度的随机索引 * 创建指定长度的随机索引
* *

View File

@ -5,6 +5,8 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import cn.hutool.core.annotation.Alias;
import lombok.Data;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
@ -33,7 +35,6 @@ import cn.hutool.json.test.bean.report.SuiteReport;
* JSONObject单元测试 * JSONObject单元测试
* *
* @author Looly * @author Looly
*
*/ */
public class JSONObjectTest { public class JSONObjectTest {
@ -224,7 +225,12 @@ public class JSONObjectTest {
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
@Test @Test
public void toBeanTest6() { public void toBeanTest6() {
JSONObject json = JSONUtil.createObj().put("targetUrl", "http://test.com").put("success", "true").put("result", JSONUtil.createObj().put("token", "tokenTest").put("userId", "测试用户1")); JSONObject json = JSONUtil.createObj()
.put("targetUrl", "http://test.com")
.put("success", "true")
.put("result", JSONUtil.createObj()
.put("token", "tokenTest")
.put("userId", "测试用户1"));
TokenAuthWarp2 bean = json.toBean(TokenAuthWarp2.class); TokenAuthWarp2 bean = json.toBean(TokenAuthWarp2.class);
Assert.assertEquals("http://test.com", bean.getTargetUrl()); Assert.assertEquals("http://test.com", bean.getTargetUrl());
@ -361,6 +367,24 @@ public class JSONObjectTest {
Assert.assertEquals("defaultBBB", bbb); Assert.assertEquals("defaultBBB", bbb);
} }
@Test
public void aliasTest(){
final BeanWithAlias beanWithAlias = new BeanWithAlias();
beanWithAlias.setValue1("张三");
beanWithAlias.setValue2(35);
final JSONObject jsonObject = JSONUtil.parseObj(beanWithAlias);
Assert.assertEquals("张三", jsonObject.getStr("name"));
Assert.assertEquals(new Integer(35), jsonObject.getInt("age"));
JSONObject json = JSONUtil.createObj()
.put("name", "张三")
.put("age", 35);
final BeanWithAlias bean = JSONUtil.toBean(Objects.requireNonNull(json).toString(), BeanWithAlias.class);
Assert.assertEquals("张三", bean.getValue1());
Assert.assertEquals(new Integer(35), bean.getValue2());
}
public enum TestEnum { public enum TestEnum {
TYPE_A, TYPE_B TYPE_A, TYPE_B
} }
@ -369,99 +393,33 @@ public class JSONObjectTest {
* 测试Bean * 测试Bean
* *
* @author Looly * @author Looly
*
*/ */
@Data
public static class TestBean { public static class TestBean {
private String strValue; private String strValue;
private int intValue; private int intValue;
private Double doubleValue; private Double doubleValue;
private subBean beanValue; private SubBean beanValue;
private List<String> list; private List<String> list;
private TestEnum testEnum; private TestEnum testEnum;
public String getStrValue() {
return strValue;
}
public void setStrValue(String strValue) {
this.strValue = strValue;
}
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
public Double getDoubleValue() {
return doubleValue;
}
public void setDoubleValue(Double doubleValue) {
this.doubleValue = doubleValue;
}
public subBean getBeanValue() {
return beanValue;
}
public void setBeanValue(subBean beanValue) {
this.beanValue = beanValue;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public TestEnum getTestEnum() {
return testEnum;
}
public void setTestEnum(TestEnum testEnum) {
this.testEnum = testEnum;
}
@Override
public String toString() {
return "TestBean [strValue=" + strValue + ", intValue=" + intValue + ", doubleValue=" + doubleValue + ", beanValue=" + beanValue + ", list=" + list + ", testEnum=" + testEnum + "]";
}
} }
/** /**
* 测试子Bean * 测试子Bean
* *
* @author Looly * @author Looly
*
*/ */
public static class subBean { @Data
public static class SubBean {
private String value1; private String value1;
private BigDecimal value2; private BigDecimal value2;
public String getValue1() {
return value1;
} }
public void setValue1(String value1) { @Data
this.value1 = value1; public static class BeanWithAlias {
} @Alias("name")
private String value1;
public BigDecimal getValue2() { @Alias("age")
return value2; private Integer value2;
}
public void setValue2(BigDecimal value2) {
this.value2 = value2;
}
@Override
public String toString() {
return "subBean [value1=" + value1 + ", value2=" + value2 + "]";
}
} }
} }