Skip to content

Commit 2f4a6a3

Browse files
committed
update functions, add some short-circuiting logical AND 、OR
1 parent 56194e9 commit 2f4a6a3

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

summer-java-core/src/main/java/com/dianpoint/summer/lang/Predicates.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,45 @@ static <T> Predicate<T> alwaysFalse() {
3939
}
4040

4141

42+
static <T> Predicate<? super T> and(Predicate<? super T>... predicates) {
43+
int length = predicates == null ? 0 : predicates.length;
44+
if (length == 0) {
45+
return alwaysTrue();
46+
}
47+
if (length == 1) {
48+
return predicates[0];
49+
}
50+
Predicate<T> andPredicate = alwaysTrue();
51+
for (Predicate<? super T> predicate : predicates) {
52+
andPredicate = andPredicate.and(predicate);
53+
}
54+
return andPredicate;
55+
}
56+
57+
/**
58+
* 输出一个逻辑或运算
59+
*
60+
* @param predicates {@link Predicate predicates}
61+
* @param <T> 被判断的对象
62+
* @return non-null
63+
*/
64+
static <T> Predicate<? super T> or(Predicate<? super T>... predicates) {
65+
int length = predicates == null ? 0 : predicates.length;
66+
if (length == 0) {
67+
return alwaysFalse();
68+
}
69+
if (length == 1) {
70+
return predicates[0];
71+
}
72+
73+
Predicate<T> orPredicate = alwaysFalse();
74+
75+
for (Predicate<? super T> predicate : predicates) {
76+
orPredicate = orPredicate.or(predicate);
77+
}
78+
return orPredicate;
79+
80+
}
81+
82+
4283
}

summer-java-core/src/main/java/com/dianpoint/summer/lang/Streams.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static <T> Set<T> filterSet(T[] values, Predicate<T> predicate) {
5555
}
5656

5757

58-
static <T, S extends Iterable<T>> S filterAll(S values, Predicate<? super T> predicate) {
58+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate) {
5959
boolean isSet = SetUtils.isSet(values);
6060
if (isSet) {
6161
return (S) filterSet(values, predicate);
@@ -66,5 +66,42 @@ static <T, S extends Iterable<T>> S filterAll(S values, Predicate<? super T> pre
6666

6767
//filter逻辑操作
6868

69+
//通用的filter过滤方法,对于集合iterable类型进行过滤处理
70+
static <T, S extends Iterable<T>> S filterAll(S values, Predicate<? super T>... predicates) {
71+
return filter(values, Predicates.and(predicates));
72+
}
73+
74+
//对iterable进行逻辑and 逻辑or处理
75+
76+
static <T> List<T> filterAllList(T[] values, Predicate<? super T>... predicates) {
77+
return filterAll(Arrays.asList(values), predicates);
78+
}
79+
80+
static <T> Set<T> filterAllSet(T[] values, Predicate<? super T>... predicates) {
81+
return filterAll(SetUtils.ofSet(values), predicates);
82+
}
83+
84+
85+
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T>... predicates) {
86+
return filter(values, Predicates.or(predicates));
87+
}
88+
89+
static <T> List<T> filterAnyList(T[] values, Predicate<? super T>... predicates) {
90+
return filterAny(Arrays.asList(values), predicates);
91+
}
92+
93+
static <T> Set<T> filterAnySet(T[] values, Predicate<? super T>... predicates) {
94+
return filterAny(SetUtils.ofSet(values), predicates);
95+
}
96+
97+
98+
//再添加一个findFirst方法
99+
static <T> T filterFirst(Iterable<T> values, Predicate<T>... predicates) {
100+
return StreamSupport.stream(values.spliterator(), false)
101+
.filter(Predicates.and(predicates))
102+
.findFirst()
103+
.orElse(null);
104+
}
105+
69106

70107
}

0 commit comments

Comments
 (0)