add methods

This commit is contained in:
Looly 2022-02-18 00:32:48 +08:00
parent 61972190ec
commit 3f2f7027a5
2 changed files with 54 additions and 20 deletions

View File

@ -600,18 +600,18 @@ public class IterUtil {
/** /**
* 返回{@link Iterator}中第一个匹配规则的值 * 返回{@link Iterator}中第一个匹配规则的值
* *
* @param <T> 数组元素类型 * @param <T> 数组元素类型
* @param iterator {@link Iterator} * @param iterator {@link Iterator}
* @param matcher 匹配接口实现此接口自定义匹配规则 * @param matcher 匹配接口实现此接口自定义匹配规则
* @return 匹配元素如果不存在匹配元素或{@link Iterator}为空返回 {@code null} * @return 匹配元素如果不存在匹配元素或{@link Iterator}为空返回 {@code null}
* @since 5.7.5 * @since 5.7.5
*/ */
public static <T> T firstMatch(Iterator<T> iterator, Matcher<T> matcher) { public static <T> T firstMatch(Iterator<T> iterator, Matcher<T> matcher) {
Assert.notNull(matcher, "Matcher must be not null !"); Assert.notNull(matcher, "Matcher must be not null !");
if (null != iterator) { if (null != iterator) {
while(iterator.hasNext()){ while (iterator.hasNext()) {
final T next = iterator.next(); final T next = iterator.next();
if(matcher.match(next)){ if (matcher.match(next)) {
return next; return next;
} }
} }
@ -718,7 +718,7 @@ public class IterUtil {
* *
* @param <E> 集合元素类型 * @param <E> 集合元素类型
* @param iter 集合 * @param iter 集合
* @param filter 过滤器接口 * @param filter 过滤器接口删除{@link Filter#accept(Object)}{@code false}的元素
* @return 编辑后的集合 * @return 编辑后的集合
* @since 4.6.5 * @since 4.6.5
*/ */
@ -735,6 +735,29 @@ public class IterUtil {
return iter; return iter;
} }
/**
* 过滤{@link Iterator}并将过滤后满足条件的元素添加到List中
*
* @param <E> 元素类型
* @param iter {@link Iterator}
* @param filter 过滤器保留{@link Filter#accept(Object)}{@code true}的元素
* @return ArrayList
* @since 5.7.22
*/
public static <E> List<E> filterToList(Iterator<E> iter, Filter<E> filter) {
final List<E> result = new ArrayList<>();
if (null != iter) {
E ele;
while (iter.hasNext()) {
ele = iter.next();
if (null == filter || filter.accept(ele)) {
result.add(ele);
}
}
}
return result;
}
/** /**
* Iterator转换为Map转换规则为<br> * Iterator转换为Map转换规则为<br>
* 按照keyFunc函数规则根据元素对象生成Key元素作为值 * 按照keyFunc函数规则根据元素对象生成Key元素作为值

View File

@ -1,9 +1,10 @@
package cn.hutool.core.io.resource; package cn.hutool.core.io.resource;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.EnumerationIter; import cn.hutool.core.collection.EnumerationIter;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ClassLoaderUtil; import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@ -21,7 +22,6 @@ import java.util.List;
* Resource资源工具类 * Resource资源工具类
* *
* @author Looly * @author Looly
*
*/ */
public class ResourceUtil { public class ResourceUtil {
@ -40,7 +40,7 @@ public class ResourceUtil {
* 读取Classpath下的资源为字符串 * 读取Classpath下的资源为字符串
* *
* @param resource 可以是绝对路径也可以是相对路径相对ClassPath * @param resource 可以是绝对路径也可以是相对路径相对ClassPath
* @param charset 编码 * @param charset 编码
* @return 资源内容 * @return 资源内容
* @since 3.1.1 * @since 3.1.1
*/ */
@ -102,7 +102,7 @@ public class ResourceUtil {
* 从ClassPath资源中获取{@link BufferedReader} * 从ClassPath资源中获取{@link BufferedReader}
* *
* @param resource ClassPath资源 * @param resource ClassPath资源
* @param charset 编码 * @param charset 编码
* @return {@link InputStream} * @return {@link InputStream}
* @since 3.1.2 * @since 3.1.2
*/ */
@ -139,13 +139,24 @@ public class ResourceUtil {
* @return 资源列表 * @return 资源列表
*/ */
public static List<URL> getResources(String resource) { public static List<URL> getResources(String resource) {
final Enumeration<URL> resources; return getResources(resource, null);
try { }
resources = ClassLoaderUtil.getClassLoader().getResources(resource);
} catch (IOException e) { /**
throw new IORuntimeException(e); * 获取指定路径下的资源列表<br>
} * 路径格式必须为目录格式,/分隔例如:
return CollUtil.newArrayList(resources); *
* <pre>
* config/a
* spring/xml
* </pre>
*
* @param resource 资源路径
* @param filter 过滤器用于过滤不需要的资源{@code null}表示不过滤保留所有元素
* @return 资源列表
*/
public static List<URL> getResources(String resource, Filter<URL> filter) {
return IterUtil.filterToList(getResourceIter(resource), filter);
} }
/** /**
@ -174,7 +185,7 @@ public class ResourceUtil {
/** /**
* 获得资源相对路径对应的URL * 获得资源相对路径对应的URL
* *
* @param resource 资源相对路径{@code null}""都表示classpath根路径 * @param resource 资源相对路径{@code null}""都表示classpath根路径
* @param baseClass 基准Class获得的相对路径相对于此Class所在路径如果为{@code null}则相对ClassPath * @param baseClass 基准Class获得的相对路径相对于此Class所在路径如果为{@code null}则相对ClassPath
* @return {@link URL} * @return {@link URL}
*/ */
@ -192,8 +203,8 @@ public class ResourceUtil {
* @since 3.2.1 * @since 3.2.1
*/ */
public static Resource getResourceObj(String path) { public static Resource getResourceObj(String path) {
if(StrUtil.isNotBlank(path)) { if (StrUtil.isNotBlank(path)) {
if(path.startsWith(URLUtil.FILE_URL_PREFIX) || FileUtil.isAbsolutePath(path)) { if (path.startsWith(URLUtil.FILE_URL_PREFIX) || FileUtil.isAbsolutePath(path)) {
return new FileResource(path); return new FileResource(path);
} }
} }