mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package cn.hutool.core.comparator;
|
||||
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -53,7 +51,7 @@ public class FuncComparator<T> extends NullComparator<T> {
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
private int compare(final T o1, final T o2, final Comparable v1, final Comparable v2) {
|
||||
int result = ObjUtil.compare(v1, v2);
|
||||
int result = CompareUtil.compare(v1, v2, this.nullGreater);
|
||||
if (0 == result) {
|
||||
//避免TreeSet / TreeMap 过滤掉排序字段相同但是对象不相同的情况
|
||||
result = CompareUtil.compare(o1, o2, this.nullGreater);
|
||||
|
@@ -51,11 +51,6 @@ public class NullComparator<T> implements Comparator<T>, Serializable {
|
||||
return new NullComparator<>(nullGreater, comparator == null ? other : comparator.thenComparing(other));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparator<T> reversed() {
|
||||
return new NullComparator<>((false == nullGreater), comparator == null ? null : comparator.reversed());
|
||||
}
|
||||
|
||||
/**
|
||||
* 不检查{@code null}的比较方法<br>
|
||||
* 用户可自行重写此方法自定义比较方式
|
||||
|
@@ -14,7 +14,7 @@ public class PropertyComparator<T> extends FuncComparator<T> {
|
||||
private static final long serialVersionUID = 9157326766723846313L;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* 构造,默认{@code null}排在后(从小到大排序)
|
||||
*
|
||||
* @param property 属性名
|
||||
*/
|
||||
@@ -26,7 +26,7 @@ public class PropertyComparator<T> extends FuncComparator<T> {
|
||||
* 构造
|
||||
*
|
||||
* @param property 属性名
|
||||
* @param isNullGreater null值是否排在后(从小到大排序)
|
||||
* @param isNullGreater {@code null}值是否排在后(从小到大排序)
|
||||
*/
|
||||
public PropertyComparator(final String property, final boolean isNullGreater) {
|
||||
super(isNullGreater, (bean)-> BeanUtil.getProperty(bean, property));
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.core.regex;
|
||||
|
||||
import cn.hutool.core.collection.SetUtil;
|
||||
import cn.hutool.core.comparator.CompareUtil;
|
||||
import cn.hutool.core.comparator.StrLengthComparator;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
@@ -11,15 +12,8 @@ import cn.hutool.core.lang.mutable.MutableObj;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.reflect.MethodUtil;
|
||||
import cn.hutool.core.text.StrUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.MatchResult;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -275,7 +269,7 @@ public class ReUtil {
|
||||
}
|
||||
|
||||
//提取模板中的编号
|
||||
final TreeSet<Integer> varNums = new TreeSet<>((o1, o2) -> ObjUtil.compare(o2, o1));
|
||||
final TreeSet<Integer> varNums = new TreeSet<>((o1, o2) -> CompareUtil.compare(o2, o1));
|
||||
final Matcher matcherForTemplate = PatternPool.GROUP_VAR.matcher(template);
|
||||
while (matcherForTemplate.find()) {
|
||||
varNums.add(Integer.parseInt(matcherForTemplate.group(1)));
|
||||
|
@@ -2,7 +2,6 @@ package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.iter.IterUtil;
|
||||
import cn.hutool.core.comparator.CompareUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.io.SerializeUtil;
|
||||
@@ -418,35 +417,6 @@ public class ObjUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code null}安全的对象比较,{@code null}对象小于其他对象
|
||||
*
|
||||
* @param <T> 被比较对象类型
|
||||
* @param c1 对象1,可以为{@code null}
|
||||
* @param c2 对象2,可以为{@code null}
|
||||
* @return 比较结果,如果{@code c1 < c2},则返回值小于0,{@code c1 == c2} 返回0,{@code c1 > c2} 则返回值大于0
|
||||
* @see CompareUtil#compare(Comparable, Comparable)
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public static <T extends Comparable<? super T>> int compare(final T c1, final T c2) {
|
||||
return CompareUtil.compare(c1, c2);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code null}安全的对象比较
|
||||
*
|
||||
* @param <T> 被比较对象类型
|
||||
* @param c1 对象1,可以为{@code null}
|
||||
* @param c2 对象2,可以为{@code null}
|
||||
* @param nullGreater {@code null}对象是否大于其他对象
|
||||
* @return 比较结果,如果{@code c1 < c2},则返回值小于0,{@code c1 == c2} 返回0,{@code c1 > c2} 则返回值大于0
|
||||
* @see CompareUtil#compare(Comparable, Comparable, boolean)
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public static <T extends Comparable<? super T>> int compare(final T c1, final T c2, final boolean nullGreater) {
|
||||
return CompareUtil.compare(c1, c2, nullGreater);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得给定类的第一个泛型参数
|
||||
*
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user