Skip to content

Commit f17b4a9

Browse files
committed
Polish Code
1 parent 2e01e1b commit f17b4a9

File tree

11 files changed

+185
-58
lines changed

11 files changed

+185
-58
lines changed

projects/stage-1/middleware-frameworks/my-cdi/src/main/java/org/geektimes/enterprise/inject/util/Beans.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.geektimes.enterprise.inject.util;
1818

1919
import org.geektimes.commons.lang.util.ArrayUtils;
20-
import org.geektimes.commons.reflect.util.ConstructorUtils;
2120
import org.geektimes.commons.reflect.util.MemberUtils;
2221
import org.geektimes.interceptor.InterceptorManager;
2322

@@ -251,7 +250,8 @@ private static boolean hasFinalMethod(Class<?> beanClass) {
251250
}
252251

253252
private static boolean hasNonDefaultConstructor(Class<?> beanClass) {
254-
ConstructorUtils.hasPublicNoArgConstructor()
253+
// TODO
254+
return true;
255255
}
256256

257257
private static boolean hasManagedBeanConstructor(Class<?> beanClass) {

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

+36-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ public interface Predicates {
2727

2828
Predicate[] EMPTY_ARRAY = new Predicate[0];
2929

30+
Predicate<?> ALWAYS_TRUE = e -> true;
31+
32+
Predicate<?> ALWAYS_FALSE = e -> false;
33+
3034
/**
3135
* {@link Predicate} always return <code>true</code>
3236
*
3337
* @param <T> the type to test
3438
* @return <code>true</code>
3539
*/
3640
static <T> Predicate<T> alwaysTrue() {
37-
return e -> true;
41+
return (Predicate<T>) ALWAYS_TRUE;
3842
}
3943

4044
/**
@@ -44,13 +48,27 @@ static <T> Predicate<T> alwaysTrue() {
4448
* @return <code>false</code>
4549
*/
4650
static <T> Predicate<T> alwaysFalse() {
47-
return e -> false;
51+
return (Predicate<T>) ALWAYS_FALSE;
52+
}
53+
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;
4866
}
4967

5068
/**
5169
* a composed predicate that represents a short-circuiting logical AND of {@link Predicate predicates}
5270
*
53-
* @param predicates {@link Predicate predicates}
71+
* @param predicates {@link Predicate other predicates}
5472
* @param <T> the type to test
5573
* @return non-null
5674
*/
@@ -65,7 +83,21 @@ static <T> Predicate<T> and(Predicate<? super T>... predicates) {
6583
/**
6684
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates}
6785
*
68-
* @param predicates {@link Predicate predicates}
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+
97+
/**
98+
* a composed predicate that represents a short-circuiting logical OR of {@link Predicate predicates}
99+
*
100+
* @param predicates {@link Predicate other predicates}
69101
* @param <T> the detected type
70102
* @return non-null
71103
*/

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

+74-20
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import static java.util.Collections.unmodifiableSet;
3232
import static java.util.stream.Collectors.toList;
3333
import static org.geektimes.commons.collection.util.CollectionUtils.*;
34-
import static org.geektimes.commons.function.Predicates.and;
35-
import static org.geektimes.commons.function.Predicates.or;
34+
import static org.geektimes.commons.function.Predicates.*;
3635

3736
/**
3837
* The utilities class for {@link Stream}
@@ -45,63 +44,118 @@ static <T> Stream<T> stream(Iterable<T> iterable) {
4544
return StreamSupport.stream(iterable.spliterator(), false);
4645
}
4746

48-
static <T, S extends Iterable<T>> Stream<T> filterStream(S values, Predicate<T> predicate) {
49-
return stream(values).filter(predicate);
47+
static <T, I extends Iterable<T>> Stream<T> filterStream(I values, Predicate<? super T> predicate) {
48+
return filterStream(values, predicate, EMPTY_ARRAY);
5049
}
5150

52-
static <E, L extends List<E>> List<E> filter(L values, Predicate<E> predicate) {
51+
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));
58+
}
59+
60+
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) {
5369
final L result;
5470
if (predicate == null) {
5571
result = values;
5672
} else {
57-
result = (L) filterStream(values, predicate).collect(toList());
73+
result = (L) filterStream(values, predicate, otherPredicates).collect(toList());
5874
}
5975
return unmodifiableList(result);
6076
}
6177

62-
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<E> predicate) {
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);
84+
}
85+
86+
static <E, S extends Set<E>> Set<E> filter(S values, Predicate<? super E> predicate,
87+
Predicate<? super E>... otherPredicates) {
6388
final S result;
6489
if (predicate == null) {
6590
result = values;
6691
} else {
67-
result = (S) filterStream(values, predicate).collect(Collectors.toSet());
92+
result = (S) filterStream(values, predicate, otherPredicates).collect(Collectors.toSet());
6893
}
6994
return unmodifiableSet(result);
7095
}
7196

72-
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<E> predicate) {
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);
103+
}
104+
105+
static <E, Q extends Queue<E>> Queue<E> filter(Q values, Predicate<? super E> predicate,
106+
Predicate<? super E>... otherPredicates) {
73107
final Q result;
74108
if (predicate == null) {
75109
result = values;
76110
} else {
77-
result = (Q) filterStream(values, predicate).collect(LinkedList::new, List::add, List::addAll);
111+
result = (Q) filterStream(values, predicate, otherPredicates)
112+
.collect(LinkedList::new, List::add, List::addAll);
78113
}
79114
return unmodifiableQueue(result);
80115
}
81116

82-
static <T, S extends Iterable<T>> S filter(S values, Predicate<T> predicate) {
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);
123+
}
124+
125+
static <T, S extends Iterable<T>> S filter(S values, Predicate<? super T> predicate,
126+
Predicate<? super T>... otherPredicates) {
83127
if (isSet(values)) {
84-
return (S) filter((Set) values, predicate);
128+
return (S) filter((Set) values, predicate, otherPredicates);
85129
} else if (isList(values)) {
86-
return (S) filter((List) values, predicate);
130+
return (S) filter((List) values, predicate, otherPredicates);
87131
} else if (isQueue(values)) {
88-
return (S) filter((Queue) values, predicate);
132+
return (S) filter((Queue) values, predicate, otherPredicates);
89133
}
90134
String message = format("The 'values' type can't be supported!", values.getClass().getName());
91135
throw new UnsupportedOperationException(message);
92136
}
93137

94-
static <T, S extends Iterable<T>> S filterAll(S values, Predicate<? super T>... predicates) {
95-
return filter(values, and(predicates));
138+
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T>... predicates) {
139+
return filterAny(values, alwaysTrue(), predicates);
96140
}
97141

98-
static <T, S extends Iterable<T>> S filterAny(S values, Predicate<? super T>... predicates) {
99-
return filter(values, or(predicates));
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));
145+
}
146+
147+
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);
100153
}
101154

102-
static <T> T filterFirst(Iterable<T> values, Predicate<T>... predicates) {
155+
static <T> T filterFirst(Iterable<T> values, Predicate<? super T> predicate,
156+
Predicate<? super T>... otherPredicates) {
103157
return stream(values)
104-
.filter(and(predicates))
158+
.filter(and(predicate, otherPredicates))
105159
.findFirst()
106160
.orElse(null);
107161
}

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/lang/util/AnnotationUtils.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import static java.util.Collections.*;
3030
import static java.util.Optional.ofNullable;
3131
import static org.geektimes.commons.function.Predicates.and;
32-
import static org.geektimes.commons.function.Streams.filterAll;
32+
import static org.geektimes.commons.function.Streams.filter;
3333
import static org.geektimes.commons.function.Streams.filterFirst;
3434
import static org.geektimes.commons.function.ThrowableSupplier.execute;
3535
import static org.geektimes.commons.lang.util.ArrayUtils.length;
@@ -178,7 +178,7 @@ public static List<Annotation> filterAnnotations(Annotation[] annotations,
178178

179179
public static <S extends Iterable<Annotation>> S filterAnnotations(S annotations,
180180
Predicate<Annotation>... annotationsToFilter) {
181-
return filterAll(annotations, annotationsToFilter);
181+
return filter(annotations, annotationsToFilter);
182182
}
183183

184184
/**
@@ -208,7 +208,7 @@ public static List<Annotation> getAllDeclaredAnnotations(Class<?> type, Predicat
208208
allAnnotations.addAll(getDeclaredAnnotations(t));
209209
}
210210

211-
return filterAll(allAnnotations, annotationsToFilter);
211+
return filter(allAnnotations, annotationsToFilter);
212212
}
213213

214214
/**
@@ -225,7 +225,7 @@ public static List<Annotation> getDeclaredAnnotations(AnnotatedElement annotated
225225
return emptyList();
226226
}
227227

228-
return filterAll(asList(annotatedElement.getAnnotations()), annotationsToFilter);
228+
return filter(asList(annotatedElement.getAnnotations()), annotationsToFilter);
229229
}
230230

231231
public static <T> T getAttributeValue(Annotation[] annotations, String attributeName, Class<T> returnType) {

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/reflect/util/ClassUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import static java.util.Arrays.asList;
4848
import static java.util.Collections.*;
4949
import static org.geektimes.commons.collection.util.CollectionUtils.asSet;
50-
import static org.geektimes.commons.function.Streams.filterAll;
50+
import static org.geektimes.commons.function.Streams.filter;
5151
import static org.geektimes.commons.function.ThrowableFunction.execute;
5252
import static org.geektimes.commons.lang.util.ArrayUtils.isEmpty;
5353
import static org.geektimes.commons.lang.util.ArrayUtils.isNotEmpty;
@@ -487,7 +487,7 @@ public static Set<Class<?>> getAllClasses(Class<?> type, boolean includedSelf, P
487487
}
488488

489489
// Keep the same order from List
490-
return CollectionUtils.asSet(filterAll(allClasses, classFilters));
490+
return CollectionUtils.asSet(filter(allClasses, classFilters));
491491
}
492492

493493
/**
@@ -547,7 +547,7 @@ public static Set<Class<?>> getAllInterfaces(Class<?> type, Predicate<Class<?>>.
547547
// FIFO -> FILO
548548
Collections.reverse(allInterfaces);
549549

550-
return CollectionUtils.asSet(filterAll(allInterfaces, interfaceFilters));
550+
return CollectionUtils.asSet(filter(allInterfaces, interfaceFilters));
551551
}
552552

553553
/**

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/reflect/util/ConstructorUtils.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package org.geektimes.commons.reflect.util;
1818

1919
import java.lang.reflect.Constructor;
20-
import java.util.Set;
20+
import java.util.List;
2121
import java.util.function.Predicate;
2222

23-
import static java.util.Collections.unmodifiableSet;
24-
import static java.util.stream.Collectors.toSet;
25-
import static java.util.stream.Stream.of;
26-
import static org.geektimes.commons.function.Predicates.and;
23+
import static java.util.Arrays.asList;
24+
import static org.geektimes.commons.function.Streams.filter;
2725
import static org.geektimes.commons.reflect.util.MemberUtils.isPublic;
2826

2927
/**
@@ -56,9 +54,9 @@ public static boolean hasPublicNoArgConstructor(Class<?> type) {
5654
return has;
5755
}
5856

59-
public static Set<Constructor<?>> getConstructors(Class<?> type, Predicate<Constructor<?>>... constructorFilters) {
60-
return unmodifiableSet(of(type.getConstructors())
61-
.filter(and(constructorFilters))
62-
.collect(toSet()));
57+
public static List<Constructor<?>> getConstructors(Class<?> type,
58+
Predicate<? super Constructor<?>>... constructorFilters) {
59+
List<Constructor<?>> constructors = asList(type.getConstructors());
60+
return filter(constructors, constructorFilters);
6361
}
6462
}

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/reflect/util/MemberUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public abstract class MemberUtils {
3131

3232
public final static Predicate<Member> NON_STATIC_METHOD_PREDICATE = MemberUtils::isNonStatic;
3333

34-
public final static Predicate<? extends Member> FINAL_METHOD_PREDICATE = MemberUtils::isFinal;
34+
public final static Predicate<Member> FINAL_METHOD_PREDICATE = MemberUtils::isFinal;
3535

36-
public final static Predicate<? extends Member> PUBLIC_METHOD_PREDICATE = MemberUtils::isPublic;
36+
public final static Predicate<Member> PUBLIC_METHOD_PREDICATE = MemberUtils::isPublic;
3737

38-
public final static Predicate<? extends Member> NON_PRIVATE_METHOD_PREDICATE = MemberUtils::isNonPrivate;
38+
public final static Predicate<Member> NON_PRIVATE_METHOD_PREDICATE = MemberUtils::isNonPrivate;
3939

4040
/**
4141
* check the specified {@link Member member} is static or not ?

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/reflect/util/MethodUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static java.util.Collections.emptySet;
2929
import static java.util.Collections.unmodifiableSet;
3030
import static org.geektimes.commons.collection.util.CollectionUtils.asSet;
31-
import static org.geektimes.commons.function.Streams.filterAll;
31+
import static org.geektimes.commons.function.Streams.filter;
3232
import static org.geektimes.commons.lang.util.StringUtils.isNotEmpty;
3333
import static org.geektimes.commons.reflect.util.ClassUtils.*;
3434
import static org.geektimes.commons.reflect.util.MemberUtils.*;
@@ -84,7 +84,7 @@ public static Set<Method> getMethods(Class<?> declaringClass, boolean includeInh
8484
}
8585
}
8686

87-
return unmodifiableSet(filterAll(allMethods, methodsToFilter));
87+
return unmodifiableSet(filter(allMethods, methodsToFilter));
8888
}
8989

9090
/**

projects/stage-1/middleware-frameworks/my-commons/src/main/java/org/geektimes/commons/reflect/util/SimpleClassScanner.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static java.util.Collections.unmodifiableSet;
2020
import static org.geektimes.commons.collection.util.CollectionUtils.*;
2121
import static org.geektimes.commons.function.Streams.filter;
22-
import static org.geektimes.commons.function.Streams.filterAll;
2322
import static org.geektimes.commons.lang.util.ClassLoaderUtils.findLoadedClass;
2423
import static org.geektimes.commons.lang.util.ClassLoaderUtils.loadClass;
2524
import static org.geektimes.commons.net.util.URLUtils.resolveArchiveFile;
@@ -138,7 +137,7 @@ public Set<Class<?>> scan(ClassLoader classLoader, URL resourceInArchive, boolea
138137
classesSet.add(class_);
139138
}
140139
}
141-
return filterAll(classesSet, classFilters);
140+
return filter(classesSet, classFilters);
142141
}
143142

144143
public Set<Class<?>> scan(ClassLoader classLoader, File archiveFile, boolean requiredLoad,
@@ -151,7 +150,7 @@ public Set<Class<?>> scan(ClassLoader classLoader, File archiveFile, boolean req
151150
classesSet.add(class_);
152151
}
153152
}
154-
return filterAll(classesSet, classFilters);
153+
return filter(classesSet, classFilters);
155154
}
156155

157156
private Set<String> filterClassNames(Set<String> classNames, String packageName, boolean recursive) {

0 commit comments

Comments
 (0)