mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
0ed05e88ef
commit
34af7f5711
@ -7,6 +7,7 @@
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒
|
||||
* 【core 】 IterUtil增加firstMatch方法
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复FileUtil.normalize处理上级路径的问题(issue#I3YPEH@Gitee)
|
||||
|
@ -1,8 +1,10 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import cn.hutool.core.exceptions.UtilException;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.lang.Editor;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
import cn.hutool.core.lang.Matcher;
|
||||
import cn.hutool.core.lang.func.Func1;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.text.StrJoiner;
|
||||
@ -129,16 +131,7 @@ public class IterUtil {
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static boolean isAllNull(Iterator<?> iter) {
|
||||
if (null == iter) {
|
||||
return true;
|
||||
}
|
||||
|
||||
while (iter.hasNext()) {
|
||||
if (null != iter.next()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return null == getFirstNoneNull(iter);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -601,10 +594,24 @@ public class IterUtil {
|
||||
* @since 5.7.2
|
||||
*/
|
||||
public static <T> T getFirstNoneNull(Iterator<T> iterator) {
|
||||
return firstMatch(iterator, Objects::nonNull);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回{@link Iterator}中第一个匹配规则的值
|
||||
*
|
||||
* @param <T> 数组元素类型
|
||||
* @param iterator {@link Iterator}
|
||||
* @param matcher 匹配接口,实现此接口自定义匹配规则
|
||||
* @return 匹配元素,如果不存在匹配元素或{@link Iterator}为空,返回 {@code null}
|
||||
* @since 5.7.5
|
||||
*/
|
||||
public static <T> T firstMatch(Iterator<T> iterator, Matcher<T> matcher) {
|
||||
Assert.notNull(matcher, "Matcher must be not null !");
|
||||
if (null != iterator) {
|
||||
while(iterator.hasNext()){
|
||||
final T next = iterator.next();
|
||||
if(null != next){
|
||||
if(matcher.match(next)){
|
||||
return next;
|
||||
}
|
||||
}
|
||||
|
@ -159,7 +159,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @param <T> 数组元素类型
|
||||
* @param matcher 匹配接口,实现此接口自定义匹配规则
|
||||
* @param array 数组
|
||||
* @return 非空元素,如果不存在非空元素或数组为空,返回{@code null}
|
||||
* @return 匹配元素,如果不存在匹配元素或数组为空,返回 {@code null}
|
||||
* @since 3.0.7
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -198,6 +198,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> int matchIndex(Matcher<T> matcher, int beginIndexInclude, T... array) {
|
||||
Assert.notNull(matcher, "Matcher must be not null !");
|
||||
if (isNotEmpty(array)) {
|
||||
for (int i = beginIndexInclude; i < array.length; i++) {
|
||||
if (matcher.match(array[i])) {
|
||||
|
@ -223,6 +223,7 @@ public class IdUtil {
|
||||
*
|
||||
* @param datacenterId 数据中心ID
|
||||
* @param maxWorkerId 最大的机器节点ID
|
||||
* @return ID
|
||||
* @since 5.7.3
|
||||
*/
|
||||
public static long getWorkerId(long datacenterId, long maxWorkerId) {
|
||||
|
@ -12,6 +12,12 @@ import java.util.*;
|
||||
*/
|
||||
public class IterUtilTest {
|
||||
|
||||
@Test
|
||||
public void getFirstNonNullTest(){
|
||||
final ArrayList<String> strings = CollUtil.newArrayList(null, null, "123", "456", null);
|
||||
Assert.assertEquals("123", IterUtil.getFirstNoneNull(strings));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldValueMapTest() {
|
||||
ArrayList<Car> carList = CollUtil.newArrayList(new Car("123", "大众"), new Car("345", "奔驰"), new Car("567", "路虎"));
|
||||
|
Loading…
x
Reference in New Issue
Block a user