This commit is contained in:
Looly
2020-09-22 17:57:16 +08:00
parent 46fafeeb86
commit 36f7909702
11 changed files with 159 additions and 17 deletions

View File

@@ -109,6 +109,7 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
}
}
}
return this.dest;
}

View File

@@ -1,6 +1,7 @@
package cn.hutool.core.collection;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.comparator.CompareUtil;
import cn.hutool.core.comparator.PinyinComparator;
import cn.hutool.core.comparator.PropertyComparator;
import cn.hutool.core.convert.Convert;
@@ -992,8 +993,13 @@ public class CollUtil {
} else if (collectionType.isAssignableFrom(LinkedHashSet.class)) {
list = new LinkedHashSet<>();
} else if (collectionType.isAssignableFrom(TreeSet.class)) {
//noinspection SortedCollectionWithNonComparableKeys
list = new TreeSet<>();
list = new TreeSet<>((o1, o2) -> {
// 优先按照对象本身比较如果没有实现比较接口默认按照toString内容比较
if (o1 instanceof Comparable) {
return ((Comparable<T>) o1).compareTo(o2);
}
return CompareUtil.compare(o1.toString(), o2.toString());
});
} else if (collectionType.isAssignableFrom(EnumSet.class)) {
list = (Collection<T>) EnumSet.noneOf((Class<Enum>) ClassUtil.getTypeArgument(collectionType));
}

View File

@@ -106,9 +106,9 @@ public class ConverterRegistry implements Serializable {
}
/**
* 获得单例的 {@link ConverterRegistry}
* 获得单例的 ConverterRegistry
*
* @return {@link ConverterRegistry}
* @return ConverterRegistry
*/
public static ConverterRegistry getInstance() {
return SingletonHolder.INSTANCE;
@@ -140,7 +140,7 @@ public class ConverterRegistry implements Serializable {
*
* @param type 转换的目标类型
* @param converterClass 转换器类,必须有默认构造方法
* @return {@link ConverterRegistry}
* @return ConverterRegistry
*/
public ConverterRegistry putCustom(Type type, Class<? extends Converter<?>> converterClass) {
return putCustom(type, ReflectUtil.newInstance(converterClass));
@@ -151,7 +151,7 @@ public class ConverterRegistry implements Serializable {
*
* @param type 转换的目标类型
* @param converter 转换器
* @return {@link ConverterRegistry}
* @return ConverterRegistry
*/
public ConverterRegistry putCustom(Type type, Converter<?> converter) {
if (null == customConverterMap) {
@@ -257,6 +257,7 @@ public class ConverterRegistry implements Serializable {
}
}
// 特殊类型转换包括Collection、Map、强转、Array等
final T result = convertSpecial(type, rowType, value, defaultValue);
if (null != result) {
@@ -269,7 +270,7 @@ public class ConverterRegistry implements Serializable {
}
// 无法转换
throw new ConvertException("No Converter for type [{}]", rowType.getName());
throw new ConvertException("Can not Converter from [{}] to [{}]", value.getClass().getName(), type.getTypeName());
}
/**

View File

@@ -2,6 +2,7 @@ package cn.hutool.core.convert.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.TypeUtil;
import java.lang.reflect.Type;
@@ -60,13 +61,8 @@ public class CollectionConverter implements Converter<Collection<?>> {
@Override
public Collection<?> convert(Object value, Collection<?> defaultValue) throws IllegalArgumentException {
Collection<?> result;
try {
result = convertInternal(value);
} catch (RuntimeException e) {
return defaultValue;
}
return ((null == result) ? defaultValue : result);
final Collection<?> result = convertInternal(value);
return ObjectUtil.defaultIfNull(result, defaultValue);
}
/**

View File

@@ -207,7 +207,16 @@ public class NumberConverter extends AbstractConverter<Number> {
@Override
protected String convertToStr(Object value) {
return StrUtil.trim(super.convertToStr(value));
String result = StrUtil.trim(super.convertToStr(value));
if(StrUtil.isNotEmpty(result)){
final char c = Character.toUpperCase(result.charAt(result.length() - 1));
if(c == 'D' || c == 'L' || c == 'F'){
// 类型标识形式例如123.6D
return StrUtil.subPre(result, -1);
}
}
return result;
}
@Override

View File

@@ -428,7 +428,7 @@ public class LocalDateTimeUtil {
* @return 一天的开始时间
*/
public static LocalDateTime beginOfDay(LocalDateTime time) {
return time.with(LocalTime.of(0, 0, 0, 0));
return time.with(LocalTime.MIN);
}
/**
@@ -438,7 +438,7 @@ public class LocalDateTimeUtil {
* @return 一天的结束时间
*/
public static LocalDateTime endOfDay(LocalDateTime time) {
return time.with(LocalTime.of(23, 59, 59, 999_999_999));
return time.with(LocalTime.MAX);
}
/**

View File

@@ -5,6 +5,7 @@ import cn.hutool.core.date.DateUtil;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.concurrent.atomic.AtomicLong;
public class ConvertToNumberTest {
@@ -31,4 +32,13 @@ public class ConvertToNumberTest {
assert date != null;
Assert.assertEquals(date.getTime(), dateLong.longValue());
}
@Test
public void toBigDecimalTest(){
BigDecimal bigDecimal = Convert.toBigDecimal("1.1f");
Assert.assertEquals(1.1f, bigDecimal.floatValue(), 1);
bigDecimal = Convert.toBigDecimal("1L");
Assert.assertEquals(1L, bigDecimal.longValue());
}
}