This commit is contained in:
Looly 2023-03-29 23:05:00 +08:00
parent a0ac4362dd
commit 8dc96fb511
8 changed files with 188 additions and 80 deletions

View File

@ -838,7 +838,7 @@ public class CollUtil {
* @param size 每个段的长度
* @return 分段列表
*/
public static <T> List<List<T>> split(final Collection<T> collection, final int size) {
public static <T> List<List<T>> partition(final Collection<T> collection, final int size) {
final List<List<T>> result = new ArrayList<>();
if (CollUtil.isEmpty(collection)) {
return result;
@ -1305,6 +1305,19 @@ public class CollUtil {
* @since 5.2.5
*/
public static <T> int[] indexOfAll(final Collection<T> collection, final Predicate<T> predicate) {
return Convert.convert(int[].class, indexListOfAll(collection, predicate));
}
/**
* 获取匹配规则定义中匹配到元素的所有位置<br>
* 此方法对于某些无序集合的位置信息以转换为数组后的位置为准
*
* @param <T> 元素类型
* @param collection 集合
* @param predicate 匹配器为空则全部匹配
* @return 位置数组
*/
public static <T> List<Integer> indexListOfAll(final Collection<T> collection, final Predicate<T> predicate) {
final List<Integer> 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

View File

@ -568,19 +568,6 @@ public class ListUtil {
return -1;
}
/**
* 获取匹配规则定义中匹配到元素的所有位置
*
* @param <T> 元素类型
* @param list 列表
* @param matcher 匹配器为空则全部匹配
* @return 位置数组
* @since 5.2.5
*/
public static <T> int[] indexOfAll(final List<T> list, final Predicate<T> matcher) {
return CollUtil.indexOfAll(list, matcher);
}
/**
* 通过传入分区长度将指定列表分区为不同的块每块区域的长度相同最后一块可能小于长度<br>
* 分区是在原List的基础上进行的返回的分区是不可变的抽象列表原列表元素变更分区中元素也会变更
@ -606,33 +593,14 @@ public class ListUtil {
: new Partition<>(list, size);
}
/**
* 对集合按照指定长度分段每一个段为单独的集合返回这个集合的列表
*
* <p>
* 需要特别注意的是此方法调用{@link List#subList(int, int)}切分List
* 此方法返回的是原List的视图也就是说原List有变更切分后的结果也会变更
* </p>
*
* @param <T> 集合元素类型
* @param list 列表为空时返回{@link #empty()}
* @param size 每个段的长度当长度超过list长度时size按照list长度计算即只返回一个节点
* @return 分段列表
* @see #partition(List, int)
* @since 5.4.5
*/
public static <T> List<List<T>> split(final List<T> list, final int size) {
return partition(list, size);
}
/**
* 将集合平均分成多个list返回这个集合的列表
* <p></p>
* <pre>
* 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]]
* </pre>
*
* @param <T> 集合元素类型
@ -642,7 +610,7 @@ public class ListUtil {
* @author lileming
* @since 5.7.10
*/
public static <T> List<List<T>> splitAvg(final List<T> list, final int limit) {
public static <T> List<List<T>> avgPartition(final List<T> list, final int limit) {
if (CollUtil.isEmpty(list)) {
return empty();
}

View File

@ -130,8 +130,8 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
*/
public List<V> 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<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
*/
public List<K> 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<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
@Override
public String toString() {
return "TableMap{" +
"keys=" + keys +
", values=" + values +
'}';
"keys=" + keys +
", values=" + values +
'}';
}
@Override
@ -317,11 +317,10 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
}
@SuppressWarnings("NullableProblems")
@Override
public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
if(null == remappingFunction){
if (null == remappingFunction) {
return null;
}
@ -329,9 +328,9 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, 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--;

View File

@ -297,17 +297,17 @@ public class CollUtilTest {
}
@Test
public void splitTest() {
public void partitionTest() {
final List<Integer> list = ListUtil.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
final List<List<Integer>> split = CollUtil.split(list, 3);
final List<List<Integer>> 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<Integer> list = ListUtil.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
final List<List<Integer>> split = CollUtil.split(list, Integer.MAX_VALUE);
final List<List<Integer>> split = CollUtil.partition(list, Integer.MAX_VALUE);
Assert.assertEquals(1, split.size());
Assert.assertEquals(9, split.get(0).size());
}

View File

@ -16,25 +16,25 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class ListUtilTest {
@Test
public void splitTest() {
List<List<Object>> lists = ListUtil.split(null, 3);
public void partitionTest() {
List<List<Object>> 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<String> 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<List<String>> CollSplitResult = CollUtil.split(list, size);
final List<List<String>> CollSplitResult = CollUtil.partition(list, size);
stopWatch.stop();
stopWatch.start("ListUtil#split");
final List<List<String>> ListSplitResult = ListUtil.split(list, size);
final List<List<String>> ListSplitResult = ListUtil.partition(list, size);
stopWatch.stop();
Assert.assertEquals(CollSplitResult, ListSplitResult);
@ -58,28 +58,28 @@ public class ListUtilTest {
@Test
public void splitAvgTest() {
List<List<Object>> lists = ListUtil.splitAvg(null, 3);
List<List<Object>> 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

View File

@ -650,7 +650,7 @@ public class AbstractEnhancedWrappedStreamTest {
public void testListSplit() {
final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> 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<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> 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();

View File

@ -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 被检查的状态码

View File

@ -75,10 +75,6 @@ import java.util.concurrent.atomic.AtomicInteger;
@SuppressWarnings("resource")
public class ExcelWriter extends ExcelBase<ExcelWriter> {
/**
* 当前行
*/
private AtomicInteger currentRow = new AtomicInteger(0);
/**
* 是否只保留别名对应的字段
*/
@ -99,6 +95,10 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* 单元格值处理接口
*/
private CellEditor cellEditor;
/**
* 当前行
*/
private final AtomicInteger currentRow;
// -------------------------------------------------------------------------- Constructor start
@ -200,6 +200,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
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<ExcelWriter> {
*/
protected void closeWithoutFlush() {
super.close();
this.currentRow.set(0);
// 清空对象
this.currentRow = null;
this.styleSet = null;
}