fix readBySax for xls not support sheetName

This commit is contained in:
Looly
2021-05-25 01:59:29 +08:00
parent c5887344e9
commit 9c290c4bcc
7 changed files with 174 additions and 57 deletions

View File

@@ -1583,6 +1583,57 @@ public class CollUtil {
return count;
}
/**
* 获取匹配规则定义中匹配到元素的第一个位置<br>
* 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。
*
* @param <T> 元素类型
* @param collection 集合
* @param matcher 匹配器,为空则全部匹配
* @return 第一个位置
* @since 5.6.6
*/
public static <T> int indexOf(Collection<T> collection, Matcher<T> matcher) {
if (isNotEmpty(collection)) {
int index = 0;
for (T t : collection) {
if (null == matcher || matcher.match(t)) {
return index;
}
index++;
}
}
return -1;
}
/**
* 获取匹配规则定义中匹配到元素的最后位置<br>
* 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。
*
* @param <T> 元素类型
* @param collection 集合
* @param matcher 匹配器,为空则全部匹配
* @return 最后一个位置
* @since 5.6.6
*/
public static <T> int lastIndexOf(Collection<T> collection, Matcher<T> matcher) {
if(collection instanceof List){
// List的查找最后一个有优化算法
return ListUtil.lastIndexOf((List<T>)collection, matcher);
}
int matchIndex = -1;
if (isNotEmpty(collection)) {
int index = collection.size();
for (T t : collection) {
if (null == matcher || matcher.match(t)) {
matchIndex = index;
}
index--;
}
}
return matchIndex;
}
/**
* 获取匹配规则定义中匹配到元素的所有位置<br>
* 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。

View File

@@ -2,7 +2,6 @@ package cn.hutool.core.collection;
import cn.hutool.core.comparator.PinyinComparator;
import cn.hutool.core.comparator.PropertyComparator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Matcher;
import cn.hutool.core.util.ArrayUtil;
@@ -455,6 +454,30 @@ public class ListUtil {
return list2;
}
/**
* 获取匹配规则定义中匹配到元素的最后位置<br>
* 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。
*
* @param <T> 元素类型
* @param list List集合
* @param matcher 匹配器,为空则全部匹配
* @return 最后一个位置
* @since 5.6.6
*/
public static <T> int lastIndexOf(List<T> list, Matcher<T> matcher) {
if (null != list) {
final int size = list.size();
if(size > 0){
for(int i = size -1; i >= 0; i--){
if (null == matcher || matcher.match(list.get(i))) {
return i;
}
}
}
}
return -1;
}
/**
* 获取匹配规则定义中匹配到元素的所有位置
*
@@ -465,17 +488,7 @@ public class ListUtil {
* @since 5.2.5
*/
public static <T> int[] indexOfAll(List<T> list, Matcher<T> matcher) {
final List<Integer> indexList = new ArrayList<>();
if (null != list) {
int index = 0;
for (T t : list) {
if (null == matcher || matcher.match(t)) {
indexList.add(index);
}
index++;
}
}
return Convert.convert(int[].class, indexList);
return CollUtil.indexOfAll(list, matcher);
}
/**

View File

@@ -163,14 +163,34 @@ public class ArrayUtil extends PrimitiveArrayUtil {
*/
@SuppressWarnings("unchecked")
public static <T> T firstMatch(Matcher<T> matcher, T... array) {
final int index = matchIndex(matcher, array);
if(index < 0){
return null;
}
return array[index];
}
/**
* 返回数组中第一个匹配规则的值的位置
*
* @param <T> 数组元素类型
* @param matcher 匹配接口,实现此接口自定义匹配规则
* @param array 数组
* @return 匹配到元素的位置,-1表示未匹配到
* @since 5.6.6
*/
@SuppressWarnings("unchecked")
public static <T> int matchIndex(Matcher<T> matcher, T... array) {
if (isNotEmpty(array)) {
for (final T val : array) {
if (matcher.match(val)) {
return val;
for(int i = 0; i < array.length; i++){
if(matcher.match(array[i])){
return i;
}
}
}
return null;
return INDEX_NOT_FOUND;
}
/**
@@ -739,14 +759,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.0.7
*/
public static <T> int indexOf(T[] array, Object value) {
if (null != array) {
for (int i = 0; i < array.length; i++) {
if (ObjectUtil.equal(value, array[i])) {
return i;
}
}
}
return INDEX_NOT_FOUND;
return matchIndex((obj)-> ObjectUtil.equal(value, obj), array);
}
/**

View File

@@ -686,6 +686,29 @@ public class CollUtilTest {
Assert.assertEquals(Integer.valueOf(1), countMap.get("d"));
}
@Test
public void indexOfTest() {
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d");
final int i = CollUtil.indexOf(list, (str) -> str.charAt(0) == 'c');
Assert.assertEquals(2, i);
}
@Test
public void lastIndexOfTest() {
// List有优化
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c", "c", "a", "b", "d");
final int i = CollUtil.lastIndexOf(list, (str) -> str.charAt(0) == 'c');
Assert.assertEquals(3, i);
}
@Test
public void lastIndexOfSetTest() {
Set<String> list = CollUtil.set(true, "a", "b", "c", "c", "a", "b", "d");
// 去重后c排第三
final int i = CollUtil.lastIndexOf(list, (str) -> str.charAt(0) == 'c');
Assert.assertEquals(2, i);
}
@Test
public void pageTest(){
List<Dict> objects = CollUtil.newArrayList();