Statement unnecessarily nested within else clause.

This commit is contained in:
ggregory 2016-02-24 22:08:37 -08:00
parent b2ec67b54a
commit 5e5ba1b812
1 changed files with 460 additions and 466 deletions

View File

@ -1,466 +1,460 @@
/* /*
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with * contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. * this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0 * The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with * (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at * the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* <p> * <p>
* Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}. * Helpers for {@code java.lang.Thread} and {@code java.lang.ThreadGroup}.
* </p> * </p>
* <p> * <p>
* #ThreadSafe# * #ThreadSafe#
* </p> * </p>
* *
* @see java.lang.Thread * @see java.lang.Thread
* @see java.lang.ThreadGroup * @see java.lang.ThreadGroup
* @since 3.5 * @since 3.5
*/ */
public class ThreadUtils { public class ThreadUtils {
/** /**
* Return the active thread with the specified id if it belong's to the specified thread group. * Return the active thread with the specified id if it belong's to the specified thread group.
* *
* @param threadId The thread id * @param threadId The thread id
* @param threadGroup The thread group * @param threadGroup The thread group
* @return The thread which belongs to a specified thread group and the thread's id match the specified id. * @return The thread which belongs to a specified thread group and the thread's id match the specified id.
* {@code null} is returned if no such thread exists * {@code null} is returned if no such thread exists
* @throws IllegalArgumentException if the specified id is zero or negative or the group is null * @throws IllegalArgumentException if the specified id is zero or negative or the group is null
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) { public static Thread findThreadById(final long threadId, final ThreadGroup threadGroup) {
if (threadGroup == null) { if (threadGroup == null) {
throw new IllegalArgumentException("The thread group must not be null"); throw new IllegalArgumentException("The thread group must not be null");
} }
final Thread thread = findThreadById(threadId); final Thread thread = findThreadById(threadId);
if(thread != null && threadGroup.equals(thread.getThreadGroup())) { if(thread != null && threadGroup.equals(thread.getThreadGroup())) {
return thread; return thread;
} }
return null; return null;
} }
/** /**
* Return the active thread with the specified id if it belong's to a thread group with the specified group name. * Return the active thread with the specified id if it belong's to a thread group with the specified group name.
* *
* @param threadId The thread id * @param threadId The thread id
* @param threadGroupName The thread group name * @param threadGroupName The thread group name
* @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id. * @return The threads which belongs to a thread group with the specified group name and the thread's id match the specified id.
* {@code null} is returned if no such thread exists * {@code null} is returned if no such thread exists
* @throws IllegalArgumentException if the specified id is zero or negative or the group name is null * @throws IllegalArgumentException if the specified id is zero or negative or the group name is null
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Thread findThreadById(final long threadId, final String threadGroupName) { public static Thread findThreadById(final long threadId, final String threadGroupName) {
if (threadGroupName == null) { if (threadGroupName == null) {
throw new IllegalArgumentException("The thread group name must not be null"); throw new IllegalArgumentException("The thread group name must not be null");
} }
final Thread thread = findThreadById(threadId); final Thread thread = findThreadById(threadId);
if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) { if(thread != null && thread.getThreadGroup() != null && thread.getThreadGroup().getName().equals(threadGroupName)) {
return thread; return thread;
} }
return null; return null;
} }
/** /**
* Return active threads with the specified name if they belong to a specified thread group. * Return active threads with the specified name if they belong to a specified thread group.
* *
* @param threadName The thread name * @param threadName The thread name
* @param threadGroup The thread group * @param threadGroup The thread group
* @return The threads which belongs to a thread group and the thread's name match the specified name, * @return The threads which belongs to a thread group and the thread's name match the specified name,
* An empty collection is returned if no such thread exists. The collection returned is always unmodifiable. * An empty collection is returned if no such thread exists. The collection returned is always unmodifiable.
* @throws IllegalArgumentException if the specified thread name or group is null * @throws IllegalArgumentException if the specified thread name or group is null
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) { public static Collection<Thread> findThreadsByName(final String threadName, final ThreadGroup threadGroup) {
return findThreads(threadGroup, false, new NamePredicate(threadName)); return findThreads(threadGroup, false, new NamePredicate(threadName));
} }
/** /**
* Return active threads with the specified name if they belong to a thread group with the specified group name. * Return active threads with the specified name if they belong to a thread group with the specified group name.
* *
* @param threadName The thread name * @param threadName The thread name
* @param threadGroupName The thread group name * @param threadGroupName The thread group name
* @return The threads which belongs to a thread group with the specified group name and the thread's name match the specified name, * @return The threads which belongs to a thread group with the specified group name and the thread's name match the specified name,
* An empty collection is returned if no such thread exists. The collection returned is always unmodifiable. * An empty collection is returned if no such thread exists. The collection returned is always unmodifiable.
* @throws IllegalArgumentException if the specified thread name or group name is null * @throws IllegalArgumentException if the specified thread name or group name is null
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) { public static Collection<Thread> findThreadsByName(final String threadName, final String threadGroupName) {
if (threadName == null) { if (threadName == null) {
throw new IllegalArgumentException("The thread name must not be null"); throw new IllegalArgumentException("The thread name must not be null");
} }
if (threadGroupName == null) { if (threadGroupName == null) {
throw new IllegalArgumentException("The thread group name must not be null"); throw new IllegalArgumentException("The thread group name must not be null");
} }
final Collection<ThreadGroup> threadGroups = findThreadGroups(new NamePredicate(threadGroupName)); final Collection<ThreadGroup> threadGroups = findThreadGroups(new NamePredicate(threadGroupName));
if(threadGroups.isEmpty()) { if(threadGroups.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
final Collection<Thread> result = new ArrayList<Thread>(); final Collection<Thread> result = new ArrayList<Thread>();
final NamePredicate threadNamePredicate = new NamePredicate(threadName); final NamePredicate threadNamePredicate = new NamePredicate(threadName);
for(final ThreadGroup group : threadGroups) { for(final ThreadGroup group : threadGroups) {
result.addAll(findThreads(group, false, threadNamePredicate)); result.addAll(findThreads(group, false, threadNamePredicate));
} }
return Collections.unmodifiableCollection(result); return Collections.unmodifiableCollection(result);
} }
/** /**
* Return active thread groups with the specified group name. * Return active thread groups with the specified group name.
* *
* @param threadGroupName The thread group name * @param threadGroupName The thread group name
* @return the thread groups with the specified group name or an empty collection if no such thread group exists. The collection returned is always unmodifiable. * @return the thread groups with the specified group name or an empty collection if no such thread group exists. The collection returned is always unmodifiable.
* @throws IllegalArgumentException if group name is null * @throws IllegalArgumentException if group name is null
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) { public static Collection<ThreadGroup> findThreadGroupsByName(final String threadGroupName) {
return findThreadGroups(new NamePredicate(threadGroupName)); return findThreadGroups(new NamePredicate(threadGroupName));
} }
/** /**
* Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed). * Return all active thread groups excluding the system thread group (A thread group is active if it has been not destroyed).
* *
* @return all thread groups excluding the system thread group. The collection returned is always unmodifiable. * @return all thread groups excluding the system thread group. The collection returned is always unmodifiable.
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Collection<ThreadGroup> getAllThreadGroups() { public static Collection<ThreadGroup> getAllThreadGroups() {
return findThreadGroups(ALWAYS_TRUE_PREDICATE); return findThreadGroups(ALWAYS_TRUE_PREDICATE);
} }
/** /**
* Return the system thread group (sometimes also referred as "root thread group"). * Return the system thread group (sometimes also referred as "root thread group").
* *
* @return the system thread group * @return the system thread group
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static ThreadGroup getSystemThreadGroup() { public static ThreadGroup getSystemThreadGroup() {
ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
while(threadGroup.getParent() != null) { while(threadGroup.getParent() != null) {
threadGroup = threadGroup.getParent(); threadGroup = threadGroup.getParent();
} }
return threadGroup; return threadGroup;
} }
/** /**
* Return all active threads (A thread is active if it has been started and has not yet died). * Return all active threads (A thread is active if it has been started and has not yet died).
* *
* @return all active threads. The collection returned is always unmodifiable. * @return all active threads. The collection returned is always unmodifiable.
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Collection<Thread> getAllThreads() { public static Collection<Thread> getAllThreads() {
return findThreads(ALWAYS_TRUE_PREDICATE); return findThreads(ALWAYS_TRUE_PREDICATE);
} }
/** /**
* Return active threads with the specified name. * Return active threads with the specified name.
* *
* @param threadName The thread name * @param threadName The thread name
* @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable. * @return The threads with the specified name or an empty collection if no such thread exists. The collection returned is always unmodifiable.
* @throws IllegalArgumentException if the specified name is null * @throws IllegalArgumentException if the specified name is null
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Collection<Thread> findThreadsByName(final String threadName) { public static Collection<Thread> findThreadsByName(final String threadName) {
return findThreads(new NamePredicate(threadName)); return findThreads(new NamePredicate(threadName));
} }
/** /**
* Return the active thread with the specified id. * Return the active thread with the specified id.
* *
* @param threadId The thread id * @param threadId The thread id
* @return The thread with the specified id or {@code null} if no such thread exists * @return The thread with the specified id or {@code null} if no such thread exists
* @throws IllegalArgumentException if the specified id is zero or negative * @throws IllegalArgumentException if the specified id is zero or negative
* @throws SecurityException * @throws SecurityException
* if the current thread cannot access the system thread group * if the current thread cannot access the system thread group
* *
* @throws SecurityException if the current thread cannot modify * @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group * thread groups from this thread's thread group up to the system thread group
*/ */
public static Thread findThreadById(final long threadId) { public static Thread findThreadById(final long threadId) {
final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId)); final Collection<Thread> result = findThreads(new ThreadIdPredicate(threadId));
return result.isEmpty() ? null : result.iterator().next();
if(result.isEmpty()) { }
return null;
} else { /**
return result.iterator().next(); * <p>
} * ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
* {@code ThreadUtils.getAllThreads()}
} * </p>
* <p>
/** * This constructor is public to permit tools that require a JavaBean instance to operate.
* <p> * </p>
* ThreadUtils instances should NOT be constructed in standard programming. Instead, the class should be used as */
* {@code ThreadUtils.getAllThreads()} public ThreadUtils() {
* </p> super();
* <p> }
* This constructor is public to permit tools that require a JavaBean instance to operate.
* </p> /**
*/ * A predicate for selecting threads.
public ThreadUtils() { */
super(); //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
} public interface ThreadPredicate /*extends java.util.function.Predicate<Thread>*/{
/** /**
* A predicate for selecting threads. * Evaluates this predicate on the given thread.
*/ * @param thread the thread
//if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate * @return {@code true} if the thread matches the predicate, otherwise {@code false}
public interface ThreadPredicate /*extends java.util.function.Predicate<Thread>*/{ */
boolean test(Thread thread);
/** }
* Evaluates this predicate on the given thread.
* @param thread the thread /**
* @return {@code true} if the thread matches the predicate, otherwise {@code false} * A predicate for selecting threadgroups.
*/ */
boolean test(Thread thread); //if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate
} public interface ThreadGroupPredicate /*extends java.util.function.Predicate<ThreadGroup>*/{
/** /**
* A predicate for selecting threadgroups. * Evaluates this predicate on the given threadgroup.
*/ * @param threadGroup the threadgroup
//if java minimal version for lang becomes 1.8 extend this interface from java.util.function.Predicate * @return {@code true} if the threadGroup matches the predicate, otherwise {@code false}
public interface ThreadGroupPredicate /*extends java.util.function.Predicate<ThreadGroup>*/{ */
boolean test(ThreadGroup threadGroup);
/** }
* Evaluates this predicate on the given threadgroup.
* @param threadGroup the threadgroup /**
* @return {@code true} if the threadGroup matches the predicate, otherwise {@code false} * Predicate which always returns true.
*/ */
boolean test(ThreadGroup threadGroup); public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
}
/**
/** * A predicate implementation which always returns true.
* Predicate which always returns true. */
*/ private final static class AlwaysTruePredicate implements ThreadPredicate, ThreadGroupPredicate{
public static final AlwaysTruePredicate ALWAYS_TRUE_PREDICATE = new AlwaysTruePredicate();
private AlwaysTruePredicate() {
/** }
* A predicate implementation which always returns true.
*/ @Override
private final static class AlwaysTruePredicate implements ThreadPredicate, ThreadGroupPredicate{ public boolean test(final ThreadGroup threadGroup) {
return true;
private AlwaysTruePredicate() { }
}
@Override
@Override public boolean test(final Thread thread) {
public boolean test(final ThreadGroup threadGroup) { return true;
return true; }
} }
@Override /**
public boolean test(final Thread thread) { * A predicate implementation which matches a thread or threadgroup name.
return true; */
} public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate {
}
private final String name;
/**
* A predicate implementation which matches a thread or threadgroup name. /**
*/ * Predicate constructor
public static class NamePredicate implements ThreadPredicate, ThreadGroupPredicate { *
* @param name thread or threadgroup name
private final String name; * @throws IllegalArgumentException if the name is {@code null}
*/
/** public NamePredicate(final String name) {
* Predicate constructor super();
* if (name == null) {
* @param name thread or threadgroup name throw new IllegalArgumentException("The name must not be null");
* @throws IllegalArgumentException if the name is {@code null} }
*/ this.name = name;
public NamePredicate(final String name) { }
super();
if (name == null) { @Override
throw new IllegalArgumentException("The name must not be null"); public boolean test(final ThreadGroup threadGroup) {
} return threadGroup != null && threadGroup.getName().equals(name);
this.name = name; }
}
@Override
@Override public boolean test(final Thread thread) {
public boolean test(final ThreadGroup threadGroup) { return thread != null && thread.getName().equals(name);
return threadGroup != null && threadGroup.getName().equals(name); }
} }
@Override /**
public boolean test(final Thread thread) { * A predicate implementation which matches a thread id.
return thread != null && thread.getName().equals(name); */
} public static class ThreadIdPredicate implements ThreadPredicate {
}
private final long threadId;
/**
* A predicate implementation which matches a thread id. /**
*/ * Predicate constructor
public static class ThreadIdPredicate implements ThreadPredicate { *
* @param threadId the threadId to match
private final long threadId; * @throws IllegalArgumentException if the threadId is zero or negative
*/
/** public ThreadIdPredicate(final long threadId) {
* Predicate constructor super();
* if (threadId <= 0) {
* @param threadId the threadId to match throw new IllegalArgumentException("The thread id must be greater than zero");
* @throws IllegalArgumentException if the threadId is zero or negative }
*/ this.threadId = threadId;
public ThreadIdPredicate(final long threadId) { }
super();
if (threadId <= 0) { @Override
throw new IllegalArgumentException("The thread id must be greater than zero"); public boolean test(final Thread thread) {
} return thread != null && thread.getId() == threadId;
this.threadId = threadId; }
} }
@Override /**
public boolean test(final Thread thread) { * Select all active threads which match the given predicate.
return thread != null && thread.getId() == threadId; *
} * @param predicate the predicate
} * @return An unmodifiable {@code Collection} of active threads matching the given predicate
*
/** * @throws IllegalArgumentException if the predicate is null
* Select all active threads which match the given predicate. * @throws SecurityException
* * if the current thread cannot access the system thread group
* @param predicate the predicate * @throws SecurityException if the current thread cannot modify
* @return An unmodifiable {@code Collection} of active threads matching the given predicate * thread groups from this thread's thread group up to the system thread group
* */
* @throws IllegalArgumentException if the predicate is null public static Collection<Thread> findThreads(final ThreadPredicate predicate){
* @throws SecurityException return findThreads(getSystemThreadGroup(), true, predicate);
* if the current thread cannot access the system thread group }
* @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group /**
*/ * Select all active threadgroups which match the given predicate.
public static Collection<Thread> findThreads(final ThreadPredicate predicate){ *
return findThreads(getSystemThreadGroup(), true, predicate); * @param predicate the predicate
} * @return An unmodifiable {@code Collection} of active threadgroups matching the given predicate
* @throws IllegalArgumentException if the predicate is null
/** * @throws SecurityException
* Select all active threadgroups which match the given predicate. * if the current thread cannot access the system thread group
* * @throws SecurityException if the current thread cannot modify
* @param predicate the predicate * thread groups from this thread's thread group up to the system thread group
* @return An unmodifiable {@code Collection} of active threadgroups matching the given predicate */
* @throws IllegalArgumentException if the predicate is null public static Collection<ThreadGroup> findThreadGroups(final ThreadGroupPredicate predicate){
* @throws SecurityException return findThreadGroups(getSystemThreadGroup(), true, predicate);
* if the current thread cannot access the system thread group }
* @throws SecurityException if the current thread cannot modify
* thread groups from this thread's thread group up to the system thread group /**
*/ * Select all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups).
public static Collection<ThreadGroup> findThreadGroups(final ThreadGroupPredicate predicate){ *
return findThreadGroups(getSystemThreadGroup(), true, predicate); * @param group the thread group
} * @param recurse if {@code true} then evaluate the predicate recursively on all threads in all subgroups of the given group
* @param predicate the predicate
/** * @return An unmodifiable {@code Collection} of active threads which match the given predicate and which belongs to the given thread group
* Select all active threads which match the given predicate and which belongs to the given thread group (or one of its subgroups). * @throws IllegalArgumentException if the given group or predicate is null
* * @throws SecurityException if the current thread cannot modify
* @param group the thread group * thread groups from this thread's thread group up to the system thread group
* @param recurse if {@code true} then evaluate the predicate recursively on all threads in all subgroups of the given group */
* @param predicate the predicate public static Collection<Thread> findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) {
* @return An unmodifiable {@code Collection} of active threads which match the given predicate and which belongs to the given thread group if (group == null) {
* @throws IllegalArgumentException if the given group or predicate is null throw new IllegalArgumentException("The group must not be null");
* @throws SecurityException if the current thread cannot modify }
* thread groups from this thread's thread group up to the system thread group if (predicate == null) {
*/ throw new IllegalArgumentException("The predicate must not be null");
public static Collection<Thread> findThreads(final ThreadGroup group, final boolean recurse, final ThreadPredicate predicate) { }
if (group == null) {
throw new IllegalArgumentException("The group must not be null"); int count = group.activeCount();
} Thread[] threads;
if (predicate == null) { do {
throw new IllegalArgumentException("The predicate must not be null"); threads = new Thread[count + (count / 2) + 1]; //slightly grow the array size
} count = group.enumerate(threads, recurse);
//return value of enumerate() must be strictly less than the array size according to javadoc
int count = group.activeCount(); } while (count >= threads.length);
Thread[] threads;
do { final List<Thread> result = new ArrayList<Thread>(count);
threads = new Thread[count + (count / 2) + 1]; //slightly grow the array size for (int i = 0; i < count; ++i) {
count = group.enumerate(threads, recurse); if (predicate.test(threads[i])) {
//return value of enumerate() must be strictly less than the array size according to javadoc result.add(threads[i]);
} while (count >= threads.length); }
}
final List<Thread> result = new ArrayList<Thread>(count); return Collections.unmodifiableCollection(result);
for (int i = 0; i < count; ++i) { }
if (predicate.test(threads[i])) {
result.add(threads[i]); /**
} * Select all active threadgroups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups).
} *
return Collections.unmodifiableCollection(result); * @param group the thread group
} * @param recurse if {@code true} then evaluate the predicate recursively on all threadgroups in all subgroups of the given group
* @param predicate the predicate
/** * @return An unmodifiable {@code Collection} of active threadgroups which match the given predicate and which is a subgroup of the given thread group
* Select all active threadgroups which match the given predicate and which is a subgroup of the given thread group (or one of its subgroups). * @throws IllegalArgumentException if the given group or predicate is null
* * @throws SecurityException if the current thread cannot modify
* @param group the thread group * thread groups from this thread's thread group up to the system thread group
* @param recurse if {@code true} then evaluate the predicate recursively on all threadgroups in all subgroups of the given group */
* @param predicate the predicate public static Collection<ThreadGroup> findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate){
* @return An unmodifiable {@code Collection} of active threadgroups which match the given predicate and which is a subgroup of the given thread group if (group == null) {
* @throws IllegalArgumentException if the given group or predicate is null throw new IllegalArgumentException("The group must not be null");
* @throws SecurityException if the current thread cannot modify }
* thread groups from this thread's thread group up to the system thread group if (predicate == null) {
*/ throw new IllegalArgumentException("The predicate must not be null");
public static Collection<ThreadGroup> findThreadGroups(final ThreadGroup group, final boolean recurse, final ThreadGroupPredicate predicate){ }
if (group == null) {
throw new IllegalArgumentException("The group must not be null"); int count = group.activeGroupCount();
} ThreadGroup[] threadGroups;
if (predicate == null) { do {
throw new IllegalArgumentException("The predicate must not be null"); threadGroups = new ThreadGroup[count + (count / 2) + 1]; //slightly grow the array size
} count = group.enumerate(threadGroups, recurse);
//return value of enumerate() must be strictly less than the array size according to javadoc
int count = group.activeGroupCount(); } while(count >= threadGroups.length);
ThreadGroup[] threadGroups;
do { final List<ThreadGroup> result = new ArrayList<ThreadGroup>(count);
threadGroups = new ThreadGroup[count + (count / 2) + 1]; //slightly grow the array size for(int i = 0; i < count; ++i) {
count = group.enumerate(threadGroups, recurse); if(predicate.test(threadGroups[i])) {
//return value of enumerate() must be strictly less than the array size according to javadoc result.add(threadGroups[i]);
} while(count >= threadGroups.length); }
}
final List<ThreadGroup> result = new ArrayList<ThreadGroup>(count); return Collections.unmodifiableCollection(result);
for(int i = 0; i < count; ++i) { }
if(predicate.test(threadGroups[i])) { }
result.add(threadGroups[i]);
}
}
return Collections.unmodifiableCollection(result);
}
}