This commit is contained in:
Looly 2020-09-10 11:39:20 +08:00
parent dfa0191b36
commit 76c6aeeb56
8 changed files with 176 additions and 112 deletions

View File

@ -3,9 +3,11 @@
-------------------------------------------------------------------------------------------------------------
# 5.4.3 (2020-09-09)
# 5.4.3 (2020-09-10)
### 新特性
* 【core 】 使用静态的of方法来new对象pr#177@Gitee
### Bug修复
-------------------------------------------------------------------------------------------------------------

View File

@ -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);
}
}

View File

@ -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 &lt; 0 or index &gt;= size()
* @throws IndexOutOfBoundsException if index &lt; 0 or index &gt;= 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
/**
* 被锁定时抛出异常
*

View File

@ -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);

View File

@ -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); // 每张切片的高度

View File

@ -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() {

View File

@ -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("深圳", "上海", "成都", "北京");

View File

@ -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