mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
调整方法名称
This commit is contained in:
parent
924a1c6a22
commit
d58502ad76
@ -25,7 +25,7 @@ public abstract class AbstractEnhancedWrappedStream<T, S extends AbstractEnhance
|
||||
* 获取被包装的元素流实例
|
||||
*/
|
||||
@Override
|
||||
public Stream<T> stream() {
|
||||
public Stream<T> unwrap() {
|
||||
return stream;
|
||||
}
|
||||
|
||||
@ -33,10 +33,10 @@ public abstract class AbstractEnhancedWrappedStream<T, S extends AbstractEnhance
|
||||
* 创建一个流包装器
|
||||
*
|
||||
* @param stream 包装的流对象
|
||||
* @throws NullPointerException 当{@code stream}为{@code null}时抛出
|
||||
* @throws NullPointerException 当{@code unwrap}为{@code null}时抛出
|
||||
*/
|
||||
protected AbstractEnhancedWrappedStream(final Stream<T> stream) {
|
||||
this.stream = Objects.requireNonNull(stream, "stream must not null");
|
||||
this.stream = Objects.requireNonNull(stream, "unwrap must not null");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,7 +102,7 @@ public class CollectorUtil {
|
||||
final K key = Opt.ofNullable(t).map(classifier).orElse(null);
|
||||
final A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
|
||||
if (ArrayUtil.isArray(container) || Objects.nonNull(t)) {
|
||||
// 如果是数组类型,不需要判空,场景——分组后需要使用:java.util.stream.Collectors.counting 求null元素个数
|
||||
// 如果是数组类型,不需要判空,场景——分组后需要使用:java.util.unwrap.Collectors.counting 求null元素个数
|
||||
downstreamAccumulator.accept(container, t);
|
||||
}
|
||||
};
|
||||
|
@ -15,7 +15,7 @@ import java.util.stream.StreamSupport;
|
||||
|
||||
/**
|
||||
* <p>单元素的扩展流实现。基于原生Stream进行了封装和增强。<br>
|
||||
* 作者经对比了vavr、eclipse-collection、stream-ex以及其他语言的api,结合日常使用习惯,进行封装和拓展
|
||||
* 作者经对比了vavr、eclipse-collection、unwrap-ex以及其他语言的api,结合日常使用习惯,进行封装和拓展
|
||||
* Stream为集合提供了一些易用api,它让开发人员能使用声明式编程的方式去编写代码。
|
||||
*
|
||||
* <p>中间操作和结束操作</p>
|
||||
@ -67,7 +67,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
* 返回{@code FastStream}的建造器
|
||||
*
|
||||
* @param <T> 元素的类型
|
||||
* @return a stream builder
|
||||
* @return a unwrap builder
|
||||
*/
|
||||
public static <T> Builder<T> builder() {
|
||||
return new Builder<T>() {
|
||||
@ -262,7 +262,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
* @return 实现类
|
||||
*/
|
||||
@Override
|
||||
public EasyStream<T> wrapping(final Stream<T> stream) {
|
||||
public EasyStream<T> wrap(final Stream<T> stream) {
|
||||
return new EasyStream<>(stream);
|
||||
}
|
||||
|
||||
@ -359,7 +359,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
public interface Builder<T> extends Consumer<T>, cn.hutool.core.builder.Builder<EasyStream<T>> {
|
||||
|
||||
/**
|
||||
* Adds an element to the stream being built.
|
||||
* Adds an element to the unwrap being built.
|
||||
*
|
||||
* @param t the element to add
|
||||
* @return {@code this} builder
|
||||
|
@ -159,7 +159,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
public EntryStream<K, V> distinctByKey() {
|
||||
// FIXME fix happen NPE when has null key
|
||||
final Set<K> accessed = new ConcurrentHashSet<>(16);
|
||||
return wrapping(stream.filter(e -> {
|
||||
return wrap(stream.filter(e -> {
|
||||
final K key = e.getKey();
|
||||
if (accessed.contains(key)) {
|
||||
return false;
|
||||
@ -177,7 +177,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
public EntryStream<K, V> distinctByValue() {
|
||||
// FIXME fix happen NPE when has null value
|
||||
final Set<V> accessed = new ConcurrentHashSet<>(16);
|
||||
return wrapping(stream.filter(e -> {
|
||||
return wrap(stream.filter(e -> {
|
||||
final V val = e.getValue();
|
||||
if (accessed.contains(val)) {
|
||||
return false;
|
||||
@ -301,7 +301,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
* @return {@link EntryStream}实例
|
||||
*/
|
||||
public EntryStream<K, V> push(final K key, final V value) {
|
||||
return wrapping(Stream.concat(stream, Stream.of(ofEntry(key, value))));
|
||||
return wrap(Stream.concat(stream, Stream.of(ofEntry(key, value))));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -312,7 +312,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
* @return {@link EntryStream}实例
|
||||
*/
|
||||
public EntryStream<K, V> unshift(final K key, final V value) {
|
||||
return wrapping(Stream.concat(Stream.of(ofEntry(key, value)), stream));
|
||||
return wrap(Stream.concat(Stream.of(ofEntry(key, value)), stream));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -328,7 +328,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
}
|
||||
final Stream<Map.Entry<K, V>> contacted = StreamSupport.stream(entries.spliterator(), isParallel())
|
||||
.map(EntryStream::ofEntry);
|
||||
return wrapping(Stream.concat(stream, contacted));
|
||||
return wrap(Stream.concat(stream, contacted));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -344,7 +344,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
}
|
||||
final Stream<Map.Entry<K, V>> contacted = StreamSupport.stream(entries.spliterator(), isParallel())
|
||||
.map(EntryStream::ofEntry);
|
||||
return wrapping(Stream.concat(contacted, stream));
|
||||
return wrap(Stream.concat(contacted, stream));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -442,9 +442,9 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
* 然后再返回由这些流中所有元素组成的流新{@link EntryStream}串行流。<br>
|
||||
* 效果类似:
|
||||
* <pre>{@code
|
||||
* // stream = [{a = 1}, {b = 2}, {c = 3}]
|
||||
* stream.flatMapKey(key -> Stream.of(key + "1", key + "2"));
|
||||
* // stream = [{a1 = 1}, {a2 = 1}, {b1 = 2}, {b2 = 2}, {c1 = 3}, {c2 = 3}]
|
||||
* // unwrap = [{a = 1}, {b = 2}, {c = 3}]
|
||||
* unwrap.flatMapKey(key -> Stream.of(key + "1", key + "2"));
|
||||
* // unwrap = [{a1 = 1}, {a2 = 1}, {b1 = 2}, {b2 = 2}, {c1 = 3}, {c2 = 3}]
|
||||
* }</pre>
|
||||
*
|
||||
* @param keyMapper 值转映射方法
|
||||
@ -466,9 +466,9 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
* 然后再返回由这些流中所有元素组成的流新{@link EntryStream}串行流。<br>
|
||||
* 效果类似:
|
||||
* <pre>{@code
|
||||
* // stream = [{a = 1}, {b = 2}, {c = 3}]
|
||||
* stream.flatMapValue(num -> Stream.of(num, num+1));
|
||||
* // stream = [{a = 1}, {a = 2}, {b = 2}, {b = 3}, {c = 3}, {c = 4}]
|
||||
* // unwrap = [{a = 1}, {b = 2}, {c = 3}]
|
||||
* unwrap.flatMapValue(num -> Stream.of(num, num+1));
|
||||
* // unwrap = [{a = 1}, {a = 2}, {b = 2}, {b = 3}, {c = 3}, {c = 4}]
|
||||
* }</pre>
|
||||
*
|
||||
* @param valueMapper 值转映射方法
|
||||
@ -755,7 +755,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
|
||||
* @return 实现类
|
||||
*/
|
||||
@Override
|
||||
public EntryStream<K, V> wrapping(final Stream<Map.Entry<K, V>> stream) {
|
||||
public EntryStream<K, V> wrap(final Stream<Map.Entry<K, V>> stream) {
|
||||
return new EntryStream<>(stream);
|
||||
}
|
||||
|
||||
|
@ -73,7 +73,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
*/
|
||||
default <C extends Collection<T>> C toColl(final Supplier<C> collectionFactory) {
|
||||
Objects.requireNonNull(collectionFactory);
|
||||
return stream().collect(Collectors.toCollection(collectionFactory));
|
||||
return unwrap().collect(Collectors.toCollection(collectionFactory));
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -181,7 +181,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
Objects.requireNonNull(valueMapper);
|
||||
Objects.requireNonNull(mergeFunction);
|
||||
Objects.requireNonNull(mapSupplier);
|
||||
return stream().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier));
|
||||
return unwrap().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier));
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -227,7 +227,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
*/
|
||||
default <R> Optional<R> transform(final Function<? super S, R> transform) {
|
||||
Objects.requireNonNull(transform);
|
||||
return Optional.ofNullable(transform.apply(wrapping(this)));
|
||||
return Optional.ofNullable(transform.apply(wrap(this)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -238,7 +238,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
*/
|
||||
default Optional<T> findFirst(final Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
return stream().filter(predicate).findFirst();
|
||||
return unwrap().filter(predicate).findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -253,7 +253,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
return NOT_FOUND_ELEMENT_INDEX;
|
||||
} else {
|
||||
final MutableInt index = new MutableInt(NOT_FOUND_ELEMENT_INDEX);
|
||||
stream().filter(e -> {
|
||||
unwrap().filter(e -> {
|
||||
index.increment();
|
||||
return predicate.test(e);
|
||||
}).findFirst();
|
||||
@ -377,7 +377,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
* @return 拼接后的字符串
|
||||
*/
|
||||
default String join(final CharSequence delimiter, final CharSequence prefix, final CharSequence suffix) {
|
||||
return stream().map(String::valueOf).collect(Collectors.joining(delimiter, prefix, suffix));
|
||||
return unwrap().map(String::valueOf).collect(Collectors.joining(delimiter, prefix, suffix));
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -432,7 +432,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
Objects.requireNonNull(classifier);
|
||||
Objects.requireNonNull(mapFactory);
|
||||
Objects.requireNonNull(downstream);
|
||||
return stream().collect(CollectorUtil.groupingBy(classifier, mapFactory, downstream));
|
||||
return unwrap().collect(CollectorUtil.groupingBy(classifier, mapFactory, downstream));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -440,10 +440,10 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
*
|
||||
* @param predicate 判断条件
|
||||
* @return map
|
||||
* @see #partitioning(Predicate, Collector)
|
||||
* @see #partition(Predicate, Collector)
|
||||
*/
|
||||
default Map<Boolean, List<T>> partitioning(final Predicate<T> predicate) {
|
||||
return this.partitioning(predicate, ArrayList::new);
|
||||
default Map<Boolean, List<T>> partition(final Predicate<T> predicate) {
|
||||
return this.partition(predicate, ArrayList::new);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -452,10 +452,10 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
* @param predicate 判断条件
|
||||
* @param collFactory 提供的集合
|
||||
* @return map
|
||||
* @see #partitioning(Predicate, Collector)
|
||||
* @see #partition(Predicate, Collector)
|
||||
*/
|
||||
default <C extends Collection<T>> Map<Boolean, C> partitioning(final Predicate<T> predicate, final Supplier<C> collFactory) {
|
||||
return this.partitioning(predicate, Collectors.toCollection(collFactory));
|
||||
default <C extends Collection<T>> Map<Boolean, C> partition(final Predicate<T> predicate, final Supplier<C> collFactory) {
|
||||
return this.partition(predicate, Collectors.toCollection(collFactory));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -466,10 +466,10 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
* @param <R> 返回值类型
|
||||
* @return map
|
||||
*/
|
||||
default <R> Map<Boolean, R> partitioning(final Predicate<T> predicate, final Collector<T, ?, R> downstream) {
|
||||
default <R> Map<Boolean, R> partition(final Predicate<T> predicate, final Collector<T, ?, R> downstream) {
|
||||
Objects.requireNonNull(predicate);
|
||||
Objects.requireNonNull(downstream);
|
||||
return stream().collect(Collectors.partitioningBy(predicate, downstream));
|
||||
return unwrap().collect(Collectors.partitioningBy(predicate, downstream));
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -485,10 +485,10 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
default void forEachIdx(final BiConsumer<? super T, Integer> action) {
|
||||
Objects.requireNonNull(action);
|
||||
if (isParallel()) {
|
||||
stream().forEach(e -> action.accept(e, NOT_FOUND_ELEMENT_INDEX));
|
||||
unwrap().forEach(e -> action.accept(e, NOT_FOUND_ELEMENT_INDEX));
|
||||
} else {
|
||||
final MutableInt index = new MutableInt(NOT_FOUND_ELEMENT_INDEX);
|
||||
stream().forEach(e -> action.accept(e, index.incrementAndGet()));
|
||||
unwrap().forEach(e -> action.accept(e, index.incrementAndGet()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -501,10 +501,10 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
||||
default void forEachOrderedIdx(final BiConsumer<? super T, Integer> action) {
|
||||
Objects.requireNonNull(action);
|
||||
if (isParallel()) {
|
||||
stream().forEachOrdered(e -> action.accept(e, NOT_FOUND_ELEMENT_INDEX));
|
||||
unwrap().forEachOrdered(e -> action.accept(e, NOT_FOUND_ELEMENT_INDEX));
|
||||
} else {
|
||||
final MutableInt index = new MutableInt(NOT_FOUND_ELEMENT_INDEX);
|
||||
stream().forEachOrdered(e -> action.accept(e, index.incrementAndGet()));
|
||||
unwrap().forEachOrdered(e -> action.accept(e, index.incrementAndGet()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,7 +57,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
while (keys.tryAdvance(key::set) && values.tryAdvance(value::set)) {
|
||||
list.add(zipper.apply(key.get(), value.get()));
|
||||
}
|
||||
return EasyStream.of(list).parallel(isParallel()).onClose(stream()::close);
|
||||
return EasyStream.of(list).parallel(isParallel()).onClose(unwrap()::close);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,7 +80,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
return EasyStream.iterate(0, i -> i < size, i -> i + batchSize)
|
||||
.map(skip -> EasyStream.of(list.subList(skip, Math.min(size, skip + batchSize)), isParallel()))
|
||||
.parallel(isParallel())
|
||||
.onClose(stream()::close);
|
||||
.onClose(unwrap()::close);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -133,7 +133,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
default S reverse() {
|
||||
final T[] array = (T[]) toArray();
|
||||
ArrayUtil.reverse(array);
|
||||
return wrapping(Stream.of(array)).parallel(isParallel());
|
||||
return wrap(Stream.of(array)).parallel(isParallel());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,8 +156,8 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
* @return 操作后的流
|
||||
*/
|
||||
default S splice(final int start, final int deleteCount, final T... items) {
|
||||
final List<T> elements = stream().collect(Collectors.toList());
|
||||
return wrapping(ListUtil.splice(elements, start, deleteCount, items).stream())
|
||||
final List<T> elements = unwrap().collect(Collectors.toList());
|
||||
return wrap(ListUtil.splice(elements, start, deleteCount, items).stream())
|
||||
.parallel(isParallel());
|
||||
}
|
||||
|
||||
@ -187,7 +187,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
*/
|
||||
default S takeWhile(final Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
return wrapping(StreamUtil.takeWhile(stream(), predicate));
|
||||
return wrap(StreamUtil.takeWhile(unwrap(), predicate));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -216,7 +216,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
*/
|
||||
default S dropWhile(final Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
return wrapping(StreamUtil.dropWhile(stream(), predicate));
|
||||
return wrap(StreamUtil.dropWhile(unwrap(), predicate));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -234,7 +234,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
// 标记是否出现过null值,用于保留第一个出现的null
|
||||
// 由于ConcurrentHashMap的key不能为null,所以用此变量来标记
|
||||
final AtomicBoolean hasNull = new AtomicBoolean(false);
|
||||
return EasyStream.of(stream().filter(e -> {
|
||||
return EasyStream.of(unwrap().filter(e -> {
|
||||
final F key = keyExtractor.apply(e);
|
||||
if (key == null) {
|
||||
// 已经出现过null值,跳过该值
|
||||
@ -250,7 +250,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
})).parallel();
|
||||
} else {
|
||||
final Set<F> exists = new HashSet<>();
|
||||
return EasyStream.of(stream().filter(e -> exists.add(keyExtractor.apply(e))));
|
||||
return EasyStream.of(unwrap().filter(e -> exists.add(keyExtractor.apply(e))));
|
||||
}
|
||||
}
|
||||
|
||||
@ -305,11 +305,11 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
default S push(final T... obj) {
|
||||
Stream<T> result = stream();
|
||||
Stream<T> result = unwrap();
|
||||
if (ArrayUtil.isNotEmpty(obj)) {
|
||||
result = Stream.concat(stream(), Stream.of(obj));
|
||||
result = Stream.concat(unwrap(), Stream.of(obj));
|
||||
}
|
||||
return wrapping(result);
|
||||
return wrap(result);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -319,11 +319,11 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
* @return 流
|
||||
*/
|
||||
default S unshift(final T... obj) {
|
||||
Stream<T> result = stream();
|
||||
Stream<T> result = unwrap();
|
||||
if (ArrayUtil.isNotEmpty(obj)) {
|
||||
result = Stream.concat(Stream.of(obj), stream());
|
||||
result = Stream.concat(Stream.of(obj), unwrap());
|
||||
}
|
||||
return wrapping(result);
|
||||
return wrap(result);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -334,10 +334,10 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
*/
|
||||
default S append(final Iterable<? extends T> iterable) {
|
||||
if (IterUtil.isEmpty(iterable)) {
|
||||
return wrapping(this);
|
||||
return wrap(this);
|
||||
}
|
||||
final Stream<? extends T> contacted = StreamSupport.stream(iterable.spliterator(), isParallel());
|
||||
return wrapping(Stream.concat(this, contacted));
|
||||
return wrap(Stream.concat(this, contacted));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -348,10 +348,10 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
*/
|
||||
default S prepend(final Iterable<? extends T> iterable) {
|
||||
if (IterUtil.isEmpty(iterable)) {
|
||||
return wrapping(this);
|
||||
return wrap(this);
|
||||
}
|
||||
final Stream<? extends T> contacted = StreamSupport.stream(iterable.spliterator(), isParallel());
|
||||
return wrapping(Stream.concat(contacted, this));
|
||||
return wrap(Stream.concat(contacted, this));
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -417,7 +417,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
@Override
|
||||
default <R> EasyStream<R> flatMap(final Function<? super T, ? extends Stream<? extends R>> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return new EasyStream<>(stream().flatMap(mapper));
|
||||
return new EasyStream<>(unwrap().flatMap(mapper));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -493,7 +493,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
.flat(recursiveRef.get())
|
||||
.unshift(e);
|
||||
recursiveRef.set(recursive);
|
||||
return wrapping(flatMap(recursive).peek(e -> childrenSetter.accept(e, null)));
|
||||
return wrap(flatMap(recursive).peek(e -> childrenSetter.accept(e, null)));
|
||||
}
|
||||
|
||||
// endregion
|
||||
@ -511,7 +511,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
||||
@Override
|
||||
default <R> EasyStream<R> map(final Function<? super T, ? extends R> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return new EasyStream<>(stream().map(mapper));
|
||||
return new EasyStream<>(unwrap().map(mapper));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,7 +5,7 @@ import java.util.function.*;
|
||||
import java.util.stream.*;
|
||||
|
||||
/**
|
||||
* <p>{@link Stream}实例的包装器,用于增强原始的{@link Stream},提供一些额外的中间与终端操作 <br>
|
||||
* <p>{@link Stream}实例的包装器,用于增强原始的{@link Stream},提供一些额外的中间与终端操作。 <br>
|
||||
* 默认提供两个可用实现:
|
||||
* <ul>
|
||||
* <li>{@link EasyStream}:针对单元素的通用增强流实现;</li>
|
||||
@ -30,19 +30,20 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
int NOT_FOUND_ELEMENT_INDEX = -1;
|
||||
|
||||
/**
|
||||
* 获取被包装的原始流
|
||||
* 获取被当前实例包装的流对象
|
||||
*
|
||||
* @return 被包装的原始流
|
||||
* @return 被当前实例包装的流对象
|
||||
*/
|
||||
Stream<T> stream();
|
||||
Stream<T> unwrap();
|
||||
|
||||
/**
|
||||
* 将一个原始流包装为指定类型的增强流
|
||||
* 将一个原始流包装为指定类型的增强流 <br>
|
||||
* 若{@code source}于当前实例包装的流并不相同,则该增强流与当前实例无关联关系
|
||||
*
|
||||
* @param source 被包装的流
|
||||
* @return S
|
||||
* @return 包装后的流
|
||||
*/
|
||||
S wrapping(final Stream<T> source);
|
||||
S wrap(final Stream<T> source);
|
||||
|
||||
/**
|
||||
* 过滤元素,返回与指定断言匹配的元素组成的流
|
||||
@ -54,7 +55,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default S filter(final Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
return wrapping(stream().filter(predicate));
|
||||
return wrap(unwrap().filter(predicate));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,7 +68,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default IntStream mapToInt(final ToIntFunction<? super T> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return stream().mapToInt(mapper);
|
||||
return unwrap().mapToInt(mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,7 +81,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default LongStream mapToLong(final ToLongFunction<? super T> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return stream().mapToLong(mapper);
|
||||
return unwrap().mapToLong(mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,7 +94,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default DoubleStream mapToDouble(final ToDoubleFunction<? super T> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return stream().mapToDouble(mapper);
|
||||
return unwrap().mapToDouble(mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,7 +107,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default IntStream flatMapToInt(final Function<? super T, ? extends IntStream> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return stream().flatMapToInt(mapper);
|
||||
return unwrap().flatMapToInt(mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -119,7 +120,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default LongStream flatMapToLong(final Function<? super T, ? extends LongStream> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return stream().flatMapToLong(mapper);
|
||||
return unwrap().flatMapToLong(mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -132,7 +133,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default DoubleStream flatMapToDouble(final Function<? super T, ? extends DoubleStream> mapper) {
|
||||
Objects.requireNonNull(mapper);
|
||||
return stream().flatMapToDouble(mapper);
|
||||
return unwrap().flatMapToDouble(mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -143,7 +144,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S distinct() {
|
||||
return wrapping(stream().distinct());
|
||||
return wrap(unwrap().distinct());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,7 +157,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S sorted() {
|
||||
return wrapping(stream().sorted());
|
||||
return wrap(unwrap().sorted());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -171,7 +172,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default S sorted(final Comparator<? super T> comparator) {
|
||||
Objects.requireNonNull(comparator);
|
||||
return wrapping(stream().sorted(comparator));
|
||||
return wrap(unwrap().sorted(comparator));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -194,7 +195,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default S peek(final Consumer<? super T> action) {
|
||||
Objects.requireNonNull(action);
|
||||
return wrapping(stream().peek(action));
|
||||
return wrap(unwrap().peek(action));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -206,7 +207,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S limit(final long maxSize) {
|
||||
return wrapping(stream().limit(maxSize));
|
||||
return wrap(unwrap().limit(maxSize));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -218,7 +219,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S skip(final long n) {
|
||||
return wrapping(stream().skip(n));
|
||||
return wrap(unwrap().skip(n));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -230,7 +231,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default void forEach(final Consumer<? super T> action) {
|
||||
Objects.requireNonNull(action);
|
||||
stream().forEach(action);
|
||||
unwrap().forEach(action);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -242,7 +243,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default void forEachOrdered(final Consumer<? super T> action) {
|
||||
Objects.requireNonNull(action);
|
||||
stream().forEachOrdered(action);
|
||||
unwrap().forEachOrdered(action);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -253,7 +254,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default Object[] toArray() {
|
||||
return stream().toArray();
|
||||
return unwrap().toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -268,7 +269,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default <A> A[] toArray(final IntFunction<A[]> generator) {
|
||||
Objects.requireNonNull(generator);
|
||||
return stream().toArray(generator);
|
||||
return unwrap().toArray(generator);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -294,7 +295,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default T reduce(final T identity, final BinaryOperator<T> accumulator) {
|
||||
Objects.requireNonNull(accumulator);
|
||||
return stream().reduce(identity, accumulator);
|
||||
return unwrap().reduce(identity, accumulator);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -303,7 +304,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
* <pre>{@code
|
||||
* boolean foundAny = false;
|
||||
* T result = null;
|
||||
* for (T element : this stream) {
|
||||
* for (T element : this unwrap) {
|
||||
* if (!foundAny) {
|
||||
* foundAny = true;
|
||||
* result = element;
|
||||
@ -330,7 +331,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default Optional<T> reduce(final BinaryOperator<T> accumulator) {
|
||||
Objects.requireNonNull(accumulator);
|
||||
return stream().reduce(accumulator);
|
||||
return unwrap().reduce(accumulator);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -349,7 +350,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
default <U> U reduce(final U identity, final BiFunction<U, ? super T, U> accumulator, final BinaryOperator<U> combiner) {
|
||||
Objects.requireNonNull(accumulator);
|
||||
Objects.requireNonNull(combiner);
|
||||
return stream().reduce(identity, accumulator, combiner);
|
||||
return unwrap().reduce(identity, accumulator, combiner);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -370,7 +371,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
Objects.requireNonNull(supplier);
|
||||
Objects.requireNonNull(accumulator);
|
||||
Objects.requireNonNull(combiner);
|
||||
return stream().collect(supplier, accumulator, combiner);
|
||||
return unwrap().collect(supplier, accumulator, combiner);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -385,7 +386,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default <R, A> R collect(final Collector<? super T, A, R> collector) {
|
||||
Objects.requireNonNull(collector);
|
||||
return stream().collect(collector);
|
||||
return unwrap().collect(collector);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -397,7 +398,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default Optional<T> min(final Comparator<? super T> comparator) {
|
||||
Objects.requireNonNull(comparator);
|
||||
return stream().min(comparator);
|
||||
return unwrap().min(comparator);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -409,7 +410,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default Optional<T> max(final Comparator<? super T> comparator) {
|
||||
Objects.requireNonNull(comparator);
|
||||
return stream().max(comparator);
|
||||
return unwrap().max(comparator);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -419,7 +420,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default long count() {
|
||||
return stream().count();
|
||||
return unwrap().count();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -431,7 +432,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default boolean anyMatch(final Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
return stream().anyMatch(predicate);
|
||||
return unwrap().anyMatch(predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -443,7 +444,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default boolean allMatch(final Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
return stream().allMatch(predicate);
|
||||
return unwrap().allMatch(predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -455,7 +456,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
@Override
|
||||
default boolean noneMatch(final Predicate<? super T> predicate) {
|
||||
Objects.requireNonNull(predicate);
|
||||
return stream().noneMatch(predicate);
|
||||
return unwrap().noneMatch(predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -465,7 +466,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default Optional<T> findFirst() {
|
||||
return stream().findFirst();
|
||||
return unwrap().findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -475,7 +476,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default Optional<T> findAny() {
|
||||
return stream().findAny();
|
||||
return unwrap().findAny();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -485,7 +486,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default Iterator<T> iterator() {
|
||||
return stream().iterator();
|
||||
return unwrap().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -495,7 +496,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default Spliterator<T> spliterator() {
|
||||
return stream().spliterator();
|
||||
return unwrap().spliterator();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -505,7 +506,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default boolean isParallel() {
|
||||
return stream().isParallel();
|
||||
return unwrap().isParallel();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -515,7 +516,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S sequential() {
|
||||
return wrapping(stream().sequential());
|
||||
return wrap(unwrap().sequential());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -525,7 +526,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S parallel() {
|
||||
return wrapping(stream().parallel());
|
||||
return wrap(unwrap().parallel());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -536,7 +537,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S unordered() {
|
||||
return wrapping(stream().unordered());
|
||||
return wrap(unwrap().unordered());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -547,7 +548,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default S onClose(Runnable closeHandler) {
|
||||
return wrapping(stream().onClose(closeHandler));
|
||||
return wrap(unwrap().onClose(closeHandler));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -557,7 +558,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
|
||||
*/
|
||||
@Override
|
||||
default void close() {
|
||||
stream().close();
|
||||
unwrap().close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -172,9 +172,9 @@ public class AbstractEnhancedWrappedStreamTest {
|
||||
put(Boolean.FALSE, asList(1, 3));
|
||||
}};
|
||||
|
||||
Map<Boolean, List<Integer>> partition = wrap(list).partitioning(t -> (t & 1) == 0, Collectors.toList());
|
||||
Map<Boolean, List<Integer>> partition = wrap(list).partition(t -> (t & 1) == 0, Collectors.toList());
|
||||
Assert.assertEquals(map, partition);
|
||||
partition = wrap(list).partitioning(t -> (t & 1) == 0);
|
||||
partition = wrap(list).partition(t -> (t & 1) == 0);
|
||||
Assert.assertEquals(map, partition);
|
||||
}
|
||||
|
||||
@ -681,14 +681,14 @@ public class AbstractEnhancedWrappedStreamTest {
|
||||
* 创建一个流包装器
|
||||
*
|
||||
* @param stream 包装的流对象
|
||||
* @throws NullPointerException 当{@code stream}为{@code null}时抛出
|
||||
* @throws NullPointerException 当{@code unwrap}为{@code null}时抛出
|
||||
*/
|
||||
protected Wrapper(Stream<T> stream) {
|
||||
super(stream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Wrapper<T> wrapping(Stream<T> source) {
|
||||
public Wrapper<T> wrap(Stream<T> source) {
|
||||
return new Wrapper<>(source);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user