Skip to content

Commit b046e22

Browse files
committed
Add Predicates
- Refactor to use Predicates, - Explicitly test deprecated code
1 parent 0d46368 commit b046e22

File tree

5 files changed

+131
-12
lines changed

5 files changed

+131
-12
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ The <action> type attribute can be add,update,fix,remove.
8888
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BasicThreadFactory.builder() and deprecate BasicThreadFactory.Builder().</action>
8989
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BasicThreadFactory.daemon().</action>
9090
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayUtils.startsWith.</action>
91+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Predicates.</action>
9192
<!-- UPDATE -->
9293
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 78 #1267, #1277, #1283, #1288, #1302.</action>
9394
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action>

src/main/java/org/apache/commons/lang3/ThreadUtils.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.stream.Collectors;
2626
import java.util.stream.Stream;
2727

28+
import org.apache.commons.lang3.function.Predicates;
2829
import org.apache.commons.lang3.time.DurationUtils;
2930

3031
/**
@@ -170,13 +171,6 @@ public interface ThreadPredicate {
170171
@Deprecated
171172
public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
172173

173-
private static final Predicate<?> ALWAYS_TRUE = t -> true;
174-
175-
@SuppressWarnings("unchecked")
176-
private static <T> Predicate<T> alwaysTruePredicate() {
177-
return (Predicate<T>) ALWAYS_TRUE;
178-
}
179-
180174
/**
181175
* Finds the active thread with the specified id.
182176
*
@@ -478,7 +472,7 @@ public static Collection<Thread> findThreadsByName(final String threadName, fina
478472
* thread groups from this thread's thread group up to the system thread group
479473
*/
480474
public static Collection<ThreadGroup> getAllThreadGroups() {
481-
return findThreadGroups(alwaysTruePredicate());
475+
return findThreadGroups(Predicates.truePredicate());
482476
}
483477

484478
/**
@@ -492,7 +486,7 @@ public static Collection<ThreadGroup> getAllThreadGroups() {
492486
* thread groups from this thread's thread group up to the system thread group
493487
*/
494488
public static Collection<Thread> getAllThreads() {
495-
return findThreads(alwaysTruePredicate());
489+
return findThreads(Predicates.truePredicate());
496490
}
497491

498492
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.function;
19+
20+
import java.util.function.Predicate;
21+
22+
/**
23+
* Factory for {@link Predicate}.
24+
*
25+
* @since 3.18.0
26+
*/
27+
public class Predicates {
28+
29+
private static final Predicate<?> ALWAYS_TRUE = t -> true;
30+
private static final Predicate<?> ALWAYS_FALSE = t -> false;
31+
32+
/**
33+
* Returns the Predicate singleton that always returns false.
34+
*
35+
* @param <T> the type of the input to the predicate.
36+
* @return the Predicate singleton.
37+
*/
38+
@SuppressWarnings("unchecked")
39+
// method name cannot be "false".
40+
public static <T> Predicate<T> falsePredicate() {
41+
return (Predicate<T>) ALWAYS_FALSE;
42+
}
43+
44+
/**
45+
* Returns the Predicate singleton that always returns true.
46+
*
47+
* @param <T> the type of the input to the predicate.
48+
* @return the Predicate singleton.
49+
*/
50+
@SuppressWarnings("unchecked")
51+
// method name cannot be "true".
52+
public static <T> Predicate<T> truePredicate() {
53+
return (Predicate<T>) ALWAYS_TRUE;
54+
}
55+
56+
/**
57+
* No instances needed.
58+
*/
59+
private Predicates() {
60+
// empty
61+
}
62+
}

src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import org.apache.commons.lang3.ThreadUtils.ThreadGroupPredicate;
4343
import org.apache.commons.lang3.ThreadUtils.ThreadPredicate;
44+
import org.apache.commons.lang3.function.Predicates;
4445
import org.junit.jupiter.api.Test;
4546

4647
/**
@@ -125,7 +126,7 @@ public void testComplexThreadGroups() throws Exception {
125126
}
126127
assertThat("getAllThreadGroups", ThreadUtils.getAllThreadGroups().size(), greaterThanOrEqualTo(7));
127128
assertThat("getAllThreads", ThreadUtils.getAllThreads().size(), greaterThanOrEqualTo(11));
128-
assertThat("findThreads(ThreadUtils.ALWAYS_TRUE_PREDICATE)", ThreadUtils.findThreads(ThreadUtils.ALWAYS_TRUE_PREDICATE).size(), greaterThanOrEqualTo(11));
129+
assertThat("findThreads(ThreadUtils.ALWAYS_TRUE_PREDICATE)", ThreadUtils.findThreads(Predicates.truePredicate()).size(), greaterThanOrEqualTo(11));
129130
assertEquals(1, ThreadUtils.findThreadsByName(t4.getName(), threadGroup3.getName()).size());
130131
assertEquals(0, ThreadUtils.findThreadsByName(t4.getName(), threadGroup2.getName()).size());
131132
assertEquals(2, ThreadUtils.findThreadsByName(t11.getName(), threadGroup7.getName()).size());
@@ -152,6 +153,18 @@ public void testConstructor() {
152153
assertFalse(Modifier.isFinal(ThreadUtils.class.getModifiers()));
153154
}
154155

156+
@SuppressWarnings("deprecation")
157+
@Test
158+
public void testDepreacted() {
159+
assertNotNull(ThreadUtils.ALWAYS_TRUE_PREDICATE);
160+
ThreadPredicate tp = ThreadUtils.ALWAYS_TRUE_PREDICATE;
161+
assertTrue(tp.test(null));
162+
assertTrue(tp.test(new Thread()));
163+
ThreadGroupPredicate tgp = ThreadUtils.ALWAYS_TRUE_PREDICATE;
164+
assertTrue(tgp.test(null));
165+
assertTrue(tgp.test(new ThreadGroup("")));
166+
}
167+
155168
@Test
156169
public void testGetAllThreadGroupsDoesNotReturnNull() {
157170
// LANG-1706 getAllThreadGroups and findThreadGroups should not return null items
@@ -299,8 +312,8 @@ public void testThreadGroupsByIdFail() {
299312

300313
@Test
301314
public void testThreadGroupsNullParent() {
302-
assertThrows(NullPointerException.class, () -> ThreadUtils.findThreadGroups(null, true, ThreadUtils.ALWAYS_TRUE_PREDICATE));
303-
assertThrows(NullPointerException.class, () -> ThreadUtils.findThreadGroups(null, false, ThreadUtils.ALWAYS_TRUE_PREDICATE));
315+
assertThrows(NullPointerException.class, () -> ThreadUtils.findThreadGroups(null, true, Predicates.truePredicate()));
316+
assertThrows(NullPointerException.class, () -> ThreadUtils.findThreadGroups(null, false, Predicates.truePredicate()));
304317
}
305318

306319
@Test
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.lang3.function;
19+
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
import java.util.function.Predicate;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
/**
28+
* Tests {@link Predicates}.
29+
*/
30+
public class PredicatesTest {
31+
32+
@Test
33+
public void testFalsePredicate() {
34+
assertFalse(Predicates.falsePredicate().test(null));
35+
assertFalse(Predicates.falsePredicate().test(new Object()));
36+
final Predicate<String> stringPredicate = Predicates.falsePredicate();
37+
assertFalse(stringPredicate.test(null));
38+
assertFalse(stringPredicate.test(""));
39+
}
40+
41+
@Test
42+
public void testTruePredicate() {
43+
assertTrue(Predicates.truePredicate().test(null));
44+
assertTrue(Predicates.truePredicate().test(new Object()));
45+
final Predicate<String> stringPredicate = Predicates.truePredicate();
46+
assertTrue(stringPredicate.test(null));
47+
assertTrue(stringPredicate.test(""));
48+
}
49+
}

0 commit comments

Comments
 (0)