mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
remove class
This commit is contained in:
parent
608928d400
commit
76ba6f0836
@ -1,53 +0,0 @@
|
||||
package cn.hutool.core.comparator;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* 针对 {@link Comparable}对象的默认比较器
|
||||
*
|
||||
* @param <E> 比较对象类型
|
||||
* @author Looly
|
||||
* @since 3.0.7
|
||||
*/
|
||||
public class ComparableComparator<E extends Comparable<? super E>> implements Comparator<E>, Serializable {
|
||||
private static final long serialVersionUID = 3020871676147289162L;
|
||||
|
||||
/** 单例 */
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static final ComparableComparator INSTANCE = new ComparableComparator<>();
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public ComparableComparator() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个{@link Comparable}对象
|
||||
*
|
||||
* <pre>
|
||||
* obj1.compareTo(obj2)
|
||||
* </pre>
|
||||
*
|
||||
* @param obj1 被比较的第一个对象
|
||||
* @param obj2 the second object to compare
|
||||
* @return obj1小返回负数,大返回正数,否则返回0
|
||||
* @throws NullPointerException obj1为{@code null}或者比较中抛出空指针异常
|
||||
*/
|
||||
@Override
|
||||
public int compare(final E obj1, final E obj2) {
|
||||
return obj1.compareTo(obj2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return "ComparableComparator".hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object object) {
|
||||
return this == object || null != object && object.getClass().equals(this.getClass());
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package cn.hutool.core.comparator;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
@ -98,19 +98,50 @@ public class CompareUtil {
|
||||
*
|
||||
* <ul>
|
||||
* <li>如需对null友好操作如下</li>
|
||||
* <li><code>Comparator.nullsFirst(Comparator.naturalOrder())</code></li>
|
||||
* <li><code>Comparator.nullsLast(Comparator.naturalOrder())</code></li>
|
||||
* <li><code>Comparator.nullsLast(CompareUtil.naturalComparator())</code></li>
|
||||
* <li><code>Comparator.nullsFirst(CompareUtil.naturalComparator())</code></li>
|
||||
* <li><code>Comparator.nullsLast(CompareUtil.natural())</code></li>
|
||||
* <li><code>Comparator.nullsFirst(CompareUtil.natural())</code></li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <E> 排序节点类型
|
||||
* @return 默认排序器
|
||||
* @since 5.7.21
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E extends Comparable<? super E>> Comparator<E> naturalComparator() {
|
||||
return ComparableComparator.INSTANCE;
|
||||
public static <E extends Comparable<? super E>> Comparator<E> natural() {
|
||||
return Comparator.naturalOrder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取反序排序器,即默认自然排序的反序排序器
|
||||
*
|
||||
* <ul>
|
||||
* <li>如需对null友好操作如下</li>
|
||||
* <li><code>Comparator.nullsLast(CompareUtil.naturalReverse())</code></li>
|
||||
* <li><code>Comparator.nullsFirst(CompareUtil.naturalReverse())</code></li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <E> 排序节点类型
|
||||
* @return 默认排序器
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static <E extends Comparable<? super E>> Comparator<E> naturalReverse() {
|
||||
return Comparator.reverseOrder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取反序排序器,即默认排序器
|
||||
*
|
||||
* <ul>
|
||||
* <li>如需对null友好操作如下</li>
|
||||
* <li><code>Comparator.nullsLast(CompareUtil.reverse())</code></li>
|
||||
* <li><code>Comparator.nullsFirst(CompareUtil.reverse())</code></li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <E> 排序节点类型
|
||||
* @return 默认排序器
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static <E extends Comparable<? super E>> Comparator<E> reverse(final Comparator<E> comparator) {
|
||||
return null == comparator ? naturalReverse() : comparator.reversed();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -255,7 +286,6 @@ public class CompareUtil {
|
||||
* @return 索引比较器
|
||||
* @since 5.8.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, U> Comparator<T> comparingIndexed(final Function<? super T, ? extends U> keyExtractor, final U[] objs) {
|
||||
return comparingIndexed(keyExtractor, false, objs);
|
||||
}
|
||||
@ -273,12 +303,8 @@ public class CompareUtil {
|
||||
* @since 6.0.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, U> Comparator<T> comparingIndexed(final Function<? super T, ? extends U> keyExtractor, final Collection<U> objs) {
|
||||
U[] array = null;
|
||||
if (objs != null && objs.size() > 0) {
|
||||
array = objs.toArray((U[]) Array.newInstance(objs.iterator().next().getClass(), objs.size()));
|
||||
}
|
||||
return comparingIndexed(keyExtractor, false, array);
|
||||
public static <T, U> Comparator<T> comparingIndexed(final Function<? super T, ? extends U> keyExtractor, final Iterable<U> objs) {
|
||||
return comparingIndexed(keyExtractor, false, ArrayUtil.toArray(objs, (Class<U>) objs.iterator().next().getClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,49 +0,0 @@
|
||||
package cn.hutool.core.comparator;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* 反转比较器
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
* @param <E> 被比较对象类型
|
||||
*/
|
||||
public class ReverseComparator<E> implements Comparator<E>, Serializable {
|
||||
private static final long serialVersionUID = 8083701245147495562L;
|
||||
|
||||
/** 原始比较器 */
|
||||
private final Comparator<? super E> comparator;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ReverseComparator(final Comparator<? super E> comparator) {
|
||||
this.comparator = (null == comparator) ? ComparableComparator.INSTANCE : comparator;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
@Override
|
||||
public int compare(final E o1, final E o2) {
|
||||
return comparator.compare(o2, o1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return "ReverseComparator".hashCode() ^ comparator.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object object) {
|
||||
if (this == object) {
|
||||
return true;
|
||||
}
|
||||
if (null == object) {
|
||||
return false;
|
||||
}
|
||||
if (object.getClass().equals(this.getClass())) {
|
||||
final ReverseComparator<?> thatrc = (ReverseComparator<?>) object;
|
||||
return comparator.equals(thatrc.comparator);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@ import java.lang.reflect.Array;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
@ -1307,22 +1306,8 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static <T> T[] toArray(final Iterable<T> iterable, final Class<T> componentType) {
|
||||
return toArray(CollUtil.toCollection(iterable), componentType);
|
||||
return CollUtil.toCollection(iterable).toArray(newArray(componentType, 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将集合转为数组
|
||||
*
|
||||
* @param <T> 数组元素类型
|
||||
* @param collection 集合
|
||||
* @param componentType 集合元素类型
|
||||
* @return 数组
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static <T> T[] toArray(final Collection<T> collection, final Class<T> componentType) {
|
||||
return collection.toArray(newArray(componentType, 0));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------- remove
|
||||
|
||||
/**
|
||||
|
@ -75,7 +75,7 @@ public class JSONConfig implements Serializable {
|
||||
* @since 5.7.21
|
||||
*/
|
||||
public JSONConfig setNatureKeyComparator() {
|
||||
return setKeyComparator(CompareUtil.naturalComparator());
|
||||
return setKeyComparator(CompareUtil.natural());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user