Merge branch 'v6-dev' of gitee.com:dromara/hutool into v6-dev

This commit is contained in:
Looly
2023-07-24 23:51:49 +08:00
12 changed files with 315 additions and 100 deletions

View File

@@ -1,5 +1,6 @@
package org.dromara.hutool.core.collection;
import lombok.*;
import org.dromara.hutool.core.collection.iter.IterUtil;
import org.dromara.hutool.core.collection.set.SetUtil;
import org.dromara.hutool.core.comparator.CompareUtil;
@@ -8,32 +9,10 @@ import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.Dict;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.*;
import java.util.function.Function;
/**
@@ -460,9 +439,9 @@ public class CollUtilTest {
@Test
public void sortByPropertyTest() {
final List<TestBean> list = ListUtil.of(
new TestBean("张三", 12, DateUtil.parse("2018-05-01")), //
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 12, DateUtil.parse("2018-04-01"))//
new TestBean("张三", 12, DateUtil.parse("2018-05-01")), //
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 12, DateUtil.parse("2018-04-01"))//
);
CollUtil.sortByProperty(list, "createTime");
@@ -474,9 +453,9 @@ public class CollUtilTest {
@Test
public void sortByPropertyTest2() {
final List<TestBean> list = ListUtil.of(
new TestBean("张三", 0, DateUtil.parse("2018-05-01")), //
new TestBean("李四", -12, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 23, DateUtil.parse("2018-04-01"))//
new TestBean("张三", 0, DateUtil.parse("2018-05-01")), //
new TestBean("李四", -12, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 23, DateUtil.parse("2018-04-01"))//
);
CollUtil.sortByProperty(list, "age");
@@ -488,8 +467,8 @@ public class CollUtilTest {
@Test
public void fieldValueMapTest() {
final List<TestBean> list = ListUtil.of(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), //
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 12, DateUtil.parse("2018-04-01"))//
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 12, DateUtil.parse("2018-04-01"))//
);
final Map<String, TestBean> map = CollUtil.fieldValueMap(list, "name");
@@ -501,8 +480,8 @@ public class CollUtilTest {
@Test
public void fieldValueAsMapTest() {
final List<TestBean> list = ListUtil.of(new TestBean("张三", 12, DateUtil.parse("2018-05-01")), //
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 14, DateUtil.parse("2018-04-01"))//
new TestBean("李四", 13, DateUtil.parse("2018-03-01")), //
new TestBean("王五", 14, DateUtil.parse("2018-04-01"))//
);
final Map<String, Integer> map = CollUtil.fieldValueAsMap(list, "name", "age");
@@ -669,7 +648,7 @@ public class CollUtilTest {
@Test
public void subInput1PositiveNegativePositiveOutputArrayIndexOutOfBoundsException() {
Assertions.assertThrows(IndexOutOfBoundsException.class, ()->{
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {
// Arrange
final List<Integer> list = new ArrayList<>();
list.add(null);
@@ -821,8 +800,8 @@ public class CollUtilTest {
oldMap.put("c", "134");
final Map<String, Long> map = IterUtil.toMap(oldMap.entrySet(),
Map.Entry::getKey,
entry -> Long.parseLong(entry.getValue()));
Map.Entry::getKey,
entry -> Long.parseLong(entry.getValue()));
Assertions.assertEquals(1L, (long) map.get("a"));
Assertions.assertEquals(12L, (long) map.get("b"));
@@ -900,12 +879,12 @@ public class CollUtilTest {
public void setValueByMapTest() {
// https://gitee.com/dromara/hutool/pulls/482
final List<Person> people = Arrays.asList(
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4),
new Person("ee", 16, "woman", 5),
new Person("ff", 17, "man", 6)
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4),
new Person("ee", 16, "woman", 5),
new Person("ff", 17, "man", 6)
);
final Map<Integer, String> genderMap = new HashMap<>();
@@ -946,12 +925,12 @@ public class CollUtilTest {
@Test
public void distinctByFunctionTest() {
final List<Person> people = Arrays.asList(
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4),
new Person("ee", 16, "woman", 5),
new Person("ff", 17, "man", 6)
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4),
new Person("ee", 16, "woman", 5),
new Person("ff", 17, "man", 6)
);
// 覆盖模式下ff覆盖了aaee覆盖了bb
@@ -986,10 +965,10 @@ public class CollUtilTest {
@Test
public void mapBeanTest() {
final List<Person> people = Arrays.asList(
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4)
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4)
);
final List<Object> extract = CollUtil.map(people, Person::getName);
@@ -1006,10 +985,10 @@ public class CollUtilTest {
@Test
public void transTest() {
final List<Person> people = Arrays.asList(
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4)
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4)
);
final Collection<String> trans = CollUtil.trans(people, Person::getName);
@@ -1046,8 +1025,8 @@ public class CollUtilTest {
Assertions.assertNotNull(list);
Assertions.assertEquals(
ListUtil.of(1, 2, 3, 4),
CollUtil.unionAll(ListUtil.of(1), ListUtil.of(2), ListUtil.of(3), ListUtil.of(4))
ListUtil.of(1, 2, 3, 4),
CollUtil.unionAll(ListUtil.of(1), ListUtil.of(2), ListUtil.of(3), ListUtil.of(4))
);
}
@@ -1083,14 +1062,14 @@ public class CollUtilTest {
Assertions.assertFalse(CollUtil.addIfAbsent(null, "123"));
Assertions.assertFalse(CollUtil.addIfAbsent(ListUtil.of("123"), "123"));
Assertions.assertFalse(CollUtil.addIfAbsent(ListUtil.of(new Animal("jack", 20)),
new Animal("jack", 20)));
new Animal("jack", 20)));
// 正常情况
Assertions.assertTrue(CollUtil.addIfAbsent(ListUtil.of("456"), "123"));
Assertions.assertTrue(CollUtil.addIfAbsent(ListUtil.of(new Animal("jack", 20)),
new Dog("jack", 20)));
new Dog("jack", 20)));
Assertions.assertTrue(CollUtil.addIfAbsent(ListUtil.of(new Animal("jack", 20)),
new Animal("tom", 20)));
new Animal("tom", 20)));
}
@Data
@@ -1224,4 +1203,55 @@ public class CollUtilTest {
final List<Pig> pig = Arrays.asList(new Pig("pig1", 12), new Pig("pig2", 12));
Assertions.assertEquals(CollUtil.unionDistinct(dog, cat, pig).size(), 5);
}
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
@Test
public void flatListTest1() {
final List<List<List<String>>> list = Arrays.asList(Arrays.asList(Arrays.asList("1", "2", "3"), Arrays.asList("5", "6", "7")));
final List<Object> objects = CollUtil.flat(list);
Assertions.assertArrayEquals(new String[]{"1", "2", "3", "5", "6", "7"}, objects.toArray());
}
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
@Test
public void flatListTest2() {
final List<List<List<String>>> list = Arrays.asList(
Arrays.asList(
Arrays.asList("a"),
Arrays.asList("b", "c"),
Arrays.asList("d", "e", "f")
),
Arrays.asList(
Arrays.asList("g", "h", "i"),
Arrays.asList("j", "k", "l")
)
);
final List<Object> flat = CollUtil.flat(list);
Assertions.assertArrayEquals(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}, flat.toArray());
}
@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
@Test
void flatListTest3() {
final List<List<List<String>>> list = Arrays.asList(
Arrays.asList(
Arrays.asList("a"),
Arrays.asList("b", "c", null),
Arrays.asList("d", "e", "f")
),
Arrays.asList(
Arrays.asList("g", "h", "i"),
Arrays.asList("j", "k", "l")
)
);
final List<Object> flat = CollUtil.flat(list, false);
Assertions.assertArrayEquals(new String[]{"a", "b", "c", null, "d", "e", "f", "g", "h", "i", "j", "k", "l"}, flat.toArray());
}
}

View File

@@ -53,4 +53,10 @@ public class VersionComparatorTest {
final VersionComparator other = new VersionComparator();
Assertions.assertNotEquals(first, other);
}
@Test
public void versionComparatorTest7() {
final int compare = VersionComparator.INSTANCE.compare("1.12.2", "1.12.1c");
Assertions.assertTrue(compare > 0);
}
}

View File

@@ -708,4 +708,23 @@ public class AbstractEnhancedWrappedStreamTest {
private List<Tree> children;
}
@Test
void test() {
List<List<List<String>>> list = Arrays.asList(
Arrays.asList(
Arrays.asList("a"),
Arrays.asList("b", "c"),
Arrays.asList("d", "e", "f")
),
Arrays.asList(
Arrays.asList("g", "h", "i"),
Arrays.asList("j", "k", "l")
)
);
List<String> r = EasyStream.of(list).<String>flat().toList();
Assertions.assertArrayEquals(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"}, r.toArray());
}
}