From 8dc96fb511eba46ecd1a8fc82dd8b37083bd940d Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 29 Mar 2023 23:05:00 +0800 Subject: [PATCH] fix code --- .../cn/hutool/core/collection/CollUtil.java | 17 ++- .../cn/hutool/core/collection/ListUtil.java | 42 +----- .../java/cn/hutool/core/map/TableMap.java | 21 ++- .../hutool/core/collection/CollUtilTest.java | 8 +- .../hutool/core/collection/ListUtilTest.java | 36 ++--- .../AbstractEnhancedWrappedStreamTest.java | 4 +- .../java/cn/hutool/http/meta/HttpStatus.java | 129 +++++++++++++++++- .../java/cn/hutool/poi/excel/ExcelWriter.java | 11 +- 8 files changed, 188 insertions(+), 80 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 1eb2701d2..0ded0c382 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -838,7 +838,7 @@ public class CollUtil { * @param size 每个段的长度 * @return 分段列表 */ - public static List> split(final Collection collection, final int size) { + public static List> partition(final Collection collection, final int size) { final List> result = new ArrayList<>(); if (CollUtil.isEmpty(collection)) { return result; @@ -1305,6 +1305,19 @@ public class CollUtil { * @since 5.2.5 */ public static int[] indexOfAll(final Collection collection, final Predicate predicate) { + return Convert.convert(int[].class, indexListOfAll(collection, predicate)); + } + + /** + * 获取匹配规则定义中匹配到元素的所有位置
+ * 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。 + * + * @param 元素类型 + * @param collection 集合 + * @param predicate 匹配器,为空则全部匹配 + * @return 位置数组 + */ + public static List indexListOfAll(final Collection collection, final Predicate predicate) { final List indexList = new ArrayList<>(); if (null != collection) { int index = 0; @@ -1316,7 +1329,7 @@ public class CollUtil { } } - return Convert.convert(int[].class, indexList); + return indexList; } // ---------------------------------------------------------------------- zip diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index 9f22debb9..7f87ec73c 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java @@ -568,19 +568,6 @@ public class ListUtil { return -1; } - /** - * 获取匹配规则定义中匹配到元素的所有位置 - * - * @param 元素类型 - * @param list 列表 - * @param matcher 匹配器,为空则全部匹配 - * @return 位置数组 - * @since 5.2.5 - */ - public static int[] indexOfAll(final List list, final Predicate matcher) { - return CollUtil.indexOfAll(list, matcher); - } - /** * 通过传入分区长度,将指定列表分区为不同的块,每块区域的长度相同(最后一块可能小于长度)
* 分区是在原List的基础上进行的,返回的分区是不可变的抽象列表,原列表元素变更,分区中元素也会变更。 @@ -606,33 +593,14 @@ public class ListUtil { : new Partition<>(list, size); } - /** - * 对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表 - * - *

- * 需要特别注意的是,此方法调用{@link List#subList(int, int)}切分List, - * 此方法返回的是原List的视图,也就是说原List有变更,切分后的结果也会变更。 - *

- * - * @param 集合元素类型 - * @param list 列表,为空时返回{@link #empty()} - * @param size 每个段的长度,当长度超过list长度时,size按照list长度计算,即只返回一个节点 - * @return 分段列表 - * @see #partition(List, int) - * @since 5.4.5 - */ - public static List> split(final List list, final int size) { - return partition(list, size); - } - /** * 将集合平均分成多个list,返回这个集合的列表 *

例:

*
-	 *     ListUtil.splitAvg(null, 3);	// []
-	 *     ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 2);	// [[1, 2], [3, 4]]
-	 *     ListUtil.splitAvg(Arrays.asList(1, 2, 3), 5);	// [[1], [2], [3], [], []]
-	 *     ListUtil.splitAvg(Arrays.asList(1, 2, 3), 2);	// [[1, 2], [3]]
+	 *     ListUtil.avgPartition(null, 3);	// []
+	 *     ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 2);	// [[1, 2], [3, 4]]
+	 *     ListUtil.avgPartition(Arrays.asList(1, 2, 3), 5);	// [[1], [2], [3], [], []]
+	 *     ListUtil.avgPartition(Arrays.asList(1, 2, 3), 2);	// [[1, 2], [3]]
 	 * 
* * @param 集合元素类型 @@ -642,7 +610,7 @@ public class ListUtil { * @author lileming * @since 5.7.10 */ - public static List> splitAvg(final List list, final int limit) { + public static List> avgPartition(final List list, final int limit) { if (CollUtil.isEmpty(list)) { return empty(); } diff --git a/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java b/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java index fb97aab82..81564f623 100755 --- a/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java @@ -130,8 +130,8 @@ public class TableMap implements Map, Iterable>, Ser */ public List getValues(final K key) { return CollUtil.getAny( - this.values, - ListUtil.indexOfAll(this.keys, (ele) -> ObjUtil.equals(ele, key)) + this.values, + CollUtil.indexOfAll(this.keys, (ele) -> ObjUtil.equals(ele, key)) ); } @@ -144,8 +144,8 @@ public class TableMap implements Map, Iterable>, Ser */ public List getKeys(final V value) { return CollUtil.getAny( - this.keys, - ListUtil.indexOfAll(this.values, (ele) -> ObjUtil.equals(ele, value)) + this.keys, + CollUtil.indexOfAll(this.values, (ele) -> ObjUtil.equals(ele, value)) ); } @@ -253,9 +253,9 @@ public class TableMap implements Map, Iterable>, Ser @Override public String toString() { return "TableMap{" + - "keys=" + keys + - ", values=" + values + - '}'; + "keys=" + keys + + ", values=" + values + + '}'; } @Override @@ -317,11 +317,10 @@ public class TableMap implements Map, Iterable>, Ser } - @SuppressWarnings("NullableProblems") @Override public V computeIfPresent(final K key, final BiFunction remappingFunction) { - if(null == remappingFunction){ + if (null == remappingFunction) { return null; } @@ -329,9 +328,9 @@ public class TableMap implements Map, Iterable>, Ser for (int i = 0; i < size(); i++) { if (ObjUtil.equals(key, keys.get(i))) { final V newValue = remappingFunction.apply(key, values.get(i)); - if(null != newValue){ + if (null != newValue) { lastValue = values.set(i, newValue); - } else{ + } else { removeByIndex(i); // 移除当前元素,下个元素前移 i--; diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 6242a5230..4c1abd8e4 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -297,17 +297,17 @@ public class CollUtilTest { } @Test - public void splitTest() { + public void partitionTest() { final List list = ListUtil.of(1, 2, 3, 4, 5, 6, 7, 8, 9); - final List> split = CollUtil.split(list, 3); + final List> split = CollUtil.partition(list, 3); Assert.assertEquals(3, split.size()); Assert.assertEquals(3, split.get(0).size()); } @Test - public void splitTest2() { + public void partitionTest2() { final ArrayList list = ListUtil.of(1, 2, 3, 4, 5, 6, 7, 8, 9); - final List> split = CollUtil.split(list, Integer.MAX_VALUE); + final List> split = CollUtil.partition(list, Integer.MAX_VALUE); Assert.assertEquals(1, split.size()); Assert.assertEquals(9, split.get(0).size()); } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java index d54714b4b..bfdea2fa9 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java @@ -16,25 +16,25 @@ import java.util.concurrent.CopyOnWriteArrayList; public class ListUtilTest { @Test - public void splitTest() { - List> lists = ListUtil.split(null, 3); + public void partitionTest() { + List> lists = ListUtil.partition(null, 3); Assert.assertEquals(ListUtil.empty(), lists); - lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 1); + lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 1); Assert.assertEquals("[[1], [2], [3], [4]]", lists.toString()); - lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 2); + lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 2); Assert.assertEquals("[[1, 2], [3, 4]]", lists.toString()); - lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 3); + lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 3); Assert.assertEquals("[[1, 2, 3], [4]]", lists.toString()); - lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 4); + lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 4); Assert.assertEquals("[[1, 2, 3, 4]]", lists.toString()); - lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 5); + lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 5); Assert.assertEquals("[[1, 2, 3, 4]]", lists.toString()); } @Test @Ignore - public void splitBenchTest() { + public void partitionBenchTest() { final List list = new ArrayList<>(); CollUtil.padRight(list, RandomUtil.randomInt(1000_0000, 1_0000_0000), "test"); @@ -44,11 +44,11 @@ public class ListUtilTest { final StopWatch stopWatch = new StopWatch(); stopWatch.start("CollUtil#split"); - final List> CollSplitResult = CollUtil.split(list, size); + final List> CollSplitResult = CollUtil.partition(list, size); stopWatch.stop(); stopWatch.start("ListUtil#split"); - final List> ListSplitResult = ListUtil.split(list, size); + final List> ListSplitResult = ListUtil.partition(list, size); stopWatch.stop(); Assert.assertEquals(CollSplitResult, ListSplitResult); @@ -58,28 +58,28 @@ public class ListUtilTest { @Test public void splitAvgTest() { - List> lists = ListUtil.splitAvg(null, 3); + List> lists = ListUtil.avgPartition(null, 3); Assert.assertEquals(ListUtil.empty(), lists); - lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 1); + lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 1); Assert.assertEquals("[[1, 2, 3, 4]]", lists.toString()); - lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 2); + lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 2); Assert.assertEquals("[[1, 2], [3, 4]]", lists.toString()); - lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 3); + lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 3); Assert.assertEquals("[[1, 2], [3], [4]]", lists.toString()); - lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 4); + lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 4); Assert.assertEquals("[[1], [2], [3], [4]]", lists.toString()); - lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3), 5); + lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3), 5); Assert.assertEquals("[[1], [2], [3], [], []]", lists.toString()); - lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3), 2); + lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3), 2); Assert.assertEquals("[[1, 2], [3]]", lists.toString()); } @Test(expected = IllegalArgumentException.class) public void splitAvgNotZero() { // limit不能小于等于0 - ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 0); + ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 0); } @Test diff --git a/hutool-core/src/test/java/cn/hutool/core/stream/AbstractEnhancedWrappedStreamTest.java b/hutool-core/src/test/java/cn/hutool/core/stream/AbstractEnhancedWrappedStreamTest.java index 7af584b4d..033d35c13 100644 --- a/hutool-core/src/test/java/cn/hutool/core/stream/AbstractEnhancedWrappedStreamTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/stream/AbstractEnhancedWrappedStreamTest.java @@ -650,7 +650,7 @@ public class AbstractEnhancedWrappedStreamTest { public void testListSplit() { final List list = Arrays.asList(1, 2, 3, 4, 5); List> lists = wrap(list).split(2).map(TerminableWrappedStream::toList).toList(); - Assert.assertEquals(ListUtil.split(list, 2), lists); + Assert.assertEquals(ListUtil.partition(list, 2), lists); // 指定长度 大于等于 列表长度 lists = wrap(list).split(list.size()).map(TerminableWrappedStream::toList).toList(); @@ -661,7 +661,7 @@ public class AbstractEnhancedWrappedStreamTest { public void testSplitList() { final List list = Arrays.asList(1, 2, 3, 4, 5); List> lists = wrap(list).splitList(2).toList(); - Assert.assertEquals(ListUtil.split(list, 2), lists); + Assert.assertEquals(ListUtil.partition(list, 2), lists); // 指定长度 大于等于 列表长度 lists = wrap(list).splitList(list.size()).toList(); diff --git a/hutool-http/src/main/java/cn/hutool/http/meta/HttpStatus.java b/hutool-http/src/main/java/cn/hutool/http/meta/HttpStatus.java index e44bd49d1..608ef3f43 100644 --- a/hutool-http/src/main/java/cn/hutool/http/meta/HttpStatus.java +++ b/hutool-http/src/main/java/cn/hutool/http/meta/HttpStatus.java @@ -15,12 +15,34 @@ package cn.hutool.http.meta; /** * HTTP状态码 * - * @author Looly + * @author Looly, Ningqingsheng * @see java.net.HttpURLConnection * */ public interface HttpStatus { + /* 1XX: Informational */ + + /** + * HTTP Status-Code 100: Continue. + */ + int HTTP_CONTINUE = 100; + + /** + * HTTP Status-Code 101: Switching Protocols. + */ + int HTTP_SWITCHING_PROTOCOLS = 101; + + /** + * HTTP Status-Code 102: Processing. + */ + int HTTP_PROCESSING = 102; + + /** + * HTTP Status-Code 103: Checkpoint. + */ + int HTTP_CHECKPOINT = 103; + /* 2XX: generally "OK" */ /** @@ -58,6 +80,21 @@ public interface HttpStatus { */ int HTTP_PARTIAL = 206; + /** + * HTTP Status-Code 207: Multi-Status. + */ + int HTTP_MULTI_STATUS = 207; + + /** + * HTTP Status-Code 208: Already Reported. + */ + int HTTP_ALREADY_REPORTED = 208; + + /** + * HTTP Status-Code 226: IM Used. + */ + int HTTP_IM_USED = 226; + /* 3XX: relocation/redirect */ /** @@ -184,6 +221,66 @@ public interface HttpStatus { */ int HTTP_UNSUPPORTED_TYPE = 415; + /** + * HTTP Status-Code 416: Requested Range Not Satisfiable. + */ + int HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416; + + /** + * HTTP Status-Code 417: Expectation Failed. + */ + int HTTP_EXPECTATION_FAILED = 417; + + /** + * HTTP Status-Code 418: I'm a teapot. + */ + int HTTP_I_AM_A_TEAPOT = 418; + + /** + * HTTP Status-Code 422: Unprocessable Entity. + */ + int HTTP_UNPROCESSABLE_ENTITY = 422; + + /** + * HTTP Status-Code 423: Locked. + */ + int HTTP_LOCKED = 423; + + /** + * HTTP Status-Code 424: Failed Dependency. + */ + int HTTP_FAILED_DEPENDENCY = 424; + + /** + * HTTP Status-Code 425: Too Early. + */ + int HTTP_TOO_EARLY = 425; + + /** + * HTTP Status-Code 426: Upgrade Required. + */ + int HTTP_UPGRADE_REQUIRED = 426; + + /** + * HTTP Status-Code 428: Precondition Required. + */ + int HTTP_PRECONDITION_REQUIRED = 428; + + /** + * HTTP Status-Code 429: Too Many Requests. + */ + int HTTP_TOO_MANY_REQUESTS = 429; + + /** + * HTTP Status-Code 431: Request Header Fields Too Large. + */ + int HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; + + /** + * HTTP Status-Code 451: Unavailable For Legal Reasons. + */ + int HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451; + /* 5XX: server error */ /** @@ -216,6 +313,36 @@ public interface HttpStatus { */ int HTTP_VERSION = 505; + /** + * HTTP Status-Code 506: Variant Also Negotiates. + */ + int HTTP_VARIANT_ALSO_NEGOTIATES = 506; + + /** + * HTTP Status-Code 507: Insufficient Storage. + */ + int HTTP_INSUFFICIENT_STORAGE = 507; + + /** + * HTTP Status-Code 508: Loop Detected. + */ + int HTTP_LOOP_DETECTED = 508; + + /** + * HTTP Status-Code 509: Bandwidth Limit Exceeded. + */ + int HTTP_BANDWIDTH_LIMIT_EXCEEDED = 509; + + /** + * HTTP Status-Code 510: Not Extended. + */ + int HTTP_NOT_EXTENDED = 510; + + /** + * HTTP Status-Code 511: Network Authentication Required. + */ + int HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; + /** * 是否为重定向状态码 * @param responseCode 被检查的状态码 diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java index 93ea06ac4..d3df43cfd 100755 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/ExcelWriter.java @@ -75,10 +75,6 @@ import java.util.concurrent.atomic.AtomicInteger; @SuppressWarnings("resource") public class ExcelWriter extends ExcelBase { - /** - * 当前行 - */ - private AtomicInteger currentRow = new AtomicInteger(0); /** * 是否只保留别名对应的字段 */ @@ -99,6 +95,10 @@ public class ExcelWriter extends ExcelBase { * 单元格值处理接口 */ private CellEditor cellEditor; + /** + * 当前行 + */ + private final AtomicInteger currentRow; // -------------------------------------------------------------------------- Constructor start @@ -200,6 +200,7 @@ public class ExcelWriter extends ExcelBase { public ExcelWriter(final Sheet sheet) { super(sheet); this.styleSet = new StyleSet(workbook); + this.currentRow = new AtomicInteger(0); } // -------------------------------------------------------------------------- Constructor end @@ -1356,9 +1357,9 @@ public class ExcelWriter extends ExcelBase { */ protected void closeWithoutFlush() { super.close(); + this.currentRow.set(0); // 清空对象 - this.currentRow = null; this.styleSet = null; }