add method

This commit is contained in:
Looly 2022-03-31 00:29:38 +08:00
parent 752516e795
commit 0a9a178fbc
5 changed files with 146 additions and 13 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.0.M2 (2022-03-30)
# 5.8.0.M2 (2022-03-31)
### ❌不兼容特性
* 【extra 】 【可能兼容问题】BeanCopierCache的key结构变更
@ -10,6 +10,7 @@
### 🐣新特性
* 【core 】 MapUtil增加entry、ofEntries方法
* 【core 】 ZipWriter增加add方法重载
* 【core 】 IterUtil增加filtered增加FilterIterissue#2228
### 🐞Bug修复
* 【core 】 IdcardUtil#getCityCodeByIdCard位数问题issue#2224@Github

View File

@ -0,0 +1,96 @@
package cn.hutool.core.collection;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Filter;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* 包装 {@link Iterator}并根据{@link Filter}定义过滤元素输出<br>
* 类实现来自Apache Commons Collection
*
* @author apache commons, looly
* @since 5.8.0
*/
public class FilterIter<E> implements Iterator<E> {
private final Iterator<? extends E> iterator;
private final Filter<? super E> filter;
/**
* 下一个元素
*/
private E nextObject;
/**
* 标记下一个元素是否被计算
*/
private boolean nextObjectSet = false;
/**
* 构造
*
* @param iterator 被包装的{@link Iterator}
* @param filter 过滤函数{@code null}表示不过滤
*/
public FilterIter(final Iterator<? extends E> iterator, final Filter<? super E> filter) {
this.iterator = Assert.notNull(iterator);
this.filter = filter;
}
@Override
public boolean hasNext() {
return nextObjectSet || setNextObject();
}
@Override
public E next() {
if (false == nextObjectSet && false == setNextObject()) {
throw new NoSuchElementException();
}
nextObjectSet = false;
return nextObject;
}
@Override
public void remove() {
if (nextObjectSet) {
throw new IllegalStateException("remove() cannot be called");
}
iterator.remove();
}
/**
* 获取被包装的{@link Iterator}
*
* @return {@link Iterator}
*/
public Iterator<? extends E> getIterator() {
return iterator;
}
/**
* 获取过滤函数
*
* @return 过滤函数可能为{@code null}
*/
public Filter<? super E> getFilter() {
return filter;
}
/**
* 设置下一个元素如果存在返回{@code true}否则{@code false}
*/
private boolean setNextObject() {
while (iterator.hasNext()) {
final E object = iterator.next();
if (null != filter && filter.accept(object)) {
nextObject = object;
nextObjectSet = true;
return true;
}
}
return false;
}
}

View File

@ -745,17 +745,20 @@ public class IterUtil {
* @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;
return toList(filtered(iter, filter));
}
/**
* 获取一个新的 {@link FilterIter}用于过滤指定元素
*
* @param iterator 被包装的 {@link Iterator}
* @param filter 过滤断言{@link Filter#accept(Object)}{@code true}时保留元素{@code false}抛弃元素
* @param <E> 元素类型
* @return {@link FilterIter}
* @since 5.8.0
*/
public static <E> FilterIter<E> filtered(final Iterator<? extends E> iterator, final Filter<? super E> filter) {
return new FilterIter<>(iterator, filter);
}
/**

View File

@ -24,7 +24,7 @@ public class TransIter<F, T> implements Iterator<T> {
* @param backingIterator {@link Iterator}
* @param func 转换函数
*/
public TransIter(Iterator<? extends F> backingIterator, Function<? super F, ? extends T> func) {
public TransIter(final Iterator<? extends F> backingIterator, final Function<? super F, ? extends T> func) {
this.backingIterator = Assert.notNull(backingIterator);
this.func = Assert.notNull(func);
}

View File

@ -103,4 +103,37 @@ public class IterUtilTest {
private String carNumber;
private String carName;
}
@Test
public void filterTest(){
List<String> obj2 = ListUtil.toList("3");
List<String> obj = ListUtil.toList("1", "3");
IterUtil.filter(obj.iterator(), obj2::contains);
Assert.assertEquals(1, obj.size());
Assert.assertEquals("3", obj.get(0));
}
@Test
public void filteredTest(){
List<String> obj2 = ListUtil.toList("3");
List<String> obj = ListUtil.toList("1", "3");
final FilterIter<String> filtered = IterUtil.filtered(obj.iterator(), obj2::contains);
Assert.assertEquals("3", filtered.next());
Assert.assertFalse(filtered.hasNext());
}
@Test
public void filterToListTest(){
List<String> obj2 = ListUtil.toList("3");
List<String> obj = ListUtil.toList("1", "3");
final List<String> filtered = IterUtil.filterToList(obj.iterator(), obj2::contains);
Assert.assertEquals(1, filtered.size());
Assert.assertEquals("3", filtered.get(0));
}
}