This commit is contained in:
huangchengxing 2022-09-06 12:21:41 +08:00
parent 741f0aa53a
commit 717305e039
7 changed files with 139 additions and 126 deletions

View File

@ -35,7 +35,7 @@ public abstract class AbstractEnhancedWrappedStream<T, S extends AbstractEnhance
* @param stream 包装的流对象
* @throws NullPointerException {@code stream}{@code null}时抛出
*/
protected AbstractEnhancedWrappedStream(Stream<T> stream) {
protected AbstractEnhancedWrappedStream(final Stream<T> stream) {
this.stream = Objects.requireNonNull(stream, "stream must not null");
}
@ -56,7 +56,7 @@ public abstract class AbstractEnhancedWrappedStream<T, S extends AbstractEnhance
* @return 是否相等
*/
@Override
public boolean equals(Object obj) {
public boolean equals(final Object obj) {
return obj instanceof Stream && stream.equals(obj);
}

View File

@ -262,7 +262,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
* @return 实现类
*/
@Override
public EasyStream<T> wrapping(Stream<T> stream) {
public EasyStream<T> wrapping(final Stream<T> stream) {
return new EasyStream<>(stream);
}
@ -281,10 +281,11 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
* toTree(Student::getId, Student::getParentId, Student::setChildren);
* }</pre>
*/
public <R extends Comparable<R>> List<T> toTree(Function<T, R> idGetter,
Function<T, R> pIdGetter,
BiConsumer<T, List<T>> childrenSetter) {
Map<R, List<T>> pIdValuesMap = group(pIdGetter);
public <R extends Comparable<R>> List<T> toTree(
final Function<T, R> idGetter,
final Function<T, R> pIdGetter,
final BiConsumer<T, List<T>> childrenSetter) {
final Map<R, List<T>> pIdValuesMap = group(pIdGetter);
return getChildrenFromMapByPidAndSet(idGetter, childrenSetter, pIdValuesMap, pIdValuesMap.get(null));
}
@ -305,13 +306,14 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
* }</pre>
*/
public <R extends Comparable<R>> List<T> toTree(Function<T, R> idGetter,
Function<T, R> pIdGetter,
BiConsumer<T, List<T>> childrenSetter,
Predicate<T> parentPredicate) {
public <R extends Comparable<R>> List<T> toTree(
final Function<T, R> idGetter,
final Function<T, R> pIdGetter,
final BiConsumer<T, List<T>> childrenSetter,
final Predicate<T> parentPredicate) {
Objects.requireNonNull(parentPredicate);
List<T> list = toList();
List<T> parents = EasyStream.of(list).filter(e ->
final List<T> list = toList();
final List<T> parents = EasyStream.of(list).filter(e ->
// 此处是为了适配 parentPredicate.test空指针 情况
// 因为Predicate.test的返回值是boolean所以如果 e -> null 这种返回null的情况会直接抛出NPE
Opt.ofTry(() -> parentPredicate.test(e)).filter(Boolean::booleanValue).isPresent())
@ -330,15 +332,16 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
* @param <R> 此处是id的泛型限制
* @return list 组装好的树
*/
private <R extends Comparable<R>> List<T> getChildrenFromMapByPidAndSet(Function<T, R> idGetter,
BiConsumer<T, List<T>> childrenSetter,
Map<R, List<T>> pIdValuesMap,
List<T> parents) {
private <R extends Comparable<R>> List<T> getChildrenFromMapByPidAndSet(
final Function<T, R> idGetter,
final BiConsumer<T, List<T>> childrenSetter,
final Map<R, List<T>> pIdValuesMap,
final List<T> parents) {
Objects.requireNonNull(idGetter);
Objects.requireNonNull(childrenSetter);
Objects.requireNonNull(pIdValuesMap);
MutableObj<Consumer<List<T>>> recursiveRef = new MutableObj<>();
Consumer<List<T>> recursive = values -> EasyStream.of(values, isParallel()).forEach(value -> {
final MutableObj<Consumer<List<T>>> recursiveRef = new MutableObj<>();
final Consumer<List<T>> recursive = values -> EasyStream.of(values, isParallel()).forEach(value -> {
List<T> children = pIdValuesMap.get(idGetter.apply(value));
childrenSetter.accept(value, children);
recursiveRef.get().accept(children);

View File

@ -38,7 +38,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param values 值集合
* @return {@link EntryStream}实例
*/
public static <A, B> EntryStream<A, B> merge(Iterable<A> keys, Iterable<B> values) {
public static <A, B> EntryStream<A, B> merge(final Iterable<A> keys, final Iterable<B> values) {
final boolean hasKeys = ObjUtil.isNotNull(keys);
final boolean hasValues = ObjUtil.isNotNull(values);
// 皆为空
@ -75,7 +75,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <B> 值类型
* @return {@link EntryStream}实例
*/
public static <A, B> EntryStream<A, B> of(Map<A, B> map) {
public static <A, B> EntryStream<A, B> of(final Map<A, B> map) {
return ObjUtil.isNull(map) ?
empty() : of(map.entrySet());
}
@ -90,7 +90,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <B> 值类型
* @return {@link EntryStream}实例
*/
public static <A, B> EntryStream<A, B> of(Iterable<? extends Map.Entry<A, B>> entries) {
public static <A, B> EntryStream<A, B> of(final Iterable<? extends Map.Entry<A, B>> entries) {
return ObjUtil.isNull(entries) ?
empty() : of(StreamSupport.stream(entries.spliterator(), false));
}
@ -106,7 +106,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return {@link EntryStream}实例
*/
public static <T, A, B> EntryStream<A, B> of(
Iterable<T> source, Function<? super T, ? extends A> keyMapper, Function<? super T, ? extends B> valueMapper) {
final Iterable<T> source, final Function<? super T, ? extends A> keyMapper, final Function<? super T, ? extends B> valueMapper) {
Objects.requireNonNull(keyMapper);
Objects.requireNonNull(valueMapper);
if (ObjUtil.isNull(source)) {
@ -126,7 +126,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <B> 值类型
* @return {@link EntryStream}实例
*/
public static <A, B> EntryStream<A, B> of(Stream<? extends Map.Entry<A, B>> stream) {
public static <A, B> EntryStream<A, B> of(final Stream<? extends Map.Entry<A, B>> stream) {
return ObjUtil.isNull(stream) ?
empty() : new EntryStream<>(stream.map(EntryStream::ofEntry));
}
@ -145,7 +145,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
/**
* 构造
*/
EntryStream(Stream<Map.Entry<K, V>> stream) {
EntryStream(final Stream<Map.Entry<K, V>> stream) {
super(stream);
}
@ -157,9 +157,10 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> distinctByKey() {
Set<K> accessed = new ConcurrentHashSet<>(16);
// FIXME fix happen NPE when has null key
final Set<K> accessed = new ConcurrentHashSet<>(16);
return wrapping(stream.filter(e -> {
K key = e.getKey();
final K key = e.getKey();
if (accessed.contains(key)) {
return false;
}
@ -174,9 +175,10 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> distinctByValue() {
Set<V> accessed = new ConcurrentHashSet<>(16);
// FIXME fix happen NPE when has null value
final Set<V> accessed = new ConcurrentHashSet<>(16);
return wrapping(stream.filter(e -> {
V val = e.getValue();
final V val = e.getValue();
if (accessed.contains(val)) {
return false;
}
@ -191,7 +193,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param filter 判断条件
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> filter(BiPredicate<? super K, ? super V> filter) {
public EntryStream<K, V> filter(final BiPredicate<? super K, ? super V> filter) {
Objects.requireNonNull(filter);
return super.filter(e -> filter.test(e.getKey(), e.getValue()));
}
@ -202,7 +204,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param filter 判断条件
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> filterByKey(Predicate<? super K> filter) {
public EntryStream<K, V> filterByKey(final Predicate<? super K> filter) {
Objects.requireNonNull(filter);
return super.filter(e -> filter.test(e.getKey()));
}
@ -213,7 +215,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param filter 判断条件
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> filterByValue(Predicate<? super V> filter) {
public EntryStream<K, V> filterByValue(final Predicate<? super V> filter) {
Objects.requireNonNull(filter);
return super.filter(e -> filter.test(e.getValue()));
}
@ -251,7 +253,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param consumer 操作
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> peekKey(Consumer<? super K> consumer) {
public EntryStream<K, V> peekKey(final Consumer<? super K> consumer) {
Objects.requireNonNull(consumer);
return super.peek(e -> consumer.accept(e.getKey()));
}
@ -262,7 +264,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param consumer 操作
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> peekValue(Consumer<? super V> consumer) {
public EntryStream<K, V> peekValue(final Consumer<? super V> consumer) {
Objects.requireNonNull(consumer);
return super.peek(e -> consumer.accept(e.getValue()));
}
@ -273,7 +275,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param comparator 排序器
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> sortByKey(Comparator<? super K> comparator) {
public EntryStream<K, V> sortByKey(final Comparator<? super K> comparator) {
Objects.requireNonNull(comparator);
return sorted(Map.Entry.comparingByKey(comparator));
}
@ -284,7 +286,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param comparator 排序器
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> sortByValue(Comparator<? super V> comparator) {
public EntryStream<K, V> sortByValue(final Comparator<? super V> comparator) {
Objects.requireNonNull(comparator);
return sorted(Map.Entry.comparingByValue(comparator));
}
@ -298,7 +300,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param value
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> push(K key, V value) {
public EntryStream<K, V> push(final K key, final V value) {
return wrapping(Stream.concat(stream, Stream.of(ofEntry(key, value))));
}
@ -309,7 +311,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param value
* @return {@link EntryStream}实例
*/
public EntryStream<K, V> unshift(K key, V value) {
public EntryStream<K, V> unshift(final K key, final V value) {
return wrapping(Stream.concat(Stream.of(ofEntry(key, value)), stream));
}
@ -320,7 +322,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return {@link EntryStream}实例
*/
@Override
public EntryStream<K, V> append(Iterable<? extends Map.Entry<K, V>> entries) {
public EntryStream<K, V> append(final Iterable<? extends Map.Entry<K, V>> entries) {
if (IterUtil.isEmpty(entries)) {
return this;
}
@ -336,7 +338,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return {@link EntryStream}实例
*/
@Override
public EntryStream<K, V> prepend(Iterable<? extends Map.Entry<K, V>> entries) {
public EntryStream<K, V> prepend(final Iterable<? extends Map.Entry<K, V>> entries) {
if (IterUtil.isEmpty(entries)) {
return this;
}
@ -370,7 +372,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <N> 新的键类型
* @return {@link EntryStream}实例
*/
public <N> EntryStream<N, V> mapKeys(Function<? super K, ? extends N> mapper) {
public <N> EntryStream<N, V> mapKeys(final Function<? super K, ? extends N> mapper) {
Objects.requireNonNull(mapper);
return new EntryStream<>(
stream.map(e -> ofEntry(mapper.apply(e.getKey()), e.getValue()))
@ -384,7 +386,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <N> 新的值类型
* @return {@link EntryStream}实例
*/
public <N> EntryStream<K, N> mapValues(Function<? super V, ? extends N> mapper) {
public <N> EntryStream<K, N> mapValues(final Function<? super V, ? extends N> mapper) {
Objects.requireNonNull(mapper);
return new EntryStream<>(
stream.map(e -> ofEntry(e.getKey(), mapper.apply(e.getValue())))
@ -400,7 +402,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 返回叠加操作后的流
*/
@Override
public <R> EasyStream<R> map(Function<? super Map.Entry<K, V>, ? extends R> mapper) {
public <R> EasyStream<R> map(final Function<? super Map.Entry<K, V>, ? extends R> mapper) {
Objects.requireNonNull(mapper);
return EasyStream.of(stream.map(mapper));
}
@ -412,7 +414,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <N> 函数执行后返回流中元素的类型
* @return 映射后的单对象组成的流
*/
public <N> EasyStream<N> map(BiFunction<? super K, ? super V, ? extends N> mapper) {
public <N> EasyStream<N> map(final BiFunction<? super K, ? super V, ? extends N> mapper) {
Objects.requireNonNull(mapper);
return EasyStream.of(stream.map(e -> mapper.apply(e.getKey(), e.getValue())));
}
@ -430,7 +432,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 返回叠加拆分操作后的流
*/
@Override
public <R> EasyStream<R> flatMap(Function<? super Map.Entry<K, V>, ? extends Stream<? extends R>> mapper) {
public <R> EasyStream<R> flatMap(final Function<? super Map.Entry<K, V>, ? extends Stream<? extends R>> mapper) {
Objects.requireNonNull(mapper);
return EasyStream.of(stream.flatMap(mapper));
}
@ -449,7 +451,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <N> 新的键类型
* @return 返回叠加拆分操作后的流
*/
public <N> EntryStream<N, V> flatMapKey(Function<? super K, Stream<? extends N>> keyMapper) {
public <N> EntryStream<N, V> flatMapKey(final Function<? super K, Stream<? extends N>> keyMapper) {
Objects.requireNonNull(keyMapper);
return new EntryStream<>(
stream.flatMap(e -> keyMapper
@ -473,7 +475,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <N> 新的值类型
* @return 返回叠加拆分操作后的流
*/
public <N> EntryStream<K, N> flatMapValue(Function<? super V, Stream<? extends N>> valueMapper) {
public <N> EntryStream<K, N> flatMapValue(final Function<? super V, Stream<? extends N>> valueMapper) {
Objects.requireNonNull(valueMapper);
return new EntryStream<>(
stream.flatMap(e -> valueMapper
@ -493,7 +495,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 集合
* @see Collectors#toMap(Function, Function, BinaryOperator, Supplier)
*/
public Map<K, V> toMap(Supplier<Map<K, V>> mapFactory, BinaryOperator<V> operator) {
public Map<K, V> toMap(final Supplier<Map<K, V>> mapFactory, final BinaryOperator<V> operator) {
Objects.requireNonNull(mapFactory);
Objects.requireNonNull(operator);
return super.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, operator, mapFactory));
@ -506,7 +508,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 集合
* @see Collectors#toMap(Function, Function, BinaryOperator)
*/
public Map<K, V> toMap(Supplier<Map<K, V>> mapFactory) {
public Map<K, V> toMap(final Supplier<Map<K, V>> mapFactory) {
Objects.requireNonNull(mapFactory);
return super.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (t1, t2) -> t2, mapFactory));
}
@ -533,7 +535,9 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @see Collectors#groupingBy(Function, Supplier, Collector)
*/
public <N> Table<N, K, V> toTable(
BiFunction<? super K, ? super V, ? extends N> rowKeyMapper, Supplier<Map<K, V>> colMapFactory, BinaryOperator<V> operator) {
final BiFunction<? super K, ? super V, ? extends N> rowKeyMapper,
final Supplier<Map<K, V>> colMapFactory,
final BinaryOperator<V> operator) {
Objects.requireNonNull(rowKeyMapper);
Objects.requireNonNull(colMapFactory);
Objects.requireNonNull(operator);
@ -553,7 +557,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 集合
* @throws IllegalArgumentException 当父集合或子集合中的键重复时抛出
*/
public <N> Table<N, K, V> toTable(BiFunction<? super K, ? super V, ? extends N> rowKeyMapper) {
public <N> Table<N, K, V> toTable(final BiFunction<? super K, ? super V, ? extends N> rowKeyMapper) {
return toTable(rowKeyMapper, HashMap::new, throwingMerger());
}
@ -567,7 +571,9 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 集合
*/
public <N> Table<N, K, V> toTableByKey(
Function<? super K, ? extends N> rowKeyMapper, Supplier<Map<K, V>> colMapFactory, BinaryOperator<V> operator) {
final Function<? super K, ? extends N> rowKeyMapper,
final Supplier<Map<K, V>> colMapFactory,
final BinaryOperator<V> operator) {
return toTable((k, v) -> rowKeyMapper.apply(k), colMapFactory, operator);
}
@ -579,7 +585,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 集合
* @throws IllegalArgumentException 当父集合或子集合中的键重复时抛出
*/
public <N> Table<N, K, V> toTableByKey(Function<? super K, ? extends N> rowKeyMapper) {
public <N> Table<N, K, V> toTableByKey(final Function<? super K, ? extends N> rowKeyMapper) {
return toTable((k, v) -> rowKeyMapper.apply(k));
}
@ -593,7 +599,9 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 集合
*/
public <N> Table<N, K, V> toTableByValue(
Function<? super V, ? extends N> rowKeyMapper, Supplier<Map<K, V>> colMapFactory, BinaryOperator<V> operator) {
final Function<? super V, ? extends N> rowKeyMapper,
final Supplier<Map<K, V>> colMapFactory,
final BinaryOperator<V> operator) {
return toTable((k, v) -> rowKeyMapper.apply(v), colMapFactory, operator);
}
@ -605,7 +613,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 集合
* @throws IllegalArgumentException 当父集合或子集合中的键重复时抛出
*/
public <N> Table<N, K, V> toTableByValue(Function<? super V, ? extends N> rowKeyMapper) {
public <N> Table<N, K, V> toTableByValue(final Function<? super V, ? extends N> rowKeyMapper) {
return toTable((k, v) -> rowKeyMapper.apply(v));
}
@ -625,7 +633,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <C> 值集合的类型
* @return 集合
*/
public <C extends Collection<V>> Map<K, C> groupByKey(Collector<V, ?, C> collector) {
public <C extends Collection<V>> Map<K, C> groupByKey(final Collector<V, ?, C> collector) {
return groupByKey((Supplier<Map<K,C>>)HashMap::new, collector);
}
@ -638,7 +646,8 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <M> 返回的map集合类型
* @return 集合
*/
public <C extends Collection<V>, M extends Map<K, C>> M groupByKey(Supplier<M> mapFactory, Collector<V, ?, C> collector) {
public <C extends Collection<V>, M extends Map<K, C>> M groupByKey(
final Supplier<M> mapFactory, final Collector<V, ?, C> collector) {
return super.collect(Collectors.groupingBy(
Map.Entry::getKey, mapFactory,
CollectorUtil.transform(ArrayList::new, s -> s.stream().map(Map.Entry::getValue).collect(collector))
@ -650,7 +659,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
*
* @param consumer 操作
*/
public void forEach(BiConsumer<K, V> consumer) {
public void forEach(final BiConsumer<K, V> consumer) {
Objects.requireNonNull(consumer);
super.forEach(e -> consumer.accept(e.getKey(), e.getValue()));
}
@ -673,7 +682,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <R> 返回值类型
* @return 收集容器
*/
public <R> R collectKeys(Collector<K, ?, R> collector) {
public <R> R collectKeys(final Collector<K, ?, R> collector) {
return toKeyStream().collect(collector);
}
@ -684,7 +693,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param <R> 返回值类型
* @return 收集容器
*/
public <R> R collectValues(Collector<V, ?, R> collector) {
public <R> R collectValues(final Collector<V, ?, R> collector) {
return toValueStream().collect(collector);
}
@ -694,7 +703,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param predicate 判断条件
* @return 是否
*/
public boolean anyMatch(BiPredicate<? super K, ? super V> predicate) {
public boolean anyMatch(final BiPredicate<? super K, ? super V> predicate) {
return super.anyMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
@ -704,7 +713,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param predicate 判断条件
* @return 是否
*/
public boolean allMatch(BiPredicate<? super K, ? super V> predicate) {
public boolean allMatch(final BiPredicate<? super K, ? super V> predicate) {
Objects.requireNonNull(predicate);
return super.allMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
@ -715,7 +724,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @param predicate 判断条件
* @return 是否
*/
public boolean noneMatch(BiPredicate<? super K, ? super V> predicate) {
public boolean noneMatch(final BiPredicate<? super K, ? super V> predicate) {
Objects.requireNonNull(predicate);
return super.noneMatch(e -> predicate.test(e.getKey(), e.getValue()));
}
@ -726,7 +735,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* 将键值对转为{@link AbstractMap.SimpleImmutableEntry}
*/
@SuppressWarnings("unchecked")
static <K, V> Map.Entry<K, V> ofEntry(Map.Entry<K, V> entry) {
static <K, V> Map.Entry<K, V> ofEntry(final Map.Entry<K, V> entry) {
return ObjUtil.defaultIfNull(
entry, e -> ofEntry(e.getKey(), e.getValue()), (Map.Entry<K, V>)EMPTY_ENTRY
);
@ -735,7 +744,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
/**
* 将键值对转为{@link AbstractMap.SimpleImmutableEntry}
*/
static <K, V> Map.Entry<K, V> ofEntry(K key, V value) {
static <K, V> Map.Entry<K, V> ofEntry(final K key, final V value) {
return new AbstractMap.SimpleImmutableEntry<>(key, value);
}
@ -746,7 +755,7 @@ public class EntryStream<K, V> extends AbstractEnhancedWrappedStream<Map.Entry<K
* @return 实现类
*/
@Override
public EntryStream<K, V> wrapping(Stream<Map.Entry<K, V>> stream) {
public EntryStream<K, V> wrapping(final Stream<Map.Entry<K, V>> stream) {
return new EntryStream<>(stream);
}

View File

@ -71,7 +71,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @param <C> 集合类型
* @return 集合
*/
default <C extends Collection<T>> C toColl(Supplier<C> collectionFactory) {
default <C extends Collection<T>> C toColl(final Supplier<C> collectionFactory) {
Objects.requireNonNull(collectionFactory);
return stream().collect(Collectors.toCollection(collectionFactory));
}
@ -88,7 +88,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @return map
* @see #toMap(Function, Function, BinaryOperator, Supplier)
*/
default <K> Map<K, T> toMap(Function<? super T, ? extends K> keyMapper) {
default <K> Map<K, T> toMap(final Function<? super T, ? extends K> keyMapper) {
return this.toMap(keyMapper, Function.identity());
}
@ -103,7 +103,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @see #toMap(Function, Function, BinaryOperator, Supplier)
*/
default <K, U> Map<K, U> toMap(
Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
final Function<? super T, ? extends K> keyMapper, final Function<? super T, ? extends U> valueMapper) {
return this.toMap(keyMapper, valueMapper, (l, r) -> r);
}
@ -118,7 +118,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @see #toMap(Function, Function, BinaryOperator, Supplier)
*/
default <K, U> Map<K, U> toUnmodifiableMap(
Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
final Function<? super T, ? extends K> keyMapper, final Function<? super T, ? extends U> valueMapper) {
return Collections.unmodifiableMap(this.toMap(keyMapper, valueMapper));
}
@ -134,9 +134,9 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @see #toMap(Function, Function, BinaryOperator, Supplier)
*/
default <K, U> Map<K, U> toMap(
Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
final Function<? super T, ? extends K> keyMapper,
final Function<? super T, ? extends U> valueMapper,
final BinaryOperator<U> mergeFunction) {
return this.toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
}
@ -152,9 +152,9 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @see #toMap(Function, Function, BinaryOperator, Supplier)
*/
default <K, U> Map<K, U> toUnmodifiableMap(
Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction) {
final Function<? super T, ? extends K> keyMapper,
final Function<? super T, ? extends U> valueMapper,
final BinaryOperator<U> mergeFunction) {
return Collections.unmodifiableMap(
this.toMap(keyMapper, valueMapper, mergeFunction, HashMap::new)
);
@ -173,9 +173,9 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @return map
*/
default <K, U, M extends Map<K, U>> M toMap(
Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
final Function<? super T, ? extends K> keyMapper,
final Function<? super T, ? extends U> valueMapper,
final BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier) {
Objects.requireNonNull(keyMapper);
Objects.requireNonNull(valueMapper);
@ -198,12 +198,12 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* 至少包含全部的key如果对应位置上的value不存在则为null;<br>
* 如果key重复, 则保留最后一个关联的value;<br>
*/
default <R> Map<T, R> toZip(Iterable<R> other) {
default <R> Map<T, R> toZip(final Iterable<R> other) {
Objects.requireNonNull(other);
// value对象迭代器
final Iterator<R> iterator = Opt.ofNullable(other).map(Iterable::iterator).orElseGet(Collections::emptyIterator);
if (this.isParallel()) {
List<T> keyList = toList();
final List<T> keyList = toList();
final Map<T, R> map = new HashMap<>(keyList.size());
for (T key : keyList) {
map.put(key, iterator.hasNext() ? iterator.next() : null);
@ -364,7 +364,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @return 拼接后的字符串
* @see #join(CharSequence, CharSequence, CharSequence)
*/
default String join(CharSequence delimiter) {
default String join(final CharSequence delimiter) {
return this.join(delimiter, "", "");
}
@ -376,7 +376,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @param suffix 后缀
* @return 拼接后的字符串
*/
default String join(CharSequence delimiter, CharSequence prefix, CharSequence suffix) {
default String join(final CharSequence delimiter, final CharSequence prefix, final CharSequence suffix) {
return stream().map(String::valueOf).collect(Collectors.joining(delimiter, prefix, suffix));
}
@ -392,7 +392,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @return map
* @see #group(Function, Supplier, Collector)
*/
default <K> Map<K, List<T>> group(Function<? super T, ? extends K> classifier) {
default <K> Map<K, List<T>> group(final Function<? super T, ? extends K> classifier) {
return this.group(classifier, Collectors.toList());
}
@ -408,7 +408,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @see #group(Function, Supplier, Collector)
*/
default <K, A, D> Map<K, D> group(
Function<? super T, ? extends K> classifier, Collector<? super T, A, D> downstream) {
final Function<? super T, ? extends K> classifier, final Collector<? super T, A, D> downstream) {
return this.group(classifier, HashMap::new, downstream);
}
@ -426,9 +426,9 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @see CollectorUtil#groupingBy(Function, Supplier, Collector)
*/
default <K, D, A, M extends Map<K, D>> M group(
Function<? super T, ? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T, A, D> downstream) {
final Function<? super T, ? extends K> classifier,
final Supplier<M> mapFactory,
final Collector<? super T, A, D> downstream) {
Objects.requireNonNull(classifier);
Objects.requireNonNull(mapFactory);
Objects.requireNonNull(downstream);
@ -442,7 +442,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @return map
* @see #partitioning(Predicate, Collector)
*/
default Map<Boolean, List<T>> partitioning(Predicate<T> predicate) {
default Map<Boolean, List<T>> partitioning(final Predicate<T> predicate) {
return this.partitioning(predicate, ArrayList::new);
}
@ -454,7 +454,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @return map
* @see #partitioning(Predicate, Collector)
*/
default <C extends Collection<T>> Map<Boolean, C> partitioning(Predicate<T> predicate, Supplier<C> collFactory) {
default <C extends Collection<T>> Map<Boolean, C> partitioning(final Predicate<T> predicate, final Supplier<C> collFactory) {
return this.partitioning(predicate, Collectors.toCollection(collFactory));
}
@ -466,7 +466,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
* @param <R> 返回值类型
* @return map
*/
default <R> Map<Boolean, R> partitioning(Predicate<T> predicate, Collector<T, ?, R> downstream) {
default <R> Map<Boolean, R> partitioning(final Predicate<T> predicate, final Collector<T, ?, R> downstream) {
Objects.requireNonNull(predicate);
Objects.requireNonNull(downstream);
return stream().collect(Collectors.partitioningBy(predicate, downstream));

View File

@ -39,7 +39,8 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
* @param <R> 合并后的结果对象类型
* @return 合并后的结果对象的流
*/
default <U, R> EasyStream<R> zip(final Iterable<U> other,
default <U, R> EasyStream<R> zip(
final Iterable<U> other,
final BiFunction<? super T, ? super U, ? extends R> zipper) {
Objects.requireNonNull(zipper);
final Spliterator<T> keys = spliterator();
@ -104,7 +105,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
* @param <V> 值类型
* @return {@link EntryStream}实例
*/
default <K, V> EntryStream<K, V> toEntries(Function<T, K> keyMapper, Function<T, V> valueMapper) {
default <K, V> EntryStream<K, V> toEntries(final Function<T, K> keyMapper, final Function<T, V> valueMapper) {
Objects.requireNonNull(keyMapper);
Objects.requireNonNull(valueMapper);
return new EntryStream<>(map(t -> EntryStream.ofEntry(keyMapper.apply(t), valueMapper.apply(t))));
@ -117,7 +118,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
* @param <K> 键类型
* @return {@link EntryStream}实例
*/
default <K> EntryStream<K, T> toEntries(Function<T, K> keyMapper) {
default <K> EntryStream<K, T> toEntries(final Function<T, K> keyMapper) {
return toEntries(keyMapper, Function.identity());
}
@ -273,7 +274,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
* .collect(Collectors.toList());
* }</pre>
*/
default S peekIdx(BiConsumer<? super T, Integer> action) {
default S peekIdx(final BiConsumer<? super T, Integer> action) {
Objects.requireNonNull(action);
if (isParallel()) {
return peek(e -> action.accept(e, NOT_FOUND_ELEMENT_INDEX));
@ -331,7 +332,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
* @param iterable 集合
* @return {@link EntryStream}实例
*/
default S append(Iterable<? extends T> iterable) {
default S append(final Iterable<? extends T> iterable) {
if (IterUtil.isEmpty(iterable)) {
return wrapping(this);
}
@ -345,7 +346,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
* @param iterable 集合
* @return {@link EntryStream}实例
*/
default S prepend(Iterable<? extends T> iterable) {
default S prepend(final Iterable<? extends T> iterable) {
if (IterUtil.isEmpty(iterable)) {
return wrapping(this);
}
@ -484,11 +485,11 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
* @param childrenSetter 设置子节点的lambda可以写作 {@code Student::setChildren}
* @return EasyStream 一个流
*/
default S flatTree(Function<T, List<T>> childrenGetter, BiConsumer<T, List<T>> childrenSetter) {
default S flatTree(final Function<T, List<T>> childrenGetter, final BiConsumer<T, List<T>> childrenSetter) {
Objects.requireNonNull(childrenGetter);
Objects.requireNonNull(childrenSetter);
MutableObj<Function<T, EasyStream<T>>> recursiveRef = new MutableObj<>();
Function<T, EasyStream<T>> recursive = e -> EasyStream.of(childrenGetter.apply(e))
final MutableObj<Function<T, EasyStream<T>>> recursiveRef = new MutableObj<>();
final Function<T, EasyStream<T>> recursive = e -> EasyStream.of(childrenGetter.apply(e))
.flat(recursiveRef.get())
.unshift(e);
recursiveRef.set(recursive);

View File

@ -42,7 +42,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @param source 被包装的流
* @return S
*/
S wrapping(Stream<T> source);
S wrapping(final Stream<T> source);
/**
* 过滤元素返回与指定断言匹配的元素组成的流
@ -52,7 +52,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 返回叠加过滤操作后的流
*/
@Override
default S filter(Predicate<? super T> predicate) {
default S filter(final Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
return wrapping(stream().filter(predicate));
}
@ -65,7 +65,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 叠加操作后元素类型全为int的流
*/
@Override
default IntStream mapToInt(ToIntFunction<? super T> mapper) {
default IntStream mapToInt(final ToIntFunction<? super T> mapper) {
Objects.requireNonNull(mapper);
return stream().mapToInt(mapper);
}
@ -78,7 +78,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 叠加操作后元素类型全为long的流
*/
@Override
default LongStream mapToLong(ToLongFunction<? super T> mapper) {
default LongStream mapToLong(final ToLongFunction<? super T> mapper) {
Objects.requireNonNull(mapper);
return stream().mapToLong(mapper);
}
@ -91,7 +91,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 叠加操作后元素类型全为double的流
*/
@Override
default DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) {
default DoubleStream mapToDouble(final ToDoubleFunction<? super T> mapper) {
Objects.requireNonNull(mapper);
return stream().mapToDouble(mapper);
}
@ -104,7 +104,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 返回叠加拆分操作后的IntStream
*/
@Override
default IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) {
default IntStream flatMapToInt(final Function<? super T, ? extends IntStream> mapper) {
Objects.requireNonNull(mapper);
return stream().flatMapToInt(mapper);
}
@ -117,7 +117,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 返回叠加拆分操作后的LongStream
*/
@Override
default LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper) {
default LongStream flatMapToLong(final Function<? super T, ? extends LongStream> mapper) {
Objects.requireNonNull(mapper);
return stream().flatMapToLong(mapper);
}
@ -130,7 +130,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 返回叠加拆分操作后的DoubleStream
*/
@Override
default DoubleStream flatMapToDouble(Function<? super T, ? extends DoubleStream> mapper) {
default DoubleStream flatMapToDouble(final Function<? super T, ? extends DoubleStream> mapper) {
Objects.requireNonNull(mapper);
return stream().flatMapToDouble(mapper);
}
@ -169,7 +169,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 一个元素按指定的Comparator排序的流
*/
@Override
default S sorted(Comparator<? super T> comparator) {
default S sorted(final Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return wrapping(stream().sorted(comparator));
}
@ -192,7 +192,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* }</pre>
*/
@Override
default S peek(Consumer<? super T> action) {
default S peek(final Consumer<? super T> action) {
Objects.requireNonNull(action);
return wrapping(stream().peek(action));
}
@ -205,7 +205,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 截取后的流
*/
@Override
default S limit(long maxSize) {
default S limit(final long maxSize) {
return wrapping(stream().limit(maxSize));
}
@ -217,7 +217,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 丢弃前面n个元素后的剩余元素组成的流
*/
@Override
default S skip(long n) {
default S skip(final long n) {
return wrapping(stream().skip(n));
}
@ -228,7 +228,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @param action 操作
*/
@Override
default void forEach(Consumer<? super T> action) {
default void forEach(final Consumer<? super T> action) {
Objects.requireNonNull(action);
stream().forEach(action);
}
@ -240,7 +240,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @param action 操作
*/
@Override
default void forEachOrdered(Consumer<? super T> action) {
default void forEachOrdered(final Consumer<? super T> action) {
Objects.requireNonNull(action);
stream().forEachOrdered(action);
}
@ -266,7 +266,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @throws ArrayStoreException 如果元素转换失败例如不是该元素类型及其父类则抛出该异常
*/
@Override
default <A> A[] toArray(IntFunction<A[]> generator) {
default <A> A[] toArray(final IntFunction<A[]> generator) {
Objects.requireNonNull(generator);
return stream().toArray(generator);
}
@ -292,7 +292,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 聚合计算后的值
*/
@Override
default T reduce(T identity, BinaryOperator<T> accumulator) {
default T reduce(final T identity, final BinaryOperator<T> accumulator) {
Objects.requireNonNull(accumulator);
return stream().reduce(identity, accumulator);
}
@ -328,7 +328,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @see #max(Comparator)
*/
@Override
default Optional<T> reduce(BinaryOperator<T> accumulator) {
default Optional<T> reduce(final BinaryOperator<T> accumulator) {
Objects.requireNonNull(accumulator);
return stream().reduce(accumulator);
}
@ -346,7 +346,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @see #reduce(Object, BinaryOperator)
*/
@Override
default <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) {
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);
@ -366,7 +366,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* }</pre>
*/
@Override
default <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) {
default <R> R collect(final Supplier<R> supplier, final BiConsumer<R, ? super T> accumulator, final BiConsumer<R, R> combiner) {
Objects.requireNonNull(supplier);
Objects.requireNonNull(accumulator);
Objects.requireNonNull(combiner);
@ -383,7 +383,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 收集后的容器
*/
@Override
default <R, A> R collect(Collector<? super T, A, R> collector) {
default <R, A> R collect(final Collector<? super T, A, R> collector) {
Objects.requireNonNull(collector);
return stream().collect(collector);
}
@ -395,7 +395,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 最小值
*/
@Override
default Optional<T> min(Comparator<? super T> comparator) {
default Optional<T> min(final Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return stream().min(comparator);
}
@ -407,7 +407,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 最大值
*/
@Override
default Optional<T> max(Comparator<? super T> comparator) {
default Optional<T> max(final Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return stream().max(comparator);
}
@ -429,7 +429,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 是否有任何一个元素满足给定断言
*/
@Override
default boolean anyMatch(Predicate<? super T> predicate) {
default boolean anyMatch(final Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
return stream().anyMatch(predicate);
}
@ -441,7 +441,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 是否所有元素满足给定断言
*/
@Override
default boolean allMatch(Predicate<? super T> predicate) {
default boolean allMatch(final Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
return stream().allMatch(predicate);
}
@ -453,7 +453,7 @@ public interface WrappedStream<T, S extends WrappedStream<T, S>> extends Stream<
* @return 是否没有元素满足给定断言
*/
@Override
default boolean noneMatch(Predicate<? super T> predicate) {
default boolean noneMatch(final Predicate<? super T> predicate) {
Objects.requireNonNull(predicate);
return stream().noneMatch(predicate);
}

View File

@ -181,7 +181,7 @@ public class EasyStreamTest {
Assert.assertEquals(collect2, distinctBy2);
Assert.assertEquals(
4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).count()
4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count()
);
}