Skip to content

Commit 3cafee8

Browse files
committed
Polish code
1 parent 3e86a47 commit 3cafee8

File tree

3 files changed

+33
-92
lines changed

3 files changed

+33
-92
lines changed

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/function/Predicates.java

-28
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,6 @@ static <T> Predicate<T> alwaysFalse() {
5151
return (Predicate<T>) ALWAYS_FALSE;
5252
}
5353

54-
/**
55-
* a composed predicate that represents a short-circuiting logical AND of {@link Predicate predicates}
56-
*
57-
* @param predicate {@link Predicate one predicate}
58-
* @param otherPredicates {@link Predicate other predicates}
59-
* @param <T> the type to test
60-
* @return non-null
61-
*/
62-
static <T> Predicate<T> and(Predicate<? super T> predicate, Predicate<? super T>... otherPredicates) {
63-
Predicate<T> andPredicate = alwaysTrue();
64-
andPredicate = andPredicate.and(predicate).and(and(otherPredicates));
65-
return andPredicate;
66-
}
67-
6854
/**
6955
* a composed predicate that represents a short-circuiting logical AND of {@link Predicate predicates}
7056
*
@@ -80,20 +66,6 @@ static <T> Predicate<T> and(Predicate<? super T>... predicates) {
8066
return andPredicate;
8167
}
8268

83-
/**
84-
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates}
85-
*
86-
* @param predicate {@link Predicate one predicate}
87-
* @param otherPredicates {@link Predicate other predicates}
88-
* @param <T> the detected type
89-
* @return non-null
90-
*/
91-
static <T> Predicate<T> or(Predicate<? super T> predicate, Predicate<? super T>... otherPredicates) {
92-
Predicate<T> orPredicate = alwaysTrue();
93-
orPredicate = orPredicate.or(predicate).or(or(otherPredicates));
94-
return orPredicate;
95-
}
96-
9769
/**
9870
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates}
9971
*

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/function/Streams.java

+32-63
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.geektimes.commons.function;
1818

19+
import org.geektimes.commons.lang.util.ArrayUtils;
20+
1921
import java.util.LinkedList;
2022
import java.util.List;
2123
import java.util.Queue;
@@ -30,8 +32,10 @@
3032
import static java.util.Collections.unmodifiableList;
3133
import static java.util.Collections.unmodifiableSet;
3234
import static java.util.stream.Collectors.toList;
35+
import static java.util.stream.Collectors.toSet;
3336
import static org.geektimes.commons.collection.util.CollectionUtils.*;
34-
import static org.geektimes.commons.function.Predicates.*;
37+
import static org.geektimes.commons.function.Predicates.and;
38+
import static org.geektimes.commons.function.Predicates.or;
3539

3640
/**
3741
* The utilities class for {@link Stream}
@@ -45,121 +49,86 @@ static <T> Stream<T> stream(Iterable<T> iterable) {
4549
}
4650

4751
static <T, I extends Iterable<T>> Stream<T> filterStream(I values, Predicate<? super T> predicate) {
48-
return filterStream(values, predicate, EMPTY_ARRAY);
52+
return stream(values).filter(predicate);
4953
}
5054

5155
static <T, I extends Iterable<T>> Stream<T> filterStream(I values, Predicate<? super T>... predicates) {
52-
return filterStream(values, alwaysTrue(), predicates);
53-
}
54-
55-
static <T, I extends Iterable<T>> Stream<T> filterStream(I values, Predicate<? super T> predicate,
56-
Predicate<? super T>... otherPredicates) {
57-
return stream(values).filter(and(predicate, otherPredicates));
56+
return ArrayUtils.isEmpty(predicates) ? stream(values) : filterStream(values, and(predicates));
5857
}
5958

6059
static <E, L extends List<E>> List<E> filter(L values, Predicate<? super E> predicate) {
61-
return filter(values, predicate, EMPTY_ARRAY);
62-
}
63-
64-
static <E, L extends List<E>> List<E> filter(L values, Predicate<? super E>... predicates) {
65-
return filter(values, alwaysTrue(), predicates);
66-
}
67-
68-
static <E, L extends List<E>> List<E> filter(L values, Predicate<? super E> predicate, Predicate<? super E>... otherPredicates) {
6960
final L result;
7061
if (predicate == null) {
7162
result = values;
7263
} else {
73-
result = (L) filterStream(values, predicate, otherPredicates).collect(toList());
64+
result = (L) filterStream(values, predicate).collect(toList());
7465
}
7566
return unmodifiableList(result);
7667
}
7768

78-
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E> predicate) {
79-
return filter(values, predicate, EMPTY_ARRAY);
80-
}
81-
82-
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E>... predicates) {
83-
return filter(values, alwaysTrue(), predicates);
69+
static <E, L extends List<E>> List<E> filter(L values, Predicate<? super E>... predicates) {
70+
return filter(values, and(predicates));
8471
}
8572

86-
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E> predicate,
87-
Predicate<? super E>... otherPredicates) {
73+
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E> predicate) {
8874
final S result;
8975
if (predicate == null) {
9076
result = values;
9177
} else {
92-
result = (S) filterStream(values, predicate, otherPredicates).collect(Collectors.toSet());
78+
result = (S) filterStream(values, predicate).collect(toSet());
9379
}
9480
return unmodifiableSet(result);
9581
}
9682

97-
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E> predicate) {
98-
return filter(values, predicate, EMPTY_ARRAY);
99-
}
100-
101-
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E>... predicates) {
102-
return filter(values, alwaysTrue(), predicates);
83+
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E>... predicates) {
84+
return filter(values, and(predicates));
10385
}
10486

105-
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E> predicate,
106-
Predicate<? super E>... otherPredicates) {
87+
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E> predicate) {
10788
final Q result;
10889
if (predicate == null) {
10990
result = values;
11091
} else {
111-
result = (Q) filterStream(values, predicate, otherPredicates)
112-
.collect(LinkedList::new, List::add, List::addAll);
92+
result = (Q) filterStream(values, predicate).collect(LinkedList::new, List::add, List::addAll);
11393
}
11494
return unmodifiableQueue(result);
11595
}
11696

117-
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate) {
118-
return (S) filter(values, predicate, EMPTY_ARRAY);
119-
}
120-
121-
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T>... predicates) {
122-
return filter(values, alwaysTrue(), predicates);
97+
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E>... predicates) {
98+
return filter(values, and(predicates));
12399
}
124100

125-
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate,
126-
Predicate<? super T>... otherPredicates) {
101+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate) {
127102
if (isSet(values)) {
128-
return (S) filter((Set) values, predicate, otherPredicates);
103+
return (S) filter((Set) values, predicate);
129104
} else if (isList(values)) {
130-
return (S) filter((List) values, predicate, otherPredicates);
105+
return (S) filter((List) values, predicate);
131106
} else if (isQueue(values)) {
132-
return (S) filter((Queue) values, predicate, otherPredicates);
107+
return (S) filter((Queue) values, predicate);
133108
}
134109
String message = format("The 'values' type can't be supported!", values.getClass().getName());
135110
throw new UnsupportedOperationException(message);
136111
}
137112

138-
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T>... predicates) {
139-
return filterAny(values, alwaysTrue(), predicates);
113+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T>... predicates) {
114+
return filter(values, and(predicates));
140115
}
141116

142-
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T> predicate,
143-
Predicate<? super T>... otherPredicates) {
144-
return filter(values, or(predicate, otherPredicates));
117+
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T>... predicates) {
118+
return filter(values, or(predicates));
145119
}
146120

147121
static <T> T filterFirst(Iterable<T> values, Predicate<? super T> predicate) {
148-
return (T) filterFirst(values, predicate, EMPTY_ARRAY);
149-
}
150-
151-
static <T> T filterFirst(Iterable<T> values, Predicate<? super T>... predicates) {
152-
return filterFirst(values, alwaysTrue(), predicates);
153-
}
154-
155-
static <T> T filterFirst(Iterable<T> values, Predicate<? super T> predicate,
156-
Predicate<? super T>... otherPredicates) {
157122
return stream(values)
158-
.filter(and(predicate, otherPredicates))
123+
.filter(predicate)
159124
.findFirst()
160125
.orElse(null);
161126
}
162127

128+
static <T> T filterFirst(Iterable<T> values, Predicate<? super T>... predicates) {
129+
return filterFirst(values, and(predicates));
130+
}
131+
163132
static <T, R> List<R> map(List<T> values, Function<T, R> mapper) {
164133
return stream(values)
165134
.map(mapper)
@@ -169,7 +138,7 @@ static <T, R> List<R> map(List<T> values, Function<T, R> mapper) {
169138
static <T, R> Set<R> map(Set<T> values, Function<T, R> mapper) {
170139
return stream(values)
171140
.map(mapper)
172-
.collect(Collectors.toSet());
141+
.collect(toSet());
173142
}
174143
}
175144

projects/stage-1/middleware-frameworks/my-commons/src/test/java/org/geektimes/commons/function/PredicatesTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testAlways() {
4040

4141
@Test
4242
public void testAnd() {
43-
43+
assertTrue(and(alwaysTrue(), alwaysTrue()).test(null));
4444
}
4545

4646
}

0 commit comments

Comments
 (0)