This commit is contained in:
Looly
2023-03-26 04:58:33 +08:00
parent 611db9a56b
commit 1d4d4e0e76
8 changed files with 78 additions and 65 deletions

View File

@@ -35,8 +35,8 @@ public class CompareUtilTest {
@Test
public void comparingIndexedTest() {
List<String> data = ListUtil.of("1", "2", "3", "4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
List<String> index = ListUtil.view("2", "1", "3", "4");
final List<String> data = ListUtil.of("1", "2", "3", "4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
final List<String> index = ListUtil.view("2", "1", "3", "4");
data.sort(CompareUtil.comparingIndexed(e -> e, index));
//[1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@@ -45,8 +45,8 @@ public class CompareUtilTest {
@Test
public void comparingIndexedTest2() {
List<String> data = ListUtil.of("1", "2", "3", "4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
List<String> index = ListUtil.view("2", "1", "3", "4");
final List<String> data = ListUtil.of("1", "2", "3", "4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
final List<String> index = ListUtil.view("2", "1", "3", "4");
//正确排序index.toArray()
data.sort(CompareUtil.comparingIndexed(e -> e, index.toArray()));
@@ -55,12 +55,22 @@ public class CompareUtilTest {
}
@Test
public void comparingIndexedTest3() {
List<String> data = ListUtil.of("1", "2", "3", "4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
String[] indexArray = new String[] {"2", "1", "3", "4"};
final List<String> data = ListUtil.of("1", "2", "3", "4", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
final String[] indexArray = new String[] {"2", "1", "3", "4"};
//正确排序array
data.sort(CompareUtil.comparingIndexed(e -> e, indexArray));
//[5, 6, 7, 8, 9, 10, 2, 2, 1, 1, 3, 3, 4, 4]
Assert.assertEquals(data, ListUtil.view("5", "6", "7", "8", "9", "10", "2", "2", "1", "1", "3", "3", "4", "4"));
}
@Test
public void compareNullTest() {
Assert.assertEquals(0, CompareUtil.compare(1, 1));
Assert.assertEquals(1, CompareUtil.compare(1, null));
Assert.assertEquals(-1, CompareUtil.compare(null, 1));
Assert.assertEquals(-1, CompareUtil.compare(1, null, true));
Assert.assertEquals(1, CompareUtil.compare(null, 1, true));
}
}

View File

@@ -0,0 +1,56 @@
package cn.hutool.core.comparator;
import cn.hutool.core.collection.ListUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class PropertyComparatorTest {
@Test
public void sortNullTest() {
final ArrayList<User> users = ListUtil.of(
new User("1", "d"),
new User("2", null),
new User("3", "a")
);
// 默认null在末尾
final List<User> sortedList1 = ListUtil.sort(users, new PropertyComparator<>("b"));
Assert.assertEquals("a", sortedList1.get(0).getB());
Assert.assertEquals("d", sortedList1.get(1).getB());
Assert.assertNull(sortedList1.get(2).getB());
// null在首
final List<User> sortedList2 = ListUtil.sort(users, new PropertyComparator<>("b", false));
Assert.assertNull(sortedList2.get(0).getB());
Assert.assertEquals("a", sortedList2.get(1).getB());
Assert.assertEquals("d", sortedList2.get(2).getB());
}
@Test
public void reversedTest() {
final ArrayList<User> users = ListUtil.of(
new User("1", "d"),
new User("2", null),
new User("3", "a")
);
// 反序
final List<User> sortedList = ListUtil.sort(users, new PropertyComparator<>("b").reversed());
Assert.assertNull(sortedList.get(0).getB());
Assert.assertEquals("d", sortedList.get(1).getB());
Assert.assertEquals("a", sortedList.get(2).getB());
}
@Data
@AllArgsConstructor
static class User{
private String a;
private String b;
}
}

View File

@@ -199,16 +199,6 @@ public class ObjUtilTest {
Assert.assertTrue(ObjUtil.isValidIfNumber(Float.MIN_VALUE));
}
@Test
public void compareTest() {
Assert.assertEquals(0, ObjUtil.compare(1, 1));
Assert.assertEquals(1, ObjUtil.compare(1, null));
Assert.assertEquals(-1, ObjUtil.compare(null, 1));
Assert.assertEquals(-1, ObjUtil.compare(1, null, true));
Assert.assertEquals(1, ObjUtil.compare(null, 1, true));
}
@Test
public void getTypeArgumentTest() {
final Bean bean = new Bean(1);