mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add trans
This commit is contained in:
parent
f05883a53a
commit
7d81b20e2b
@ -11,6 +11,7 @@
|
||||
* 【setting】 StatementUtil增加null缓存(pr#1076@Github)
|
||||
* 【core 】 扩充Console功能,支持可变参数(issue#1077@Github)
|
||||
* 【crypto 】 增加ECKeyUtil(issue#I1UOF5@Gitee)
|
||||
* 【core 】 增加TransXXX(issue#I1TU1Y@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【core 】 修复Dict.of错误(issue#I1UUO5@Gitee)
|
||||
|
@ -414,9 +414,9 @@ public class CollUtil {
|
||||
/**
|
||||
* 自定义函数判断集合是否包含某类值
|
||||
*
|
||||
* @param collection 集合
|
||||
* @param collection 集合
|
||||
* @param containFunc 自定义判断函数
|
||||
* @param <T> 值类型
|
||||
* @param <T> 值类型
|
||||
* @return 是否包含自定义规则的值
|
||||
*/
|
||||
public static <T> boolean contains(Collection<T> collection, Predicate<? super T> containFunc) {
|
||||
@ -2837,6 +2837,18 @@ public class CollUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用给定的转换函数,转换源集合为新类型的集合
|
||||
*
|
||||
* @param <F> 源元素类型
|
||||
* @param <T> 目标元素类型
|
||||
* @return 新类型的集合
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <F, T> Collection<T> trans(Collection<F> collection, Function<? super F, ? extends T> function) {
|
||||
return new TransCollection<>(collection, function);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------- Interface start
|
||||
|
||||
/**
|
||||
|
@ -807,4 +807,18 @@ public class IterUtil {
|
||||
public static <T> Iterator<T> empty() {
|
||||
return Collections.emptyIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按照给定函数,转换{@link Iterator}为另一种类型的{@link Iterator}
|
||||
*
|
||||
* @param <F> 源元素类型
|
||||
* @param <T> 目标元素类型
|
||||
* @param iterator 源{@link Iterator}
|
||||
* @param function 转换函数
|
||||
* @return 转换后的{@link Iterator}
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static <F, T> Iterator<T> trans(Iterator<F> iterator, Function<? super F, ? extends T> function) {
|
||||
return new TransIter<>(iterator, function);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* {@link Spliterator}相关工具类
|
||||
*
|
||||
* @author looly
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public class SpliteratorUtil {
|
||||
|
||||
/**
|
||||
* 使用给定的转换函数,转换源{@link Spliterator}为新类型的{@link Spliterator}
|
||||
*
|
||||
* @param <F> 源元素类型
|
||||
* @param <T> 目标元素类型
|
||||
* @return 新类型的{@link Spliterator}
|
||||
*/
|
||||
public static <F, T> Spliterator<T> trans(Spliterator<F> fromSpliterator, Function<? super F, ? extends T> function) {
|
||||
return new TransSpliterator<>(fromSpliterator, function);
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
|
||||
import java.util.AbstractCollection;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 使用给定的转换函数,转换源集合为新类型的集合
|
||||
*
|
||||
* @param <F> 源元素类型
|
||||
* @param <T> 目标元素类型
|
||||
* @author looly
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public class TransCollection<F, T> extends AbstractCollection<T> {
|
||||
|
||||
private final Collection<F> fromCollection;
|
||||
private final Function<? super F, ? extends T> function;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param fromCollection 源集合
|
||||
* @param function 转换函数
|
||||
*/
|
||||
public TransCollection(Collection<F> fromCollection, Function<? super F, ? extends T> function) {
|
||||
this.fromCollection = Assert.notNull(fromCollection);
|
||||
this.function = Assert.notNull(function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return IterUtil.trans(fromCollection.iterator(), function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
fromCollection.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return fromCollection.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super T> action) {
|
||||
Assert.notNull(action);
|
||||
fromCollection.forEach((f) -> action.accept(function.apply(f)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeIf(Predicate<? super T> filter) {
|
||||
Assert.notNull(filter);
|
||||
return fromCollection.removeIf(element -> filter.test(function.apply(element)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<T> spliterator() {
|
||||
return SpliteratorUtil.trans(fromCollection.spliterator(), function);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return fromCollection.size();
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 使用给定的转换函数,转换源{@link Iterator}为新类型的{@link Iterator}
|
||||
*
|
||||
* @param <F> 源元素类型
|
||||
* @param <T> 目标元素类型
|
||||
* @author looly
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public class TransIter<F, T> implements Iterator<T> {
|
||||
|
||||
private final Iterator<? extends F> backingIterator;
|
||||
private final Function<? super F, ? extends T> func;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param backingIterator 源{@link Iterator}
|
||||
* @param func 转换函数
|
||||
*/
|
||||
public TransIter(Iterator<? extends F> backingIterator, Function<? super F, ? extends T> func) {
|
||||
this.backingIterator = Assert.notNull(backingIterator);
|
||||
this.func = Assert.notNull(func);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean hasNext() {
|
||||
return backingIterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T next() {
|
||||
return func.apply(backingIterator.next());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void remove() {
|
||||
backingIterator.remove();
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import java.util.Spliterator;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 使用给定的转换函数,转换源{@link Spliterator}为新类型的{@link Spliterator}
|
||||
*
|
||||
* @param <F> 源元素类型
|
||||
* @param <T> 目标元素类型
|
||||
* @author looly
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public class TransSpliterator<F, T> implements Spliterator<T> {
|
||||
private final Spliterator<F> fromSpliterator;
|
||||
private final Function<? super F, ? extends T> function;
|
||||
|
||||
public TransSpliterator(Spliterator<F> fromSpliterator, Function<? super F, ? extends T> function) {
|
||||
this.fromSpliterator = fromSpliterator;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super T> action) {
|
||||
return fromSpliterator.tryAdvance(
|
||||
fromElement -> action.accept(function.apply(fromElement)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachRemaining(Consumer<? super T> action) {
|
||||
fromSpliterator.forEachRemaining(fromElement -> action.accept(function.apply(fromElement)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<T> trySplit() {
|
||||
Spliterator<F> fromSplit = fromSpliterator.trySplit();
|
||||
return (fromSplit != null) ? new TransSpliterator<>(fromSplit, function) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long estimateSize() {
|
||||
return fromSpliterator.estimateSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int characteristics() {
|
||||
return fromSpliterator.characteristics()
|
||||
& ~(Spliterator.DISTINCT | Spliterator.NONNULL | Spliterator.SORTED);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user