Skip to content

Commit b9f3a55

Browse files
committed
完善测试用例,兼容空值调用
1 parent 7d864f0 commit b9f3a55

11 files changed

Lines changed: 118 additions & 66 deletions

File tree

src/main/java/com/taowater/ztream/Ztream.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,6 @@ public static <T> Ztream<T> of(Stream<T> stream) {
113113
return Any.of(stream).map(Ztream::new).orElseGet(Ztream::empty);
114114
}
115115

116-
/**
117-
* 如果流集合不为空,则执行以所有元素收集成List为入参的消费函数
118-
*
119-
* @param consumer 消费者
120-
*/
121-
public <C extends Collection<T>> void ifNotEmpty(Consumer<C> consumer) {
122-
C list = (C) this.toList();
123-
if (EmptyUtil.isNotEmpty(list)) {
124-
consumer.accept(list);
125-
}
126-
}
127-
128116
/**
129117
* 遍历
130118
*
@@ -177,7 +165,10 @@ public <N> Ztream<N> convert(Class<N> clazz, BiConsumer<T, N> consumer) {
177165
*/
178166
@SafeVarargs
179167
public final Ztream<T> append(T... values) {
180-
return this.append(Ztream.of(values).toList());
168+
if (EmptyUtil.isEmpty(values)) {
169+
return this;
170+
}
171+
return append(Arrays.spliterator(values));
181172
}
182173

183174
/**

src/main/java/com/taowater/ztream/op/Collect.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.taowater.taol.core.util.ConvertUtil;
5+
import com.taowater.ztream.Any;
56

67
import java.util.*;
78
import java.util.function.Function;
@@ -13,15 +14,15 @@
1314
* 收集操作
1415
*
1516
* @author Zhu56
16-
* @date 2022/11/13 19:12:35
17+
* @since 0.0.1
1718
*/
1819
public interface Collect<T> extends Stream<T> {
1920

2021
/**
2122
* 收集为集合
2223
*
2324
* @param collectionFactory 集合工厂
24-
* @return {@link C }
25+
* @return 集合
2526
*/
2627
default <C extends Collection<T>> C collect(Supplier<? extends C> collectionFactory) {
2728
return collect(Collectors.toCollection(collectionFactory));
@@ -32,28 +33,28 @@ default <C extends Collection<T>> C collect(Supplier<? extends C> collectionFact
3233
*
3334
* @param fun 属性
3435
* @param collectionFactory 集合工厂
35-
* @return {@link C }
36+
* @return 集合
3637
*/
3738
default <V, C extends Collection<V>> C collect(Function<? super T, ? extends V> fun, Supplier<? extends C> collectionFactory) {
38-
return this.map(fun).collect(Collectors.toCollection(collectionFactory));
39+
return this.map(e -> Any.of(e).get(fun)).collect(Collectors.toCollection(collectionFactory));
3940
}
4041

4142
/**
4243
* 转换类型收集
4344
*
4445
* @param clazz clazz
4546
* @param collectionFactory 收集工厂
46-
* @return {@link C }
47+
* @return 集合
4748
*/
4849
default <N, C extends Collection<N>> C collect(Class<N> clazz, Supplier<? extends C> collectionFactory) {
49-
return this.map(e -> ConvertUtil.convert(e, clazz)).collect(Collectors.toCollection(collectionFactory));
50+
return this.map(e -> Any.of(e).get(o -> ConvertUtil.convert(o, clazz))).collect(Collectors.toCollection(collectionFactory));
5051
}
5152

5253
/**
5354
* 收集属性
5455
*
5556
* @param fun 属性
56-
* @return {@link List }<{@link V }>
57+
* @return 属性 ArrayList 集合
5758
*/
5859
default <V> List<V> collect(Function<? super T, ? extends V> fun) {
5960
return this.collect(fun, ArrayList::new);
@@ -63,7 +64,7 @@ default <V> List<V> collect(Function<? super T, ? extends V> fun) {
6364
* 转换元素类型并收集
6465
*
6566
* @param clazz clazz
66-
* @return {@link List}<{@link N}>
67+
* @return 转换 ArrayList 集合
6768
*/
6869
default <N> List<N> collect(Class<N> clazz) {
6970
return this.collect(clazz, ArrayList::new);
@@ -72,7 +73,7 @@ default <N> List<N> collect(Class<N> clazz) {
7273
/**
7374
* 收集为{@link ArrayList}
7475
*
75-
* @return 集合
76+
* @return ArrayList 集合
7677
*/
7778
default List<T> toList() {
7879
return collect(Collectors.toList());
@@ -82,7 +83,7 @@ default List<T> toList() {
8283
* 收集某属性为list
8384
*
8485
* @param fun 函数
85-
* @return {@link List }<{@link V }>
86+
* @return 属性 ArrayList 集合
8687
*/
8788
default <V> List<V> toList(Function<? super T, ? extends V> fun) {
8889
return this.collect(fun, ArrayList::new);
@@ -92,7 +93,7 @@ default <V> List<V> toList(Function<? super T, ? extends V> fun) {
9293
* 转换类型收集为list
9394
*
9495
* @param clazz clazz
95-
* @return {@link List}<{@link N}>
96+
* @return 转换 ArrayList 集合
9697
*/
9798
default <N> List<N> toList(Class<N> clazz) {
9899
return this.collect(clazz, ArrayList::new);
@@ -101,7 +102,7 @@ default <N> List<N> toList(Class<N> clazz) {
101102
/**
102103
* 收集为{@link HashSet}
103104
*
104-
* @return 集合
105+
* @return HashSet 集合
105106
*/
106107
default Set<T> toSet() {
107108
return collect(Collectors.toSet());
@@ -111,7 +112,7 @@ default Set<T> toSet() {
111112
* 收集某属性为set
112113
*
113114
* @param fun 函数
114-
* @return {@link Set}<{@link V}>
115+
* @return 属性 HashSet 集合
115116
*/
116117
default <V> Set<V> toSet(Function<? super T, ? extends V> fun) {
117118
return this.collect(fun, HashSet::new);
@@ -120,8 +121,8 @@ default <V> Set<V> toSet(Function<? super T, ? extends V> fun) {
120121
/**
121122
* 转换类型收集set
122123
*
123-
* @param clazz clazz
124-
* @return {@link List}<{@link N}>
124+
* @param clazz 类型
125+
* @return 转换 HashSet 集合
125126
*/
126127
default <N> Set<N> toSet(Class<N> clazz) {
127128
return this.collect(clazz, HashSet::new);

src/main/java/com/taowater/ztream/op/Filter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* 过滤操作
1919
*
2020
* @author zhu56
21-
* @date 2024/07/13 00:33
21+
* @see 0.0.3
2222
*/
2323
public interface Filter<T, S extends IZtream<T, S>> extends IZtream<T, S> {
2424

@@ -89,6 +89,7 @@ default <N extends Comparable<? super N>> S ge(Function<? super T, ? extends N>
8989
* @param rightValue 右值
9090
* @return 新流
9191
*/
92+
@SuppressWarnings("unchecked")
9293
default <N extends Comparable<? super N>> S between(Function<? super T, ? extends N> fun, N leftValue, N rightValue) {
9394
if (EmptyUtil.isAllEmpty(leftValue, rightValue)) {
9495
return wrap(this);
@@ -107,7 +108,7 @@ default <N extends Comparable<? super N>> S between(Function<? super T, ? extend
107108
*/
108109
default S rightLike(Function<? super T, String> fun, String value) {
109110
return filter(e -> {
110-
String str = fun.apply(e);
111+
String str = Any.of(e).get(fun);
111112
if (EmptyUtil.isEmpty(str)) {
112113
return EmptyUtil.isEmpty(value);
113114
}
@@ -136,7 +137,11 @@ default <V, C extends Collection<? extends V>> S in(Function<? super T, ? extend
136137
* @param values 值
137138
* @return 新流
138139
*/
140+
@SuppressWarnings("unchecked")
139141
default <V> S in(Function<? super T, ? extends V> fun, V... values) {
142+
if (EmptyUtil.isEmpty(values)) {
143+
return wrap(Stream.empty());
144+
}
140145
return in(fun, Stream.of(values).collect(Collectors.toSet()));
141146
}
142147

@@ -158,6 +163,7 @@ default <V, C extends Collection<? extends V>> S notIn(Function<? super T, ? ext
158163
* @param values 值
159164
* @return 新流
160165
*/
166+
@SuppressWarnings("unchecked")
161167
default <V> S notIn(Function<? super T, ? extends V> fun, V... values) {
162168
return notIn(fun, new HashSet<>(Arrays.asList(values)));
163169
}

src/main/java/com/taowater/ztream/op/GroupBy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* 分组操作
1717
*
1818
* @author Zhu56
19-
* @date 2022/11/13 20:21:12
19+
* @since 0.0.1
2020
*/
2121
public interface GroupBy<T> extends Stream<T> {
2222

src/main/java/com/taowater/ztream/op/Join.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* join操作
1313
*
1414
* @author Zhu56
15-
* @date 2022/11/13 19:12:35
15+
* @since 0.0.1
1616
*/
1717
public interface Join<T, S extends IZtream<T, S>> extends IZtream<T, S> {
1818

src/main/java/com/taowater/ztream/op/Judge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* 判断操作
1515
*
1616
* @author zhu56
17-
* @date 2024/07/15 00:42
17+
* @date @since 0.1.13
1818
*/
1919
public interface Judge<T, S extends IZtream<T, S>> extends IZtream<T, S> {
2020

src/main/java/com/taowater/ztream/op/Math.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
* 数学统计相关
1616
*
1717
* @author zhu56
18-
* @version 1.0
19-
* @date 2022/11/14 9:53
18+
* @since 0.0.1
2019
*/
2120
public interface Math<T> extends Stream<T> {
2221

src/main/java/com/taowater/ztream/op/Sort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* 排序操作
1515
*
1616
* @author zhu56
17-
* @date 2024/07/13 00:39
17+
* @since 0.0.3
1818
*/
1919
public interface Sort<T, S extends IZtream<T, S>> extends IZtream<T, S> {
2020

src/main/java/com/taowater/ztream/op/ToMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* 映射操作
1212
*
1313
* @author Zhu56
14-
* @date 2022/11/13 20:21:12
14+
* @since 0.0.1
1515
*/
1616
public interface ToMap<T> extends Stream<T> {
1717

src/test/java/com/taowater/ztream/TestClass.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ public static class Student extends Person {
2424

2525
public static class Person {
2626
private String sex;
27-
2827
}
2928

3029
@Data
31-
@EqualsAndHashCode(callSuper = true)
3230
public static class Teacher extends Person {
3331
private String name;
3432
private Long money;

0 commit comments

Comments
 (0)