mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
4c62f40bb6
commit
1ca70d893d
@ -227,15 +227,20 @@ public class CollUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含{@code null}元素
|
||||
* 是否包含{@code null}元素<br>
|
||||
* <ul>
|
||||
* <list>集合为{@code null},返回{@code true}</list>
|
||||
* <list>集合为空集合,即元素个数为0,返回{@code false}</list>
|
||||
* <list>集合中元素为"",返回{@code false}</list>
|
||||
* </ul>
|
||||
*
|
||||
* @param iterable 被检查的Iterable对象,如果为{@code null} 返回true
|
||||
* @return 是否包含{@code null}元素
|
||||
* @see IterUtil#hasNull(Iterable)
|
||||
* @see IterUtil#hasNull(Iterator)
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public static boolean hasNull(final Iterable<?> iterable) {
|
||||
return IterUtil.hasNull(iterable);
|
||||
return IterUtil.hasNull(IterUtil.getIter(iterable));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1359,6 +1364,10 @@ public class CollUtil {
|
||||
* @return 第一个元素,为空返回{@code null}
|
||||
*/
|
||||
public static <T> T getFirst(final Iterable<T> iterable) {
|
||||
if (iterable instanceof List) {
|
||||
final List<T> list = (List<T>) iterable;
|
||||
return CollUtil.isEmpty(list) ? null: list.get(0);
|
||||
}
|
||||
return IterUtil.getFirst(IterUtil.getIter(iterable));
|
||||
}
|
||||
|
||||
|
@ -87,17 +87,12 @@ public class IterUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含{@code null}元素
|
||||
*
|
||||
* @param iter 被检查的{@link Iterable}对象,如果为{@code null} 返回true
|
||||
* @return 是否包含{@code null}元素
|
||||
*/
|
||||
public static boolean hasNull(final Iterable<?> iter) {
|
||||
return hasNull(null == iter ? null : iter.iterator());
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含{@code null}元素
|
||||
* 是否包含{@code null}元素<br>
|
||||
* <ul>
|
||||
* <list>Iterator为{@code null},返回{@code true}</list>
|
||||
* <list>Iterator为空集合,即元素个数为0,返回{@code false}</list>
|
||||
* <list>Iterator中元素为"",返回{@code false}</list>
|
||||
* </ul>
|
||||
*
|
||||
* @param iter 被检查的{@link Iterator}对象,如果为{@code null} 返回true
|
||||
* @return 是否包含{@code null}元素
|
||||
|
@ -64,6 +64,9 @@ public class UTCDateParser extends DefaultDateBasic implements DateParser {
|
||||
if (length == DatePattern.UTC_SIMPLE_PATTERN.length() - 2) {
|
||||
// 格式类似:2018-09-13T05:34:31
|
||||
return new DateTime(source, DatePattern.UTC_SIMPLE_FORMAT);
|
||||
} else if (length == DatePattern.UTC_SIMPLE_PATTERN.length() - 5) {
|
||||
// 格式类似:2018-09-13T05:34
|
||||
return new DateTime(source + ":00", DatePattern.UTC_SIMPLE_FORMAT);
|
||||
} else if (StrUtil.contains(source, CharUtil.DOT)) {
|
||||
// 可能为: 2021-03-17T06:31:33.99
|
||||
return new DateTime(source, DatePattern.UTC_SIMPLE_MS_FORMAT);
|
||||
|
@ -267,7 +267,7 @@ public class NumberUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 补充Math.ceilDiv() JDK8中添加了和Math.floorDiv()但却没有ceilDiv()
|
||||
* 补充Math.ceilDiv() JDK8中添加了和 {@link Math#floorDiv(int, int)} 但却没有ceilDiv()
|
||||
*
|
||||
* @param v1 被除数
|
||||
* @param v2 除数
|
||||
@ -1448,6 +1448,7 @@ public class NumberUtil {
|
||||
* 4、空串返回0
|
||||
* 5、.123形式返回0(按照小于0的小数对待)
|
||||
* 6、123.56截取小数点之前的数字,忽略小数部分
|
||||
* 7、科学计数法抛出NumberFormatException异常
|
||||
* </pre>
|
||||
*
|
||||
* @param number 数字,支持0x开头、0开头和普通十进制
|
||||
@ -1460,6 +1461,11 @@ public class NumberUtil {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(StrUtil.containsIgnoreCase(number, "E")){
|
||||
// 科学计数法忽略支持,科学计数法一般用于表示非常小和非常大的数字,这类数字转换为int后精度丢失,没有意义。
|
||||
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", number));
|
||||
}
|
||||
|
||||
if (StrUtil.startWithIgnoreCase(number, "0x")) {
|
||||
// 0x04表示16进制数
|
||||
return Integer.parseInt(number.substring(2), 16);
|
||||
|
@ -15,6 +15,7 @@ import lombok.ToString;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@ -39,6 +40,34 @@ import java.util.function.Function;
|
||||
*/
|
||||
public class CollUtilTest {
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void emptyIfNullTest() {
|
||||
final Set<?> set = null;
|
||||
final Set<?> set1 = CollUtil.emptyIfNull(set);
|
||||
Assert.assertEquals(SetUtil.empty(), set1);
|
||||
|
||||
final List<?> list = null;
|
||||
final List<?> list1 = CollUtil.emptyIfNull(list);
|
||||
Assert.assertEquals(ListUtil.empty(), list1);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void hasNullTest() {
|
||||
ArrayList<Object> list = null;
|
||||
Assert.assertTrue(CollUtil.hasNull(list));
|
||||
|
||||
list = ListUtil.of();
|
||||
Assert.assertFalse(CollUtil.hasNull(list));
|
||||
|
||||
list = ListUtil.of("");
|
||||
Assert.assertFalse(CollUtil.hasNull(list));
|
||||
|
||||
list = ListUtil.of("", null);
|
||||
Assert.assertTrue(CollUtil.hasNull(list));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultIfEmpty() {
|
||||
ArrayList<String> strings = CollUtil.defaultIfEmpty(ListUtil.of(), ListUtil.of("1"));
|
||||
@ -50,7 +79,7 @@ public class CollUtilTest {
|
||||
|
||||
@Test
|
||||
public void defaultIfEmpty2() {
|
||||
ArrayList<String> strings = CollUtil.defaultIfEmpty(ListUtil.of(), Function.identity(), () -> ListUtil.of("1"));
|
||||
final ArrayList<String> strings = CollUtil.defaultIfEmpty(ListUtil.of(), Function.identity(), () -> ListUtil.of("1"));
|
||||
Assert.assertEquals(ListUtil.of("1"), strings);
|
||||
}
|
||||
|
||||
@ -322,7 +351,7 @@ public class CollUtilTest {
|
||||
|
||||
final List<String> removed = new ArrayList<>();
|
||||
final ArrayList<String> filtered = CollUtil.remove(list, t -> {
|
||||
if("a".equals(t)){
|
||||
if ("a".equals(t)) {
|
||||
removed.add(t);
|
||||
return true;
|
||||
}
|
||||
@ -484,8 +513,8 @@ public class CollUtilTest {
|
||||
|
||||
@Test
|
||||
public void listTest2() {
|
||||
final List<String> list1 = ListUtil.of( "a", "b", "c");
|
||||
final List<String> list2 = ListUtil.ofLinked( "a", "b", "c");
|
||||
final List<String> list1 = ListUtil.of("a", "b", "c");
|
||||
final List<String> list2 = ListUtil.ofLinked("a", "b", "c");
|
||||
Assert.assertEquals("[a, b, c]", list1.toString());
|
||||
Assert.assertEquals("[a, b, c]", list2.toString());
|
||||
}
|
||||
@ -749,7 +778,7 @@ public class CollUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapToMapTest(){
|
||||
public void mapToMapTest() {
|
||||
final HashMap<String, String> oldMap = new HashMap<>();
|
||||
oldMap.put("a", "1");
|
||||
oldMap.put("b", "12");
|
||||
@ -759,9 +788,9 @@ public class CollUtilTest {
|
||||
Map.Entry::getKey,
|
||||
entry -> Long.parseLong(entry.getValue()));
|
||||
|
||||
Assert.assertEquals(1L, (long)map.get("a"));
|
||||
Assert.assertEquals(12L, (long)map.get("b"));
|
||||
Assert.assertEquals(134L, (long)map.get("c"));
|
||||
Assert.assertEquals(1L, (long) map.get("a"));
|
||||
Assert.assertEquals(12L, (long) map.get("b"));
|
||||
Assert.assertEquals(134L, (long) map.get("c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -826,7 +855,7 @@ public class CollUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setValueByMapTest(){
|
||||
public void setValueByMapTest() {
|
||||
// https://gitee.com/dromara/hutool/pulls/482
|
||||
final List<Person> people = Arrays.asList(
|
||||
new Person("aa", 12, "man", 1),
|
||||
@ -867,13 +896,13 @@ public class CollUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctTest(){
|
||||
public void distinctTest() {
|
||||
final ArrayList<Integer> distinct = CollUtil.distinct(ListUtil.view(5, 3, 10, 9, 0, 5, 10, 9));
|
||||
Assert.assertEquals(ListUtil.view(5, 3, 10, 9, 0), distinct);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void distinctByFunctionTest(){
|
||||
public void distinctByFunctionTest() {
|
||||
final List<Person> people = Arrays.asList(
|
||||
new Person("aa", 12, "man", 1),
|
||||
new Person("bb", 13, "woman", 2),
|
||||
@ -906,14 +935,14 @@ public class CollUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapTest(){
|
||||
public void mapTest() {
|
||||
final ArrayList<String> list = ListUtil.of("a", "b", "c");
|
||||
final List<Object> extract = CollUtil.map(list, (e) -> e + "_1");
|
||||
Assert.assertEquals(ListUtil.of("a_1", "b_1", "c_1"), extract);
|
||||
Assert.assertEquals(ListUtil.of("a_1", "b_1", "c_1"), extract);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mapBeanTest(){
|
||||
public void mapBeanTest() {
|
||||
final List<Person> people = Arrays.asList(
|
||||
new Person("aa", 12, "man", 1),
|
||||
new Person("bb", 13, "woman", 2),
|
||||
@ -926,14 +955,14 @@ public class CollUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createTest(){
|
||||
public void createTest() {
|
||||
final Collection<Object> collection = CollUtil.create(Collections.emptyList().getClass());
|
||||
Console.log(collection.getClass());
|
||||
Assert.assertNotNull(collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void transTest(){
|
||||
public void transTest() {
|
||||
final List<Person> people = Arrays.asList(
|
||||
new Person("aa", 12, "man", 1),
|
||||
new Person("bb", 13, "woman", 2),
|
||||
@ -945,6 +974,7 @@ public class CollUtilTest {
|
||||
Assert.assertEquals("[aa, bb, cc, dd]", trans.toString());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void unionNullTest() {
|
||||
final List<String> list1 = new ArrayList<>();
|
||||
@ -954,6 +984,7 @@ public class CollUtilTest {
|
||||
Assert.assertNotNull(union);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void unionDistinctNullTest() {
|
||||
final List<String> list1 = new ArrayList<>();
|
||||
@ -963,6 +994,7 @@ public class CollUtilTest {
|
||||
Assert.assertNotNull(set);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void unionAllNullTest() {
|
||||
final List<String> list1 = new ArrayList<>();
|
||||
@ -972,6 +1004,7 @@ public class CollUtilTest {
|
||||
Assert.assertNotNull(list);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void intersectionNullTest() {
|
||||
final List<String> list1 = new ArrayList<>();
|
||||
@ -983,6 +1016,7 @@ public class CollUtilTest {
|
||||
Assert.assertNotNull(collection);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
public void intersectionDistinctNullTest() {
|
||||
final List<String> list1 = new ArrayList<>();
|
||||
@ -1024,8 +1058,20 @@ public class CollUtilTest {
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
static class Dog extends Animal {
|
||||
public Dog(String name, Integer age) {
|
||||
public Dog(final String name, final Integer age) {
|
||||
super(name, age);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFirstTest() {
|
||||
Assert.assertNull(CollUtil.getFirst(null));
|
||||
Assert.assertNull(CollUtil.getFirst(ListUtil.of()));
|
||||
|
||||
Assert.assertEquals("1", CollUtil.getFirst(ListUtil.of("1", "2", "3")));
|
||||
final ArrayDeque<String> deque = new ArrayDeque<>();
|
||||
deque.add("3");
|
||||
deque.add("4");
|
||||
Assert.assertEquals("3", CollUtil.getFirst(deque));
|
||||
}
|
||||
}
|
||||
|
@ -682,6 +682,15 @@ public class DateUtilTest {
|
||||
Assert.assertEquals("2021-03-30 12:56:51", Objects.requireNonNull(parse).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseUTCTest3() {
|
||||
// issue#I5M6DP
|
||||
final String dateStr = "2022-08-13T09:30";
|
||||
final DateTime dateTime = DateUtil.parse(dateStr);
|
||||
Assert.assertNotNull(dateTime);
|
||||
Assert.assertEquals("2022-08-13 09:30:00", dateTime.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseCSTTest() {
|
||||
final String dateStr = "Wed Sep 16 11:26:23 CST 2009";
|
||||
|
@ -12,7 +12,6 @@ import java.math.RoundingMode;
|
||||
* {@link NumberUtil} 单元测试类
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class NumberUtilTest {
|
||||
|
||||
@ -50,7 +49,7 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBlankTest(){
|
||||
public void addBlankTest() {
|
||||
final BigDecimal result = NumberUtil.add("123", " ");
|
||||
Assert.assertEquals(new BigDecimal("123"), result);
|
||||
}
|
||||
@ -74,7 +73,7 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mulNullTest(){
|
||||
public void mulNullTest() {
|
||||
final BigDecimal mul = NumberUtil.mul(new BigDecimal("10"), null);
|
||||
Assert.assertEquals(BigDecimal.ZERO, mul);
|
||||
}
|
||||
@ -206,7 +205,7 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decimalFormatNaNTest(){
|
||||
public void decimalFormatNaNTest() {
|
||||
final Double a = 0D;
|
||||
final Double b = 0D;
|
||||
|
||||
@ -215,7 +214,7 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decimalFormatNaNTest2(){
|
||||
public void decimalFormatNaNTest2() {
|
||||
final Double a = 0D;
|
||||
final Double b = 0D;
|
||||
|
||||
@ -300,6 +299,20 @@ public class NumberUtilTest {
|
||||
Assert.assertEquals(1482, v1);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void parseIntTest3() {
|
||||
final int v1 = NumberUtil.parseInt("d");
|
||||
Assert.assertEquals(0, v1);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void parseIntTest4() {
|
||||
// issue#I5M55F
|
||||
// 科学计数法忽略支持,科学计数法一般用于表示非常小和非常大的数字,这类数字转换为int后精度丢失,没有意义。
|
||||
final String numberStr = "429900013E20220812163344551";
|
||||
NumberUtil.parseInt(numberStr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNumberTest() {
|
||||
// from 5.4.8 issue#I23ORQ@Gitee
|
||||
@ -311,6 +324,15 @@ public class NumberUtilTest {
|
||||
Assert.assertEquals(1482L, v2.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNumberTest2() {
|
||||
// issue#I5M55F
|
||||
final String numberStr = "429900013E20220812163344551";
|
||||
final Number number = NumberUtil.parseNumber(numberStr);
|
||||
Assert.assertNotNull(number);
|
||||
Assert.assertTrue(number instanceof BigDecimal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseHexNumberTest() {
|
||||
// 千位分隔符去掉
|
||||
@ -359,7 +381,7 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStrTest(){
|
||||
public void toStrTest() {
|
||||
Assert.assertEquals("1", NumberUtil.toStr(new BigDecimal("1.0000000000")));
|
||||
Assert.assertEquals("0", NumberUtil.toStr(NumberUtil.sub(new BigDecimal("9600.00000"), new BigDecimal("9600.00000"))));
|
||||
Assert.assertEquals("0", NumberUtil.toStr(NumberUtil.sub(new BigDecimal("9600.0000000000"), new BigDecimal("9600.000000"))));
|
||||
@ -367,15 +389,15 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toPlainNumberTest(){
|
||||
public void toPlainNumberTest() {
|
||||
final String num = "5344.34234e3";
|
||||
final String s = new BigDecimal(num).toPlainString();
|
||||
Assert.assertEquals("5344342.34", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isOddOrEvenTest(){
|
||||
final int[] a = { 0, 32, -32, 123, -123 };
|
||||
public void isOddOrEvenTest() {
|
||||
final int[] a = {0, 32, -32, 123, -123};
|
||||
Assert.assertFalse(NumberUtil.isOdd(a[0]));
|
||||
Assert.assertTrue(NumberUtil.isEven(a[0]));
|
||||
|
||||
@ -393,21 +415,21 @@ public class NumberUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toBigIntegerTest(){
|
||||
final Number number=1123123;
|
||||
final Number number2=1123123.123;
|
||||
public void toBigIntegerTest() {
|
||||
final Number number = 1123123;
|
||||
final Number number2 = 1123123.123;
|
||||
Assert.assertNotNull(NumberUtil.toBigInteger(number));
|
||||
Assert.assertNotNull(NumberUtil.toBigInteger(number2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void divIntegerTest(){
|
||||
public void divIntegerTest() {
|
||||
final BigDecimal div = NumberUtil.div(100101300, 100);
|
||||
Assert.assertEquals(1001013, div.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isDoubleTest(){
|
||||
public void isDoubleTest() {
|
||||
Assert.assertFalse(NumberUtil.isDouble(null));
|
||||
Assert.assertFalse(NumberUtil.isDouble(""));
|
||||
Assert.assertFalse(NumberUtil.isDouble(" "));
|
||||
@ -434,10 +456,4 @@ public class NumberUtilTest {
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
NumberUtil.range(0, Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void parseIntTest3() {
|
||||
int v1 = NumberUtil.parseInt("d");
|
||||
Assert.assertEquals(0, v1);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user