Skip to content

Commit f1e497c

Browse files
committed
Merge branch 'dev-features'
2 parents a49c010 + b4478fe commit f1e497c

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package com.dianpoint.summer.lang;
22

3+
import com.dianpoint.summer.utils.SetUtils;
4+
5+
import java.util.Arrays;
6+
import java.util.LinkedHashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
import java.util.function.Predicate;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
12+
import java.util.stream.StreamSupport;
13+
314
/**
415
* @author: congccoder
516
* @email: congccoder@gmail.com | <a href="https://github.com/ccoderJava">github-homepage</a>
@@ -8,5 +19,89 @@
819

920
public interface Streams {
1021

22+
static <T> Stream<T> stream(T... values) {
23+
return Stream.of(values);
24+
}
25+
26+
static <T> Stream<T> stream(Iterable<T> iterable) {
27+
return StreamSupport.stream(iterable.spliterator(), false);
28+
}
29+
30+
31+
static <T> Stream<T> filterStream(T[] values, Predicate<? super T> predicate) {
32+
Stream<T> stream = stream(values);
33+
return stream.filter(predicate);
34+
}
35+
36+
static <T, S extends Iterable<T>> Stream<T> filterStream(S values, Predicate<? super T> predicate) {
37+
return stream(values).filter(predicate);
38+
}
39+
40+
static <T> List<T> filterList(T[] values, Predicate<? super T> predicate) {
41+
return filterList(Arrays.asList(values), predicate);
42+
}
43+
44+
static <T, S extends Iterable<T>> List<T> filterList(S values, Predicate<? super T> predicate) {
45+
return filterStream(values, predicate).collect(Collectors.toList());
46+
}
47+
48+
49+
static <T, S extends Iterable<T>> Set<T> filterSet(S values, Predicate<? super T> predicate) {
50+
return filterStream(values, predicate).collect(LinkedHashSet::new, Set::add, Set::addAll);
51+
}
52+
53+
static <T> Set<T> filterSet(T[] values, Predicate<T> predicate) {
54+
return filterSet(Arrays.asList(values), predicate);
55+
}
56+
57+
58+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate) {
59+
boolean isSet = SetUtils.isSet(values);
60+
if (isSet) {
61+
return (S) filterSet(values, predicate);
62+
}
63+
return (S) filterList(values, predicate);
64+
}
65+
66+
67+
//filter逻辑操作
68+
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+
11106

12107
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.dianpoint.summer.lang;
2+
3+
import com.dianpoint.summer.utils.AssertUtils;
4+
5+
import java.util.function.BiConsumer;
6+
7+
@FunctionalInterface
8+
public interface ThrowableConsumer<T> {
9+
10+
void accept(T t) throws Throwable;
11+
12+
13+
/**
14+
* 定义一个默认的异常处理方法,用来固定抛出一个runtimeException
15+
*
16+
* @param t 入参
17+
* @param throwable 待抛出的异常
18+
*/
19+
default void handleException(T t, Throwable throwable) {
20+
throw new RuntimeException(throwable);
21+
}
22+
23+
/**
24+
* 默认执行逻辑,无客制化异常处理逻辑,采用默认的异常处理方法{@link #handleException(Object, Throwable)}
25+
*
26+
* @param t 入参
27+
*/
28+
default void execute(T t) {
29+
execute(t, this::handleException);
30+
}
31+
32+
default void execute(T t, BiConsumer<T, Throwable> exceptionHandler) throws NullPointerException {
33+
AssertUtils.assertNotNull(exceptionHandler, () -> "The 'exceptionHandler' must not be null");
34+
try {
35+
accept(t);
36+
} catch (Throwable failure) {
37+
exceptionHandler.accept(t, failure);
38+
}
39+
}
40+
41+
default void execute(T t, ThrowableConsumer<T> consumer) throws NullPointerException {
42+
consumer.execute(t, this::handleException);
43+
}
44+
45+
default void execute(T t, ThrowableConsumer<T> consumer, BiConsumer<T, Throwable> exceptionHandler)
46+
throws NullPointerException {
47+
AssertUtils.assertNotNull(exceptionHandler, () -> "The 'exceptionHandler' must not be null");
48+
consumer.execute(t, exceptionHandler);
49+
}
50+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.dianpoint.summer.lang;
2+
3+
import com.dianpoint.summer.utils.AssertUtils;
4+
5+
import java.util.function.BiFunction;
6+
7+
/**
8+
* 一个通用的抽象的处理异常的function
9+
*
10+
* @param <T> 入参
11+
* @param <R> 结果
12+
*/
13+
@FunctionalInterface
14+
public interface ThrowableFunction<T, R> {
15+
16+
R apply(T t) throws Throwable;
17+
18+
default R handlerException(T t, Throwable throwable) {
19+
throw new RuntimeException(throwable);
20+
}
21+
22+
default R execute(T t, BiFunction<T, Throwable, R> exceptionHandler) {
23+
R result;
24+
try {
25+
result = apply(t);
26+
} catch (Throwable failure) {
27+
result = exceptionHandler.apply(t, failure);
28+
}
29+
return result;
30+
}
31+
32+
default R execute(T t) throws RuntimeException {
33+
return execute(t, this::handlerException);
34+
}
35+
36+
default <T, R> R execute(T t, ThrowableFunction<T, R> function, BiFunction<T, Throwable, R> exceptionHandler)
37+
throws IllegalArgumentException {
38+
AssertUtils.assertNotNull(function, "The 'function' must not be null");
39+
AssertUtils.assertNotNull(exceptionHandler, "The 'exceptionHandler' must not be null");
40+
41+
return function.execute(t, exceptionHandler);
42+
}
43+
44+
default <T, R> R execute(T t, ThrowableFunction<T, R> function) throws IllegalArgumentException {
45+
AssertUtils.assertNotNull(function, "The 'function' must not be null");
46+
return execute(t, function, function::handlerException);
47+
}
48+
49+
default <V> ThrowableFunction<V, R> compose(ThrowableFunction<? super V, ? extends T> before) {
50+
AssertUtils.assertNotNull(before, "The 'before' must not be null");
51+
return (V v) -> apply(before.apply(v));
52+
}
53+
54+
default <V> ThrowableFunction<T, V> andThen(ThrowableFunction<? super R, ? extends V> after) {
55+
AssertUtils.assertNotNull(after, "The 'after' must not be null");
56+
return (T t) -> after.apply(apply(t));
57+
}
58+
59+
60+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.dianpoint.summer.utils;
2+
3+
import java.util.Collections;
4+
import java.util.LinkedHashSet;
5+
import java.util.Set;
6+
7+
public class SetUtils implements Utils {
8+
9+
protected static final float FIXED_LOAD_FACTOR = 1.00f;
10+
11+
public static boolean isSet(Iterable<?> elements) {
12+
return elements instanceof Set;
13+
}
14+
15+
public static <E> Set<E> of(E... elements) {
16+
return ofSet(elements);
17+
}
18+
19+
public static <E> Set<E> ofSet(E... elements) {
20+
int size = elements == null ? 0 : elements.length;
21+
if (size < 1) {
22+
return Collections.emptySet();
23+
} else if (size == 1) {
24+
return Collections.singleton(elements[0]);
25+
}
26+
27+
Set<E> set = new LinkedHashSet<>(size, FIXED_LOAD_FACTOR);
28+
for (int i = 0; i < size; i++) {
29+
set.add(elements[i]);
30+
}
31+
return Collections.unmodifiableSet(set);
32+
}
33+
}

0 commit comments

Comments
 (0)