Skip to content

Commit

Permalink
!177 新增一些工具方法
Browse files Browse the repository at this point in the history
Merge pull request !177 from easepan/feat/init-with-of
  • Loading branch information
looly authored and gitee-org committed Sep 10, 2020
2 parents 0863461 + cdbbaf1 commit dfa0191
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
15 changes: 15 additions & 0 deletions hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,19 @@ public static <T> List<T> unmodifiable(List<T> list) {
public static <T> List<T> empty() {
return Collections.emptyList();
}

/**
* 像java11一样获取一个List
* @param ts 对象
* @param <T> 对象类型
* @return 不可修改List
*/
public static <T> List<T> of(T... ts) {
if (ArrayUtil.isEmpty(ts)) {
return Collections.emptyList();
}
List<T> unmodifiableList = new ArrayList<>(ts.length);
Collections.addAll(unmodifiableList, ts);
return Collections.unmodifiableList(unmodifiableList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Arrays;

/**
* 比较器链。此链包装了多个比较器,最终比较结果按照比较器顺序综合多个比较器结果。<br>
Expand Down Expand Up @@ -81,6 +82,26 @@ public ComparatorChain(final List<Comparator<E>> list, final BitSet bits) {
orderingBits = bits;
}

public static <E> ComparatorChain<E> of(Comparator<E> comparator) {
return of(comparator, false);
}

public static <E> ComparatorChain<E> of(Comparator<E> comparator, boolean reverse) {
return new ComparatorChain<>(comparator, reverse);
}

public static <E> ComparatorChain<E> of(Comparator<E>... comparators) {
return of(Arrays.asList(comparators));
}

public static <E> ComparatorChain<E> of(List<Comparator<E>> comparators) {
return new ComparatorChain<>(comparators);
}

public static <E> ComparatorChain<E> of(List<Comparator<E>> comparators, BitSet bits) {
return new ComparatorChain<>(comparators, bits);
}

// -----------------------------------------------------------------------
/**
* 在链的尾部添加比较器,使用正向排序
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cn.hutool.core.comparator;

import java.text.Collator;
import java.util.Comparator;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Function;

/**
* 比较工具类
Expand Down Expand Up @@ -108,4 +112,34 @@ public static <T> int compare(T o1, T o2, boolean isNullGreater) {

return result;
}

/**
* 中文比较器
*
* @param keyExtractor 从对象中提取中文
* @param <T> 对象类型
*
* @return 中文比较器
*/
public static <T> Comparator<T> comparingPinyin(Function<T, String> keyExtractor) {
return comparingPinyin(keyExtractor, false);
}

/**
* 中文比较器
*
* @param keyExtractor 从对象中提取中文
* @param reverse 是否反序
* @param <T> 对象类型
*
* @return 中文比较器
*/
public static <T> Comparator<T> comparingPinyin(Function<T, String> keyExtractor, boolean reverse) {
Objects.requireNonNull(keyExtractor);
PinyinComparator pinyinComparator = new PinyinComparator();
if (reverse) {
return (o1, o2) -> pinyinComparator.compare(keyExtractor.apply(o2), keyExtractor.apply(o1));
}
return (o1, o2) -> pinyinComparator.compare(keyExtractor.apply(o1), keyExtractor.apply(o2));
}
}
4 changes: 4 additions & 0 deletions hutool-core/src/main/java/cn/hutool/core/lang/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public Pair(K key, V value) {
this.value = value;
}

public static <K, V> Pair<K, V> of(K key, V value) {
return new Pair<>(key, value);
}

/**
* 获取键
* @return 键
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package cn.hutool.core.comparator;

import cn.hutool.core.collection.ListUtil;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CompareUtilTest {

@Test
Expand All @@ -13,4 +18,21 @@ public void compareTest(){
compare = CompareUtil.compare(null, "a", false);
Assert.assertTrue(compare < 0);
}

@Test
public void comparingPinyin() {
List<String> list = new ArrayList<>();
Collections.addAll(list, "成都", "北京", "上海", "深圳");

List<String> ascendingOrderResult = ListUtil.of("北京", "成都", "上海", "深圳");
List<String> descendingOrderResult = ListUtil.of("深圳", "上海", "成都", "北京");

// 正序
list.sort(CompareUtil.comparingPinyin(e -> e));
Assert.assertEquals(list, ascendingOrderResult);

// 反序
list.sort(CompareUtil.comparingPinyin(e -> e, true));
Assert.assertEquals(list, descendingOrderResult);
}
}

0 comments on commit dfa0191

Please sign in to comment.