mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix code
This commit is contained in:
parent
dfa0191b36
commit
76c6aeeb56
@ -3,9 +3,11 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.4.3 (2020-09-09)
|
||||
# 5.4.3 (2020-09-10)
|
||||
|
||||
### 新特性
|
||||
* 【core 】 使用静态的of方法来new对象(pr#177@Gitee)
|
||||
|
||||
### Bug修复
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
@ -149,6 +149,23 @@ public class ListUtil {
|
||||
return (LinkedList<T>) list(true, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数组转为一个不可变List<br>
|
||||
* 类似于Java9中的List.of
|
||||
*
|
||||
* @param ts 对象
|
||||
* @param <T> 对象类型
|
||||
* @return 不可修改List
|
||||
* @since 5.4.3
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <T> List<T> of(T... ts) {
|
||||
if (ArrayUtil.isEmpty(ts)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return Collections.unmodifiableList(toList(ts));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建一个CopyOnWriteArrayList
|
||||
*
|
||||
@ -474,19 +491,4 @@ public class ListUtil {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,12 @@ import cn.hutool.core.lang.Chain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.BitSet;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 比较器链。此链包装了多个比较器,最终比较结果按照比较器顺序综合多个比较器结果。<br>
|
||||
@ -22,13 +22,85 @@ import java.util.Arrays;
|
||||
public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<E>>, Comparator<E>, Serializable {
|
||||
private static final long serialVersionUID = -2426725788913962429L;
|
||||
|
||||
/** 比较器链. */
|
||||
/**
|
||||
* 比较器链.
|
||||
*/
|
||||
private final List<Comparator<E>> chain;
|
||||
/** 对应比较器位置是否反序. */
|
||||
/**
|
||||
* 对应比较器位置是否反序.
|
||||
*/
|
||||
private final BitSet orderingBits;
|
||||
/** 比较器是否被锁定。锁定的比较器链不能再添加新的比较器。比较器会在开始比较时开始加锁。 */
|
||||
/**
|
||||
* 比较器是否被锁定。锁定的比较器链不能再添加新的比较器。比较器会在开始比较时开始加锁。
|
||||
*/
|
||||
private boolean lock = false;
|
||||
|
||||
//------------------------------------------------------------------------------------- Static method start
|
||||
|
||||
/**
|
||||
* 构建 {@link ComparatorChain}
|
||||
*
|
||||
* @param <E> 被比较对象类型
|
||||
* @param comparator 比较器
|
||||
* @return {@link ComparatorChain}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <E> ComparatorChain<E> of(Comparator<E> comparator) {
|
||||
return of(comparator, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 {@link ComparatorChain}
|
||||
*
|
||||
* @param <E> 被比较对象类型
|
||||
* @param comparator 比较器
|
||||
* @param reverse 是否反向
|
||||
* @return {@link ComparatorChain}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <E> ComparatorChain<E> of(Comparator<E> comparator, boolean reverse) {
|
||||
return new ComparatorChain<>(comparator, reverse);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 {@link ComparatorChain}
|
||||
*
|
||||
* @param <E> 被比较对象类型
|
||||
* @param comparators 比较器数组
|
||||
* @return {@link ComparatorChain}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
@SafeVarargs
|
||||
public static <E> ComparatorChain<E> of(Comparator<E>... comparators) {
|
||||
return of(Arrays.asList(comparators));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 {@link ComparatorChain}
|
||||
*
|
||||
* @param <E> 被比较对象类型
|
||||
* @param comparators 比较器列表
|
||||
* @return {@link ComparatorChain}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <E> ComparatorChain<E> of(List<Comparator<E>> comparators) {
|
||||
return new ComparatorChain<>(comparators);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 {@link ComparatorChain}
|
||||
*
|
||||
* @param <E> 被比较对象类型
|
||||
* @param comparators 比较器列表
|
||||
* @param bits {@link Comparator} 列表对应的排序boolean值,true表示正序,false反序
|
||||
* @return {@link ComparatorChain}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <E> ComparatorChain<E> of(List<Comparator<E>> comparators, BitSet bits) {
|
||||
return new ComparatorChain<>(comparators, bits);
|
||||
}
|
||||
//------------------------------------------------------------------------------------- Static method start
|
||||
|
||||
/**
|
||||
* 构造空的比较器链,必须至少有一个比较器,否则会在compare时抛出{@link UnsupportedOperationException}
|
||||
*/
|
||||
@ -82,27 +154,6 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
|
||||
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);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
/**
|
||||
* 在链的尾部添加比较器,使用正向排序
|
||||
*
|
||||
@ -136,7 +187,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
|
||||
* @param index 位置
|
||||
* @param comparator {@link Comparator}
|
||||
* @return this
|
||||
* @exception IndexOutOfBoundsException if index < 0 or index >= size()
|
||||
* @throws IndexOutOfBoundsException if index < 0 or index >= size()
|
||||
*/
|
||||
public ComparatorChain<E> setComparator(final int index, final Comparator<E> comparator) throws IndexOutOfBoundsException {
|
||||
return setComparator(index, comparator, false);
|
||||
@ -197,6 +248,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
|
||||
|
||||
/**
|
||||
* 是否已经被锁定。当开始比较时(调用compare方法)此值为true
|
||||
*
|
||||
* @return true = ComparatorChain cannot be modified; false = ComparatorChain can still be modified.
|
||||
*/
|
||||
public boolean isLocked() {
|
||||
@ -278,6 +330,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------------------- Private method start
|
||||
|
||||
/**
|
||||
* 被锁定时抛出异常
|
||||
*
|
||||
|
@ -1,8 +1,6 @@
|
||||
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;
|
||||
|
||||
@ -116,10 +114,10 @@ public class CompareUtil {
|
||||
/**
|
||||
* 中文比较器
|
||||
*
|
||||
* @param keyExtractor 从对象中提取中文
|
||||
* @param keyExtractor 从对象中提取中文(参与比较的内容)
|
||||
* @param <T> 对象类型
|
||||
*
|
||||
* @return 中文比较器
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <T> Comparator<T> comparingPinyin(Function<T, String> keyExtractor) {
|
||||
return comparingPinyin(keyExtractor, false);
|
||||
@ -128,11 +126,11 @@ public class CompareUtil {
|
||||
/**
|
||||
* 中文比较器
|
||||
*
|
||||
* @param keyExtractor 从对象中提取中文
|
||||
* @param keyExtractor 从对象中提取中文(参与比较的内容)
|
||||
* @param reverse 是否反序
|
||||
* @param <T> 对象类型
|
||||
*
|
||||
* @return 中文比较器
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <T> Comparator<T> comparingPinyin(Function<T, String> keyExtractor, boolean reverse) {
|
||||
Objects.requireNonNull(keyExtractor);
|
||||
|
@ -392,8 +392,13 @@ public class ImgUtil {
|
||||
int srcWidth = srcImage.getWidth(null); // 源图宽度
|
||||
int srcHeight = srcImage.getHeight(null); // 源图高度
|
||||
|
||||
try {
|
||||
if (srcWidth > destWidth && srcHeight > destHeight) {
|
||||
if(srcWidth < destWidth){
|
||||
destWidth = srcWidth;
|
||||
}
|
||||
if(srcHeight < destHeight){
|
||||
destHeight = srcHeight;
|
||||
}
|
||||
|
||||
int cols; // 切片横向数量
|
||||
int rows; // 切片纵向数量
|
||||
// 计算切片的横向和纵向数量
|
||||
@ -415,14 +420,10 @@ public class ImgUtil {
|
||||
// 即: CropImageFilter(int x,int y,int width,int height)
|
||||
tag = cut(srcImage, new Rectangle(j * destWidth, i * destHeight, destWidth, destHeight));
|
||||
// 输出为文件
|
||||
ImageIO.write(toRenderedImage(tag), IMAGE_TYPE_JPEG, new File(descDir, "_r" + i + "_c" + j + ".jpg"));
|
||||
write(tag, FileUtil.file(descDir, "_r" + i + "_c" + j + ".jpg"));
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IORuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 图像切割(指定切片的行数和列数)
|
||||
@ -463,9 +464,9 @@ public class ImgUtil {
|
||||
cols = 2; // 切片列数
|
||||
}
|
||||
// 读取源图像
|
||||
final Image bi = toBufferedImage(srcImage);
|
||||
int srcWidth = bi.getWidth(null); // 源图宽度
|
||||
int srcHeight = bi.getHeight(null); // 源图高度
|
||||
final BufferedImage bi = toBufferedImage(srcImage);
|
||||
int srcWidth = bi.getWidth(); // 源图宽度
|
||||
int srcHeight = bi.getHeight(); // 源图高度
|
||||
|
||||
int destWidth = NumberUtil.partValue(srcWidth, cols); // 每张切片的宽度
|
||||
int destHeight = NumberUtil.partValue(srcHeight, rows); // 每张切片的高度
|
||||
|
@ -8,10 +8,9 @@ import java.util.Objects;
|
||||
/**
|
||||
* 键值对对象,只能在构造时传入键值
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @author looly
|
||||
* @since 4.1.5
|
||||
*/
|
||||
public class Pair<K, V> extends CloneSupport<Pair<K, V>> implements Serializable {
|
||||
@ -20,6 +19,20 @@ public class Pair<K, V> extends CloneSupport<Pair<K, V>> implements Serializable
|
||||
private final K key;
|
||||
private final V value;
|
||||
|
||||
/**
|
||||
* 构建{@link Pair}对象
|
||||
*
|
||||
* @param <K> 键类型
|
||||
* @param <V> 值类型
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return {@link Pair}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <K, V> Pair<K, V> of(K key, V value) {
|
||||
return new Pair<>(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
@ -31,12 +44,9 @@ public class Pair<K, V> extends CloneSupport<Pair<K, V>> implements Serializable
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static <K, V> Pair<K, V> of(K key, V value) {
|
||||
return new Pair<>(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取键
|
||||
*
|
||||
* @return 键
|
||||
*/
|
||||
public K getKey() {
|
||||
@ -45,6 +55,7 @@ public class Pair<K, V> extends CloneSupport<Pair<K, V>> implements Serializable
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
*
|
||||
* @return 值
|
||||
*/
|
||||
public V getValue() {
|
||||
|
@ -4,8 +4,6 @@ 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 {
|
||||
@ -21,8 +19,7 @@ public class CompareUtilTest {
|
||||
|
||||
@Test
|
||||
public void comparingPinyin() {
|
||||
List<String> list = new ArrayList<>();
|
||||
Collections.addAll(list, "成都", "北京", "上海", "深圳");
|
||||
List<String> list = ListUtil.toList("成都", "北京", "上海", "深圳");
|
||||
|
||||
List<String> ascendingOrderResult = ListUtil.of("北京", "成都", "上海", "深圳");
|
||||
List<String> descendingOrderResult = ListUtil.of("深圳", "上海", "成都", "北京");
|
||||
|
@ -84,7 +84,7 @@ public class ImgUtilTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void sliceByRowsAndColsTest() {
|
||||
ImgUtil.sliceByRowsAndCols(FileUtil.file("e:/pic/1.png"), FileUtil.file("e:/pic/dest"), 10, 10);
|
||||
ImgUtil.sliceByRowsAndCols(FileUtil.file("d:/test/logo.jpg"), FileUtil.file("d:/test/dest"), 1, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
x
Reference in New Issue
Block a user