Looly 5 年 前
コミット
76c6aeeb56

+ 3 - 1
CHANGELOG.md

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

+ 18 - 16
hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java

@@ -150,6 +150,23 @@ public class ListUtil {
 	}
 
 	/**
+	 * 数组转为一个不可变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
 	 *
 	 * @param <T>        集合元素类型
@@ -236,7 +253,7 @@ public class ListUtil {
 			}
 		}
 
-		if((pageNo * pageSize) > resultSize){
+		if ((pageNo * pageSize) > resultSize) {
 			// 越界直接返回空
 			return new ArrayList<>(0);
 		}
@@ -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);
-	}
 }

+ 93 - 40
hutool-core/src/main/java/cn/hutool/core/comparator/ComparatorChain.java

@@ -4,31 +4,103 @@ 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>
  * 按照比较器链的顺序分别比较,如果比较出相等则转向下一个比较器,否则直接返回<br>
  * 此类copy from Apache-commons-collections
- * 
+ *
  * @author looly
  * @since 3.0.7
  */
 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}
 	 */
@@ -37,7 +109,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 	}
 
 	/**
-	 *构造,初始化单一比较器。比较器为正序
+	 * 构造,初始化单一比较器。比较器为正序
 	 *
 	 * @param comparator 在比较器链中的第一个比较器
 	 */
@@ -49,7 +121,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 	 * 构造,初始化单一比较器。自定义正序还是反序
 	 *
 	 * @param comparator 在比较器链中的第一个比较器
-	 * @param reverse 是否反序,true表示反序,false正序
+	 * @param reverse    是否反序,true表示反序,false正序
 	 */
 	public ComparatorChain(final Comparator<E> comparator, final boolean reverse) {
 		chain = new ArrayList<>(1);
@@ -62,9 +134,9 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 
 	/**
 	 * 构造,使用已有的比较器列表
-	 * 
+	 *
 	 * @param list 比较器列表
-	 * @see #ComparatorChain(List,BitSet)
+	 * @see #ComparatorChain(List, BitSet)
 	 */
 	public ComparatorChain(final List<Comparator<E>> list) {
 		this(list, new BitSet(list.size()));
@@ -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);
-	}
-
-	// -----------------------------------------------------------------------
 	/**
 	 * 在链的尾部添加比较器,使用正向排序
 	 *
@@ -117,7 +168,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 	 * 在链的尾部添加比较器,使用给定排序方式
 	 *
 	 * @param comparator {@link Comparator} 比较器
-	 * @param reverse 是否反序,true表示正序,false反序
+	 * @param reverse    是否反序,true表示正序,false反序
 	 * @return this
 	 */
 	public ComparatorChain<E> addComparator(final Comparator<E> comparator, final boolean reverse) {
@@ -133,10 +184,10 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 	/**
 	 * 替换指定位置的比较器,保持原排序方式
 	 *
-	 * @param index 位置
+	 * @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);
@@ -145,9 +196,9 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 	/**
 	 * 替换指定位置的比较器,替换指定排序方式
 	 *
-	 * @param index 位置
+	 * @param index      位置
 	 * @param comparator {@link Comparator}
-	 * @param reverse 是否反序,true表示正序,false反序
+	 * @param reverse    是否反序,true表示正序,false反序
 	 * @return this
 	 */
 	public ComparatorChain<E> setComparator(final int index, final Comparator<E> comparator, final boolean reverse) {
@@ -197,12 +248,13 @@ 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() {
 		return lock;
 	}
-	
+
 	@Override
 	public Iterator<Comparator<E>> iterator() {
 		return this.chain.iterator();
@@ -212,7 +264,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 	public ComparatorChain<E> addChain(Comparator<E> element) {
 		return this.addComparator(element);
 	}
-	
+
 	/**
 	 * 执行比较<br>
 	 * 按照比较器链的顺序分别比较,如果比较出相等则转向下一个比较器,否则直接返回
@@ -228,7 +280,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 			checkChainIntegrity();
 			lock = true;
 		}
-		
+
 		final Iterator<Comparator<E>> comparators = chain.iterator();
 		Comparator<? super E> comparator;
 		int retval;
@@ -278,6 +330,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 	}
 
 	//------------------------------------------------------------------------------------------------------------------------------- Private method start
+
 	/**
 	 * 被锁定时抛出异常
 	 *
@@ -291,7 +344,7 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
 
 	/**
 	 * 检查比较器链是否为空,为空抛出异常
-	 * 
+	 *
 	 * @throws UnsupportedOperationException 为空抛出此异常
 	 */
 	private void checkChainIntegrity() {

+ 7 - 9
hutool-core/src/main/java/cn/hutool/core/comparator/CompareUtil.java

@@ -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 <T> 对象类型
-	 *
+	 * @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 reverse 是否反序
-	 * @param <T> 对象类型
-	 *
+	 * @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);

+ 32 - 31
hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java

@@ -392,35 +392,36 @@ public class ImgUtil {
 		int srcWidth = srcImage.getWidth(null); // 源图宽度
 		int srcHeight = srcImage.getHeight(null); // 源图高度
 
-		try {
-			if (srcWidth > destWidth && srcHeight > destHeight) {
-				int cols; // 切片横向数量
-				int rows; // 切片纵向数量
-				// 计算切片的横向和纵向数量
-				if (srcWidth % destWidth == 0) {
-					cols = srcWidth / destWidth;
-				} else {
-					cols = (int) Math.floor((double) srcWidth / destWidth) + 1;
-				}
-				if (srcHeight % destHeight == 0) {
-					rows = srcHeight / destHeight;
-				} else {
-					rows = (int) Math.floor((double) srcHeight / destHeight) + 1;
-				}
-				// 循环建立切片
-				Image tag;
-				for (int i = 0; i < rows; i++) {
-					for (int j = 0; j < cols; j++) {
-						// 四个参数分别为图像起点坐标和宽高
-						// 即: 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"));
-					}
-				}
+		if(srcWidth < destWidth){
+			destWidth = srcWidth;
+		}
+		if(srcHeight < destHeight){
+			destHeight = srcHeight;
+		}
+
+		int cols; // 切片横向数量
+		int rows; // 切片纵向数量
+		// 计算切片的横向和纵向数量
+		if (srcWidth % destWidth == 0) {
+			cols = srcWidth / destWidth;
+		} else {
+			cols = (int) Math.floor((double) srcWidth / destWidth) + 1;
+		}
+		if (srcHeight % destHeight == 0) {
+			rows = srcHeight / destHeight;
+		} else {
+			rows = (int) Math.floor((double) srcHeight / destHeight) + 1;
+		}
+		// 循环建立切片
+		Image tag;
+		for (int i = 0; i < rows; i++) {
+			for (int j = 0; j < cols; j++) {
+				// 四个参数分别为图像起点坐标和宽高
+				// 即: CropImageFilter(int x,int y,int width,int height)
+				tag = cut(srcImage, new Rectangle(j * destWidth, i * destHeight, destWidth, destHeight));
+				// 输出为文件
+				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); // 每张切片的高度

+ 21 - 10
hutool-core/src/main/java/cn/hutool/core/lang/Pair.java

@@ -7,23 +7,36 @@ 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{
+public class Pair<K, V> extends CloneSupport<Pair<K, V>> implements Serializable {
 	private static final long serialVersionUID = 1L;
-	
+
 	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);
+	}
+
+	/**
 	 * 构造
-	 * 
-	 * @param key 键
+	 *
+	 * @param key   
 	 * @param value 值
 	 */
 	public Pair(K key, V 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() {

+ 1 - 4
hutool-core/src/test/java/cn/hutool/core/comparator/CompareUtilTest.java

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

+ 1 - 1
hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java

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