From c9064c2334fd50bbea907b76333e214d44fb3d3d Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 30 Apr 2022 10:46:17 +0800 Subject: [PATCH 01/11] ad test --- .../test/java/cn/hutool/core/date/DateUtilTest.java | 11 +++++++++++ .../hutool/poi/excel/cell/setters/package-info.java | 6 ++++++ 2 files changed, 17 insertions(+) create mode 100644 hutool-poi/src/main/java/cn/hutool/poi/excel/cell/setters/package-info.java diff --git a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java index bdabd60d8..3984bf599 100755 --- a/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java @@ -1064,6 +1064,17 @@ public class DateUtilTest { Assert.assertFalse(DateUtil.isOverlap(startTime,endTime,realStartTime1,realEndTime1)); } + @Test + public void isOverlapTest2() { + DateTime oneStartTime = DateUtil.parseDate("2021-02-01"); + DateTime oneEndTime = DateUtil.parseDate("2022-06-30"); + + DateTime oneStartTime2 = DateUtil.parseDate("2019-04-05"); + DateTime oneEndTime2 = DateUtil.parseDate("2021-04-05"); + + Assert.assertTrue(DateUtil.isOverlap(oneStartTime, oneEndTime, oneStartTime2, oneEndTime2)); + } + @Test public void isInTest(){ String sourceStr = "2022-04-19 00:00:00"; diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/setters/package-info.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/setters/package-info.java new file mode 100644 index 000000000..b0d905b77 --- /dev/null +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/setters/package-info.java @@ -0,0 +1,6 @@ +/** + * Excel中单元格设置相关类,一些{@link cn.hutool.poi.excel.cell.CellSetter}的实现类 + * + * @author looly + */ +package cn.hutool.poi.excel.cell.setters; From 8b98cb3e90ae7d5b5ef57ea0ce555466e0c66bda Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 30 Apr 2022 11:40:20 +0800 Subject: [PATCH 02/11] add test --- .../test/java/cn/hutool/core/net/RFC3986Test.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hutool-core/src/test/java/cn/hutool/core/net/RFC3986Test.java b/hutool-core/src/test/java/cn/hutool/core/net/RFC3986Test.java index 781b78001..e549e9a9f 100644 --- a/hutool-core/src/test/java/cn/hutool/core/net/RFC3986Test.java +++ b/hutool-core/src/test/java/cn/hutool/core/net/RFC3986Test.java @@ -1,9 +1,13 @@ package cn.hutool.core.net; +import cn.hutool.core.codec.PercentCodec; import cn.hutool.core.util.CharsetUtil; import org.junit.Assert; import org.junit.Test; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + public class RFC3986Test { @Test @@ -26,4 +30,12 @@ public class RFC3986Test { String encode = RFC3986.QUERY_PARAM_VALUE.encode("a=%25", CharsetUtil.CHARSET_UTF_8, '%'); Assert.assertEquals("a=%25", encode); } + + @Test + public void encodeAllTest() throws UnsupportedEncodingException { + String toVerifyText = "行吧行吧 cargo:1.0,\"Deta-ils:[{"; + final String encode = PercentCodec.of(RFC3986.UNRESERVED).setEncodeSpaceAsPlus(true).encode(toVerifyText, CharsetUtil.CHARSET_UTF_8); + final String encodeJdk = URLEncoder.encode(toVerifyText, "UTF-8"); + Assert.assertEquals(encode, encodeJdk); + } } From 2c00b09ce8e02ddd697b2ff39add13691f2ff048 Mon Sep 17 00:00:00 2001 From: Husky <2466896229@qq.com> Date: Sat, 30 Apr 2022 18:32:00 +0800 Subject: [PATCH 03/11] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=B3=A8=E9=87=8A=202=E3=80=81=E5=A2=9E=E5=8A=A0isMer?= =?UTF-8?q?gedRegion=E7=A9=BA=E6=8C=87=E9=92=88=E5=88=A4=E6=96=AD=203?= =?UTF-8?q?=E3=80=81=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96CellRangeAddress?= =?UTF-8?q?=E6=96=B9=E6=B3=95=204=E3=80=81=E5=A2=9E=E5=8A=A0=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=90=88=E5=B9=B6=E5=8D=95=E5=85=83=E6=A0=BC=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E6=96=B9=E6=B3=95(=E7=9B=AE=E5=89=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=85=88=E5=90=88=E5=B9=B6=E5=86=8D=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F,=E4=BD=86=E6=98=AF=E6=9C=89=E7=A7=8D?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=BA=E8=AF=A5=E5=8D=95=E5=85=83=E6=A0=BC?= =?UTF-8?q?=E6=9C=AC=E6=9D=A5=E5=B0=B1=E6=98=AF=E5=90=88=E5=B9=B6=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=A0=BC,=E5=8F=AA=E9=9C=80=E8=A6=81=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=A0=B7=E5=BC=8F)=205=E3=80=81=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=A0=B9=E6=8D=AEcellStyle=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=A0=BC=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/poi/excel/cell/CellUtil.java | 109 +++++++++++++++--- 1 file changed, 90 insertions(+), 19 deletions(-) diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java index f4747bae3..1c7b59405 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java @@ -199,7 +199,7 @@ public class CellUtil { * * @param row Excel表的行 * @param cellIndex 列号 - * @return {@link Row} + * @return {@link Cell} * @since 5.5.0 */ public static Cell getCell(Row row, int cellIndex) { @@ -218,7 +218,7 @@ public class CellUtil { * * @param row Excel表的行 * @param cellIndex 列号 - * @return {@link Row} + * @return {@link Cell} * @since 4.0.2 */ public static Cell getOrCreateCell(Row row, int cellIndex) { @@ -265,18 +265,78 @@ public class CellUtil { * @return 是否是合并单元格 */ public static boolean isMergedRegion(Sheet sheet, int x, int y) { - final int sheetMergeCount = sheet.getNumMergedRegions(); - CellRangeAddress ca; - for (int i = 0; i < sheetMergeCount; i++) { - ca = sheet.getMergedRegion(i); - if (y >= ca.getFirstRow() && y <= ca.getLastRow() - && x >= ca.getFirstColumn() && x <= ca.getLastColumn()) { - return true; + if (sheet != null) { + final int sheetMergeCount = sheet.getNumMergedRegions(); + CellRangeAddress ca; + for (int i = 0; i < sheetMergeCount; i++) { + ca = sheet.getMergedRegion(i); + if (y >= ca.getFirstRow() && y <= ca.getLastRow() + && x >= ca.getFirstColumn() && x <= ca.getLastColumn()) { + return true; + } } } return false; } + /** + * 获取合并单元格{@link CellRangeAddress},如果不是返回null + * + * @param sheet {@link Sheet} + * @param locationRef 单元格地址标识符,例如A11,B5 + * @return {@link CellRangeAddress} + */ + public static CellRangeAddress getCellRangeAddress(Sheet sheet, String locationRef) { + final CellLocation cellLocation = ExcelUtil.toLocation(locationRef); + return getCellRangeAddress(sheet, cellLocation.getX(), cellLocation.getY()); + } + + /** + * 获取合并单元格{@link CellRangeAddress},如果不是返回null + * + * @param cell {@link Cell} + * @return {@link CellRangeAddress} + */ + public static CellRangeAddress getCellRangeAddress(Cell cell) { + return getCellRangeAddress(cell.getSheet(), cell.getColumnIndex(), cell.getRowIndex()); + } + + /** + * 获取合并单元格{@link CellRangeAddress},如果不是返回null + * + * @param sheet {@link Sheet} + * @param x 列号,从0开始 + * @param y 行号,从0开始 + * @return {@link CellRangeAddress} + */ + public static CellRangeAddress getCellRangeAddress(Sheet sheet, int x, int y) { + if (sheet != null) { + final int sheetMergeCount = sheet.getNumMergedRegions(); + CellRangeAddress ca; + for (int i = 0; i < sheetMergeCount; i++) { + ca = sheet.getMergedRegion(i); + if (y >= ca.getFirstRow() && y <= ca.getLastRow() + && x >= ca.getFirstColumn() && x <= ca.getLastColumn()) { + return ca; + } + } + } + return null; + } + + /** + * 设置合并单元格样式,如果不是则不设置 + * + * @param cell {@link Cell} + * @param cellStyle {@link CellStyle} + */ + public static void setMergedRegionStyle(Cell cell, CellStyle cellStyle) { + CellRangeAddress cellRangeAddress = getCellRangeAddress(cell); + if (cellRangeAddress != null) { + setMergeCellStyle(cellStyle, cellRangeAddress, cell.getSheet()); + } + } + /** * 合并单元格,可以根据设置的值来合并行和列 * @@ -310,16 +370,7 @@ public class CellUtil { lastColumn // last column (0-based) ); - if (null != cellStyle) { - RegionUtil.setBorderTop(cellStyle.getBorderTop(), cellRangeAddress, sheet); - RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet); - RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet); - RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet); - RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(),cellRangeAddress,sheet); - RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(),cellRangeAddress,sheet); - RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(),cellRangeAddress,sheet); - RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(),cellRangeAddress,sheet); - } + setMergeCellStyle(cellStyle, cellRangeAddress, sheet); return sheet.addMergedRegion(cellRangeAddress); } @@ -435,5 +486,25 @@ public class CellUtil { } return null; } + + /** + * 根据{@link CellStyle}设置合并单元格边框样式 + * + * @param cellStyle {@link CellStyle} + * @param cellRangeAddress {@link CellRangeAddress} + * @param sheet {@link Sheet} + */ + private static void setMergeCellStyle(CellStyle cellStyle, CellRangeAddress cellRangeAddress, Sheet sheet) { + if (null != cellStyle) { + RegionUtil.setBorderTop(cellStyle.getBorderTop(), cellRangeAddress, sheet); + RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet); + RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet); + RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet); + RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(), cellRangeAddress, sheet); + RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(), cellRangeAddress, sheet); + RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(), cellRangeAddress, sheet); + RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(), cellRangeAddress, sheet); + } + } // -------------------------------------------------------------------------------------------------------------- Private method end } From c3c4c87bea33229e3c6dcce9cbfa8a2322637580 Mon Sep 17 00:00:00 2001 From: mo_chou_555 <7496450+mo_chou_555@user.noreply.gitee.com> Date: Tue, 3 May 2022 12:24:41 +0800 Subject: [PATCH 04/11] test --- Test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Test.txt diff --git a/Test.txt b/Test.txt new file mode 100644 index 000000000..e69de29bb From 50d77194c80a20d0d22c7a6d4510cbca1b08326d Mon Sep 17 00:00:00 2001 From: mo_chou_555 <7496450+mo_chou_555@user.noreply.gitee.com> Date: Tue, 3 May 2022 12:25:58 +0800 Subject: [PATCH 05/11] remove test --- Test.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Test.txt diff --git a/Test.txt b/Test.txt deleted file mode 100644 index e69de29bb..000000000 From 2a9eaa6a7fcd9b304eaea4763843c89d072189b2 Mon Sep 17 00:00:00 2001 From: mo_chou_555 <7496450+mo_chou_555@user.noreply.gitee.com> Date: Tue, 3 May 2022 12:31:55 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Assert=E7=9A=84equals?= =?UTF-8?q?=E5=8F=8AnotEquals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/lang/Assert.java | 94 +++++++++++++++++++ .../java/cn/hutool/core/lang/AssertTest.java | 29 +++++- 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java index a33b2a4ed..b0b347c02 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java @@ -3,6 +3,7 @@ package cn.hutool.core.lang; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import java.util.Map; @@ -1003,6 +1004,99 @@ public class Assert { return value; } + /** + * 断言两个对象是否不相等,如果两个对象相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.notEquals(obj1,obj2);
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @throws IllegalArgumentException obj1 must be not equals obj2 + */ + public static void notEquals(Object obj1, Object obj2) { + notEquals(obj1, obj2, "({}) must be not equals ({})", obj1, obj2); + } + + /** + * 断言两个对象是否不相等,如果两个对象相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.notEquals(obj1,obj2,"obj1 must be not equals obj2");
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorMsgTemplate 异常信息模板,类似于"aa{}bb{}cc" + * @param params 异常信息参数,用于替换"{}"占位符 + * @throws IllegalArgumentException obj1 must be not equals obj2 + */ + public static void notEquals(Object obj1, Object obj2, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + notEquals(obj1, obj2, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言两个对象是否不相等,如果两个对象相等,抛出指定类型异常,并使用指定的函数获取错误信息返回 + * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorSupplier 错误抛出异常附带的消息生产接口 + * @param 异常类型 + * @throws X obj1 must be not equals obj2 + */ + public static void notEquals(Object obj1, Object obj2, Supplier errorSupplier) throws X { + if (ObjectUtil.equals(obj1, obj2)) { + throw errorSupplier.get(); + } + } + // ----------------------------------------------------------------------------------------------------------- Check not equals + + /** + * 断言两个对象是否相等,如果两个对象不相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.isEquals(obj1,obj2);
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @throws IllegalArgumentException obj1 must be equals obj2 + */ + public static void isEquals(Object obj1, Object obj2) { + isEquals(obj1, obj2, "({}) must be equals ({})", obj1, obj2); + } + + /** + * 断言两个对象是否相等,如果两个对象不相等 抛出IllegalArgumentException 异常 + *
+	 *   Assert.isEquals(obj1,obj2,"obj1 must be equals obj2");
+	 * 
+ * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorMsgTemplate 异常信息模板,类似于"aa{}bb{}cc" + * @param params 异常信息参数,用于替换"{}"占位符 + * @throws IllegalArgumentException obj1 must be equals obj2 + */ + public static void isEquals(Object obj1, Object obj2, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + isEquals(obj1, obj2, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + } + + /** + * 断言两个对象是否相等,如果两个对象不相等,抛出指定类型异常,并使用指定的函数获取错误信息返回 + * + * @param obj1 对象1 + * @param obj2 对象2 + * @param errorSupplier 错误抛出异常附带的消息生产接口 + * @param 异常类型 + * @throws X obj1 must be equals obj2 + */ + public static void isEquals(Object obj1, Object obj2, Supplier errorSupplier) throws X { + if (ObjectUtil.notEqual(obj1, obj2)) { + throw errorSupplier.get(); + } + } + + // ----------------------------------------------------------------------------------------------------------- Check is equals + // -------------------------------------------------------------------------------------------------------------------------------------------- Private method start /** diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java index e20ef6e96..be095d835 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java @@ -1,9 +1,10 @@ package cn.hutool.core.lang; +import cn.hutool.core.util.StrUtil; import org.junit.Test; public class AssertTest { - + @Test public void isNullTest(){ String a = null; @@ -33,6 +34,30 @@ public class AssertTest { public void isTrueTest3() { int i = -1; //noinspection ConstantConditions - Assert.isTrue(i > 0, ()-> new IndexOutOfBoundsException("relation message to return")); + Assert.isTrue(i > 0, () -> new IndexOutOfBoundsException("relation message to return")); } + + @Test + public void isEqualsTest() { + //String a="ab"; + //final String b = new String("abc"); + String a = null; + final String b = null; + Assert.isEquals(a, b); + Assert.isEquals(a, b, "{}不等于{}", a, b); + Assert.isEquals(a, b, () -> new RuntimeException(StrUtil.format("{}和{}不相等", a, b))); + } + + @Test + public void notEqualsTest() { + //String c="19"; + //final String d = new String("19"); + String c = null; + final String d = null; + //Assert.notEquals(c,d); + //Assert.notEquals(c,d,"{}等于{}",c,d); + Assert.notEquals(c, d, () -> new RuntimeException(StrUtil.format("{}和{}相等", c, d))); + + } + } From 56b0c907e92a0a54c2318045226a50b5a71644e9 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 5 May 2022 09:20:02 +0800 Subject: [PATCH 07/11] add methods --- CHANGELOG.md | 3 ++- .../src/main/java/cn/hutool/core/lang/Assert.java | 10 +++++----- .../src/test/java/cn/hutool/core/lang/AssertTest.java | 10 +++++----- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7917c07f..0e0fd1c54 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.0.M5 (2022-04-28) +# 5.8.0.M5 (2022-05-05) ### ❌不兼容特性 * 【extra 】 升级jakarta.validation-api到3.x,包名变更导致不能向下兼容 @@ -12,6 +12,7 @@ ### 🐣新特性 * 【core 】 Singleton增加部分方法(pr#609@Gitee) * 【core 】 BeanUtil增加beanToMap重载(pr#2292@Github) +* 【core 】 Assert增加对应的equals及notEquals方法(pr#612@Gitee) ### 🐞Bug修复 * 【db 】 修复RedisDS无法设置maxWaitMillis问题(issue#I54TZ9@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java index b0b347c02..ec0b69b27 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java @@ -1060,8 +1060,8 @@ public class Assert { * @param obj2 对象2 * @throws IllegalArgumentException obj1 must be equals obj2 */ - public static void isEquals(Object obj1, Object obj2) { - isEquals(obj1, obj2, "({}) must be equals ({})", obj1, obj2); + public static void equals(Object obj1, Object obj2) { + equals(obj1, obj2, "({}) must be equals ({})", obj1, obj2); } /** @@ -1076,8 +1076,8 @@ public class Assert { * @param params 异常信息参数,用于替换"{}"占位符 * @throws IllegalArgumentException obj1 must be equals obj2 */ - public static void isEquals(Object obj1, Object obj2, String errorMsgTemplate, Object... params) throws IllegalArgumentException { - isEquals(obj1, obj2, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + public static void equals(Object obj1, Object obj2, String errorMsgTemplate, Object... params) throws IllegalArgumentException { + equals(obj1, obj2, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); } /** @@ -1089,7 +1089,7 @@ public class Assert { * @param 异常类型 * @throws X obj1 must be equals obj2 */ - public static void isEquals(Object obj1, Object obj2, Supplier errorSupplier) throws X { + public static void equals(Object obj1, Object obj2, Supplier errorSupplier) throws X { if (ObjectUtil.notEqual(obj1, obj2)) { throw errorSupplier.get(); } diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java index be095d835..7ddde8d47 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java @@ -38,14 +38,14 @@ public class AssertTest { } @Test - public void isEqualsTest() { + public void equalsTest() { //String a="ab"; //final String b = new String("abc"); String a = null; final String b = null; - Assert.isEquals(a, b); - Assert.isEquals(a, b, "{}不等于{}", a, b); - Assert.isEquals(a, b, () -> new RuntimeException(StrUtil.format("{}和{}不相等", a, b))); + Assert.equals(a, b); + Assert.equals(a, b, "{}不等于{}", a, b); + Assert.equals(a, b, () -> new RuntimeException(StrUtil.format("{}和{}不相等", a, b))); } @Test @@ -53,7 +53,7 @@ public class AssertTest { //String c="19"; //final String d = new String("19"); String c = null; - final String d = null; + final String d = "null"; //Assert.notEquals(c,d); //Assert.notEquals(c,d,"{}等于{}",c,d); Assert.notEquals(c, d, () -> new RuntimeException(StrUtil.format("{}和{}相等", c, d))); From e05a3b19e0faa3f048eaf217948a8b7be2dd0586 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 5 May 2022 11:16:39 +0800 Subject: [PATCH 08/11] add methods --- CHANGELOG.md | 1 + .../java/cn/hutool/core/util/ObjectUtil.java | 16 ++++++++-------- .../java/cn/hutool/poi/excel/cell/CellUtil.java | 13 ++++++++----- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e0fd1c54..61c13f66d 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * 【core 】 Singleton增加部分方法(pr#609@Gitee) * 【core 】 BeanUtil增加beanToMap重载(pr#2292@Github) * 【core 】 Assert增加对应的equals及notEquals方法(pr#612@Gitee) +* 【core 】 Assert增加对应的equals及notEquals方法(pr#612@Gitee) ### 🐞Bug修复 * 【db 】 修复RedisDS无法设置maxWaitMillis问题(issue#I54TZ9@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java index 8fe082b43..00c3454de 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ObjectUtil.java @@ -104,7 +104,7 @@ public class ObjectUtil { int count; if (obj instanceof Iterator) { - Iterator iter = (Iterator) obj; + final Iterator iter = (Iterator) obj; count = 0; while (iter.hasNext()) { count++; @@ -113,7 +113,7 @@ public class ObjectUtil { return count; } if (obj instanceof Enumeration) { - Enumeration enumeration = (Enumeration) obj; + final Enumeration enumeration = (Enumeration) obj; count = 0; while (enumeration.hasMoreElements()) { count++; @@ -161,9 +161,9 @@ public class ObjectUtil { } if (obj instanceof Iterator) { - Iterator iter = (Iterator) obj; + final Iterator iter = (Iterator) obj; while (iter.hasNext()) { - Object o = iter.next(); + final Object o = iter.next(); if (equal(o, element)) { return true; } @@ -171,9 +171,9 @@ public class ObjectUtil { return false; } if (obj instanceof Enumeration) { - Enumeration enumeration = (Enumeration) obj; + final Enumeration enumeration = (Enumeration) obj; while (enumeration.hasMoreElements()) { - Object o = enumeration.nextElement(); + final Object o = enumeration.nextElement(); if (equal(o, element)) { return true; } @@ -181,9 +181,9 @@ public class ObjectUtil { return false; } if (obj.getClass().isArray() == true) { - int len = Array.getLength(obj); + final int len = Array.getLength(obj); for (int i = 0; i < len; i++) { - Object o = Array.get(obj, i); + final Object o = Array.get(obj, i); if (equal(o, element)) { return true; } diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java index 1c7b59405..8c4f4d4cf 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/CellUtil.java @@ -105,7 +105,7 @@ public class CellUtil { cellType = cell.getCellType(); } - Object value; + final Object value; switch (cellType) { case NUMERIC: value = new NumericCellValue(cell).getValue(); @@ -206,7 +206,7 @@ public class CellUtil { if (null == row) { return null; } - Cell cell = row.getCell(cellIndex); + final Cell cell = row.getCell(cellIndex); if (null == cell) { return new NullCell(row, cellIndex); } @@ -262,7 +262,7 @@ public class CellUtil { * @param sheet {@link Sheet} * @param x 列号,从0开始 * @param y 行号,从0开始 - * @return 是否是合并单元格 + * @return 是否是合并单元格,如果提供的sheet为{@code null},返回{@code false} */ public static boolean isMergedRegion(Sheet sheet, int x, int y) { if (sheet != null) { @@ -285,6 +285,7 @@ public class CellUtil { * @param sheet {@link Sheet} * @param locationRef 单元格地址标识符,例如A11,B5 * @return {@link CellRangeAddress} + * @since 5.8.0 */ public static CellRangeAddress getCellRangeAddress(Sheet sheet, String locationRef) { final CellLocation cellLocation = ExcelUtil.toLocation(locationRef); @@ -296,6 +297,7 @@ public class CellUtil { * * @param cell {@link Cell} * @return {@link CellRangeAddress} + * @since 5.8.0 */ public static CellRangeAddress getCellRangeAddress(Cell cell) { return getCellRangeAddress(cell.getSheet(), cell.getColumnIndex(), cell.getRowIndex()); @@ -308,6 +310,7 @@ public class CellUtil { * @param x 列号,从0开始 * @param y 行号,从0开始 * @return {@link CellRangeAddress} + * @since 5.8.0 */ public static CellRangeAddress getCellRangeAddress(Sheet sheet, int x, int y) { if (sheet != null) { @@ -331,7 +334,7 @@ public class CellUtil { * @param cellStyle {@link CellStyle} */ public static void setMergedRegionStyle(Cell cell, CellStyle cellStyle) { - CellRangeAddress cellRangeAddress = getCellRangeAddress(cell); + final CellRangeAddress cellRangeAddress = getCellRangeAddress(cell); if (cellRangeAddress != null) { setMergeCellStyle(cellStyle, cellRangeAddress, cell.getSheet()); } @@ -457,7 +460,7 @@ public class CellUtil { anchor.setRow1(cell.getRowIndex()); anchor.setRow2(cell.getRowIndex() + 2); } - Comment comment = drawing.createCellComment(anchor); + final Comment comment = drawing.createCellComment(anchor); comment.setString(factory.createRichTextString(commentText)); comment.setAuthor(StrUtil.nullToEmpty(commentAuthor)); cell.setCellComment(comment); From 34704bd3267e9d3bc10e7e2ed4a5721299726530 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 5 May 2022 11:32:54 +0800 Subject: [PATCH 09/11] add test --- .../main/java/cn/hutool/db/meta/MetaUtil.java | 26 +++++++++---------- .../java/cn/hutool/db/meta/MetaUtilTest.java | 18 ++++++++++--- .../java/cn/hutool/dfa/SensitiveUtil.java | 10 +++---- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/hutool-db/src/main/java/cn/hutool/db/meta/MetaUtil.java b/hutool-db/src/main/java/cn/hutool/db/meta/MetaUtil.java index bb151db76..f1ecafa83 100755 --- a/hutool-db/src/main/java/cn/hutool/db/meta/MetaUtil.java +++ b/hutool-db/src/main/java/cn/hutool/db/meta/MetaUtil.java @@ -82,13 +82,13 @@ public class MetaUtil { conn = ds.getConnection(); // catalog和schema获取失败默认使用null代替 - String catalog = getCatalog(conn); + final String catalog = getCatalog(conn); if (null == schema) { schema = getSchema(conn); } final DatabaseMetaData metaData = conn.getMetaData(); - try (ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) { + try (final ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert.toStrArray(types))) { if (null != rs) { String table; while (rs.next()) { @@ -116,9 +116,9 @@ public class MetaUtil { */ public static String[] getColumnNames(ResultSet rs) throws DbRuntimeException { try { - ResultSetMetaData rsmd = rs.getMetaData(); - int columnCount = rsmd.getColumnCount(); - String[] labelNames = new String[columnCount]; + final ResultSetMetaData rsmd = rs.getMetaData(); + final int columnCount = rsmd.getColumnCount(); + final String[] labelNames = new String[columnCount]; for (int i = 0; i < labelNames.length; i++) { labelNames[i] = rsmd.getColumnLabel(i + 1); } @@ -137,17 +137,17 @@ public class MetaUtil { * @throws DbRuntimeException SQL执行异常 */ public static String[] getColumnNames(DataSource ds, String tableName) { - List columnNames = new ArrayList<>(); + final List columnNames = new ArrayList<>(); Connection conn = null; try { conn = ds.getConnection(); // catalog和schema获取失败默认使用null代替 - String catalog = getCatalog(conn); - String schema = getSchema(conn); + final String catalog = getCatalog(conn); + final String schema = getSchema(conn); final DatabaseMetaData metaData = conn.getMetaData(); - try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) { + try (final ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) { if (null != rs) { while (rs.next()) { columnNames.add(rs.getString("COLUMN_NAME")); @@ -225,7 +225,7 @@ public class MetaUtil { final DatabaseMetaData metaData = conn.getMetaData(); // 获得表元数据(表注释) - try (ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) { + try (final ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[]{TableType.TABLE.value()})) { if (null != rs) { if (rs.next()) { table.setComment(rs.getString("REMARKS")); @@ -234,7 +234,7 @@ public class MetaUtil { } // 获得主键 - try (ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) { + try (final ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) { if (null != rs) { while (rs.next()) { table.addPk(rs.getString("COLUMN_NAME")); @@ -243,7 +243,7 @@ public class MetaUtil { } // 获得列 - try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) { + try (final ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) { if (null != rs) { while (rs.next()) { table.setColumn(Column.create(table, rs)); @@ -252,7 +252,7 @@ public class MetaUtil { } // 获得索引信息(since 5.7.23) - try (ResultSet rs = metaData.getIndexInfo(catalog, schema, tableName, false, false)) { + try (final ResultSet rs = metaData.getIndexInfo(catalog, schema, tableName, false, false)) { final Map indexInfoMap = new LinkedHashMap<>(); if (null != rs) { while (rs.next()) { diff --git a/hutool-db/src/test/java/cn/hutool/db/meta/MetaUtilTest.java b/hutool-db/src/test/java/cn/hutool/db/meta/MetaUtilTest.java index 0abd88676..d9ece6a58 100644 --- a/hutool-db/src/test/java/cn/hutool/db/meta/MetaUtilTest.java +++ b/hutool-db/src/test/java/cn/hutool/db/meta/MetaUtilTest.java @@ -2,6 +2,7 @@ package cn.hutool.db.meta; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.db.DbRuntimeException; import cn.hutool.db.ds.DSFactory; import org.junit.Assert; import org.junit.Test; @@ -20,25 +21,34 @@ public class MetaUtilTest { @Test public void getTablesTest() { - List tables = MetaUtil.getTables(ds); + final List tables = MetaUtil.getTables(ds); Assert.assertEquals("user", tables.get(0)); } @Test public void getTableMetaTest() { - Table table = MetaUtil.getTableMeta(ds, "user"); + final Table table = MetaUtil.getTableMeta(ds, "user"); Assert.assertEquals(CollectionUtil.newHashSet("id"), table.getPkNames()); } @Test public void getColumnNamesTest() { - String[] names = MetaUtil.getColumnNames(ds, "user"); + final String[] names = MetaUtil.getColumnNames(ds, "user"); Assert.assertArrayEquals(StrUtil.splitToArray("id,name,age,birthday,gender", ','), names); } @Test public void getTableIndexInfoTest() { - Table table = MetaUtil.getTableMeta(ds, "user_1"); + final Table table = MetaUtil.getTableMeta(ds, "user_1"); + Assert.assertEquals(table.getIndexInfoList().size(), 2); + } + + /** + * 表不存在抛出异常。 + */ + @Test(expected = DbRuntimeException.class) + public void getTableNotExistTest() { + final Table table = MetaUtil.getTableMeta(ds, "user_not_exist"); Assert.assertEquals(table.getIndexInfoList().size(), 2); } } diff --git a/hutool-dfa/src/main/java/cn/hutool/dfa/SensitiveUtil.java b/hutool-dfa/src/main/java/cn/hutool/dfa/SensitiveUtil.java index bcf81a784..ba8507cff 100755 --- a/hutool-dfa/src/main/java/cn/hutool/dfa/SensitiveUtil.java +++ b/hutool-dfa/src/main/java/cn/hutool/dfa/SensitiveUtil.java @@ -194,7 +194,7 @@ public final class SensitiveUtil { * @return 敏感词过滤处理后的bean对象 */ public static T sensitiveFilter(T bean, boolean isGreedMatch, SensitiveProcessor sensitiveProcessor) { - String jsonText = JSONUtil.toJsonStr(bean); + final String jsonText = JSONUtil.toJsonStr(bean); @SuppressWarnings("unchecked") final Class c = (Class) bean.getClass(); return JSONUtil.toBean(sensitiveFilter(jsonText, isGreedMatch, sensitiveProcessor), c); } @@ -224,7 +224,7 @@ public final class SensitiveUtil { } //敏感词过滤场景下,不需要密集匹配 - List foundWordList = getFoundAllSensitive(text, true, isGreedMatch); + final List foundWordList = getFoundAllSensitive(text, true, isGreedMatch); if (CollUtil.isEmpty(foundWordList)) { return text; } @@ -233,10 +233,10 @@ public final class SensitiveUtil { final Map foundWordMap = new HashMap<>(foundWordList.size(), 1); foundWordList.forEach(foundWord -> foundWordMap.put(foundWord.getStartIndex(), foundWord)); - int length = text.length(); - StringBuilder textStringBuilder = new StringBuilder(); + final int length = text.length(); + final StringBuilder textStringBuilder = new StringBuilder(); for (int i = 0; i < length; i++) { - FoundWord fw = foundWordMap.get(i); + final FoundWord fw = foundWordMap.get(i); if (fw != null) { textStringBuilder.append(sensitiveProcessor.process(fw)); i = fw.getEndIndex(); From fc51570e0350de4fd41b345d621bfdf4e345eb7c Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 5 May 2022 11:33:31 +0800 Subject: [PATCH 10/11] fix code --- .../src/main/java/cn/hutool/core/lang/Opt.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java b/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java index 139741a3e..eddc1476f 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Opt.java @@ -41,7 +41,7 @@ import java.util.stream.Stream; /** * 复制jdk16中的Optional,以及自己进行了一点调整和新增,比jdk8中的Optional多了几个实用的函数
- * 详细见:https://gitee.com/dromara/hutool/pulls/426 + * 详细见:https://gitee.com/dromara/hutool/pulls/426 * * @param 包裹里元素的类型 * @author VampireAchao @@ -60,8 +60,7 @@ public class Opt { * @return Opt */ public static Opt empty() { - @SuppressWarnings("unchecked") - Opt t = (Opt) EMPTY; + @SuppressWarnings("unchecked") final Opt t = (Opt) EMPTY; return t; } @@ -322,7 +321,7 @@ public class Opt { return empty(); } else { @SuppressWarnings("unchecked") - Opt r = (Opt) mapper.apply(value); + final Opt r = (Opt) mapper.apply(value); return Objects.requireNonNull(r); } } @@ -400,8 +399,7 @@ public class Opt { if (isPresent()) { return this; } else { - @SuppressWarnings("unchecked") - Opt r = (Opt) supplier.get(); + @SuppressWarnings("unchecked") final Opt r = (Opt) supplier.get(); return Objects.requireNonNull(r); } } @@ -544,7 +542,7 @@ public class Opt { return false; } - Opt other = (Opt) obj; + final Opt other = (Opt) obj; return Objects.equals(value, other.value); } From 81ef04875bb5d59343cfb772128d79c4e2954416 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 5 May 2022 14:01:09 +0800 Subject: [PATCH 11/11] add methods --- CHANGELOG.md | 1 + .../cn/hutool/crypto/digest/DigestUtil.java | 106 ++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61c13f66d..9275986f4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * 【core 】 BeanUtil增加beanToMap重载(pr#2292@Github) * 【core 】 Assert增加对应的equals及notEquals方法(pr#612@Gitee) * 【core 】 Assert增加对应的equals及notEquals方法(pr#612@Gitee) +* 【core 】 DigestUtil增加sha512方法(issue#2298@Github) ### 🐞Bug修复 * 【db 】 修复RedisDS无法设置maxWaitMillis问题(issue#I54TZ9@Gitee) diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/digest/DigestUtil.java b/hutool-crypto/src/main/java/cn/hutool/crypto/digest/DigestUtil.java index fc6fd61ce..e6c04e305 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/digest/DigestUtil.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/digest/DigestUtil.java @@ -487,4 +487,110 @@ public class DigestUtil { public static boolean bcryptCheck(String password, String hashed) { return BCrypt.checkpw(password, hashed); } + + // ------------------------------------------------------------------------------------------- SHA-512 + + /** + * 计算SHA-512摘要值 + * + * @param data 被摘要数据 + * @return SHA-512摘要 + */ + public static byte[] sha512(final byte[] data) { + return new Digester(DigestAlgorithm.SHA512).digest(data); + } + + /** + * 计算SHA-512摘要值 + * + * @param data 被摘要数据 + * @param charset 编码 + * @return SHA-512摘要 + * @since 3.0.8 + */ + public static byte[] sha512(final String data, final String charset) { + return new Digester(DigestAlgorithm.SHA512).digest(data, charset); + } + + /** + * 计算sha512摘要值,使用UTF-8编码 + * + * @param data 被摘要数据 + * @return MD5摘要 + */ + public static byte[] sha512(final String data) { + return sha512(data, CharsetUtil.UTF_8); + } + + /** + * 计算SHA-512摘要值 + * + * @param data 被摘要数据 + * @return SHA-512摘要 + */ + public static byte[] sha512(final InputStream data) { + return new Digester(DigestAlgorithm.SHA512).digest(data); + } + + /** + * 计算SHA-512摘要值 + * + * @param file 被摘要文件 + * @return SHA-512摘要 + */ + public static byte[] sha512(final File file) { + return new Digester(DigestAlgorithm.SHA512).digest(file); + } + + /** + * 计算SHA-1摘要值,并转为16进制字符串 + * + * @param data 被摘要数据 + * @return SHA-512摘要的16进制表示 + */ + public static String sha512Hex(final byte[] data) { + return new Digester(DigestAlgorithm.SHA512).digestHex(data); + } + + /** + * 计算SHA-512摘要值,并转为16进制字符串 + * + * @param data 被摘要数据 + * @param charset 编码 + * @return SHA-512摘要的16进制表示 + */ + public static String sha512Hex(final String data, final String charset) { + return new Digester(DigestAlgorithm.SHA512).digestHex(data, charset); + } + + /** + * 计算SHA-512摘要值,并转为16进制字符串 + * + * @param data 被摘要数据 + * @return SHA-512摘要的16进制表示 + */ + public static String sha512Hex(final String data) { + return sha512Hex(data, CharsetUtil.UTF_8); + } + + /** + * 计算SHA-512摘要值,并转为16进制字符串 + * + * @param data 被摘要数据 + * @return SHA-512摘要的16进制表示 + */ + public static String sha512Hex(final InputStream data) { + return new Digester(DigestAlgorithm.SHA512).digestHex(data); + } + + /** + * 计算SHA-512摘要值,并转为16进制字符串 + * + * @param file 被摘要文件 + * @return SHA-512摘要的16进制表示 + */ + public static String sha512Hex(final File file) { + return new Digester(DigestAlgorithm.SHA512).digestHex(file); + } + }