ThreadUtils.java 7.21 KB
package org.apache.commons.lang3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ThreadUtils
{
  public static final a ALWAYS_TRUE_PREDICATE = new a((byte)0);
  
  public static Thread findThreadById(long paramLong)
  {
    Collection localCollection = findThreads(new ThreadIdPredicate(paramLong));
    if (localCollection.isEmpty()) {
      return null;
    }
    return (Thread)localCollection.iterator().next();
  }
  
  public static Thread findThreadById(long paramLong, String paramString)
  {
    if (paramString == null) {
      throw new IllegalArgumentException("The thread group name must not be null");
    }
    Thread localThread = findThreadById(paramLong);
    if ((localThread != null) && (localThread.getThreadGroup() != null) && (localThread.getThreadGroup().getName().equals(paramString))) {
      return localThread;
    }
    return null;
  }
  
  public static Thread findThreadById(long paramLong, ThreadGroup paramThreadGroup)
  {
    if (paramThreadGroup == null) {
      throw new IllegalArgumentException("The thread group must not be null");
    }
    Thread localThread = findThreadById(paramLong);
    if ((localThread != null) && (paramThreadGroup.equals(localThread.getThreadGroup()))) {
      return localThread;
    }
    return null;
  }
  
  public static Collection<ThreadGroup> findThreadGroups(ThreadGroup paramThreadGroup, boolean paramBoolean, ThreadGroupPredicate paramThreadGroupPredicate)
  {
    if (paramThreadGroup == null) {
      throw new IllegalArgumentException("The group must not be null");
    }
    if (paramThreadGroupPredicate == null) {
      throw new IllegalArgumentException("The predicate must not be null");
    }
    int i = paramThreadGroup.activeGroupCount();
    ThreadGroup[] arrayOfThreadGroup;
    int j;
    do
    {
      arrayOfThreadGroup = new ThreadGroup[i + i / 2 + 1];
      j = paramThreadGroup.enumerate(arrayOfThreadGroup, paramBoolean);
      i = j;
    } while (j >= arrayOfThreadGroup.length);
    paramThreadGroup = new ArrayList(j);
    i = 0;
    while (i < j)
    {
      if (paramThreadGroupPredicate.test(arrayOfThreadGroup[i])) {
        paramThreadGroup.add(arrayOfThreadGroup[i]);
      }
      i += 1;
    }
    return Collections.unmodifiableCollection(paramThreadGroup);
  }
  
  public static Collection<ThreadGroup> findThreadGroups(ThreadGroupPredicate paramThreadGroupPredicate)
  {
    return findThreadGroups(getSystemThreadGroup(), true, paramThreadGroupPredicate);
  }
  
  public static Collection<ThreadGroup> findThreadGroupsByName(String paramString)
  {
    return findThreadGroups(new NamePredicate(paramString));
  }
  
  public static Collection<Thread> findThreads(ThreadGroup paramThreadGroup, boolean paramBoolean, ThreadPredicate paramThreadPredicate)
  {
    if (paramThreadGroup == null) {
      throw new IllegalArgumentException("The group must not be null");
    }
    if (paramThreadPredicate == null) {
      throw new IllegalArgumentException("The predicate must not be null");
    }
    int i = paramThreadGroup.activeCount();
    Thread[] arrayOfThread;
    int j;
    do
    {
      arrayOfThread = new Thread[i + i / 2 + 1];
      j = paramThreadGroup.enumerate(arrayOfThread, paramBoolean);
      i = j;
    } while (j >= arrayOfThread.length);
    paramThreadGroup = new ArrayList(j);
    i = 0;
    while (i < j)
    {
      if (paramThreadPredicate.test(arrayOfThread[i])) {
        paramThreadGroup.add(arrayOfThread[i]);
      }
      i += 1;
    }
    return Collections.unmodifiableCollection(paramThreadGroup);
  }
  
  public static Collection<Thread> findThreads(ThreadPredicate paramThreadPredicate)
  {
    return findThreads(getSystemThreadGroup(), true, paramThreadPredicate);
  }
  
  public static Collection<Thread> findThreadsByName(String paramString)
  {
    return findThreads(new NamePredicate(paramString));
  }
  
  public static Collection<Thread> findThreadsByName(String paramString1, String paramString2)
  {
    if (paramString1 == null) {
      throw new IllegalArgumentException("The thread name must not be null");
    }
    if (paramString2 == null) {
      throw new IllegalArgumentException("The thread group name must not be null");
    }
    Object localObject = findThreadGroups(new NamePredicate(paramString2));
    if (((Collection)localObject).isEmpty()) {
      return Collections.emptyList();
    }
    paramString2 = new ArrayList();
    paramString1 = new NamePredicate(paramString1);
    localObject = ((Collection)localObject).iterator();
    while (((Iterator)localObject).hasNext()) {
      paramString2.addAll(findThreads((ThreadGroup)((Iterator)localObject).next(), false, paramString1));
    }
    return Collections.unmodifiableCollection(paramString2);
  }
  
  public static Collection<Thread> findThreadsByName(String paramString, ThreadGroup paramThreadGroup)
  {
    return findThreads(paramThreadGroup, false, new NamePredicate(paramString));
  }
  
  public static Collection<ThreadGroup> getAllThreadGroups()
  {
    return findThreadGroups(ALWAYS_TRUE_PREDICATE);
  }
  
  public static Collection<Thread> getAllThreads()
  {
    return findThreads(ALWAYS_TRUE_PREDICATE);
  }
  
  public static ThreadGroup getSystemThreadGroup()
  {
    for (ThreadGroup localThreadGroup = Thread.currentThread().getThreadGroup(); localThreadGroup.getParent() != null; localThreadGroup = localThreadGroup.getParent()) {}
    return localThreadGroup;
  }
  
  public static class NamePredicate
    implements ThreadUtils.ThreadGroupPredicate, ThreadUtils.ThreadPredicate
  {
    private final String a;
    
    public NamePredicate(String paramString)
    {
      if (paramString == null) {
        throw new IllegalArgumentException("The name must not be null");
      }
      this.a = paramString;
    }
    
    public boolean test(Thread paramThread)
    {
      return (paramThread != null) && (paramThread.getName().equals(this.a));
    }
    
    public boolean test(ThreadGroup paramThreadGroup)
    {
      return (paramThreadGroup != null) && (paramThreadGroup.getName().equals(this.a));
    }
  }
  
  public static abstract interface ThreadGroupPredicate
  {
    public abstract boolean test(ThreadGroup paramThreadGroup);
  }
  
  public static class ThreadIdPredicate
    implements ThreadUtils.ThreadPredicate
  {
    private final long a;
    
    public ThreadIdPredicate(long paramLong)
    {
      if (paramLong <= 0L) {
        throw new IllegalArgumentException("The thread id must be greater than zero");
      }
      this.a = paramLong;
    }
    
    public boolean test(Thread paramThread)
    {
      return (paramThread != null) && (paramThread.getId() == this.a);
    }
  }
  
  public static abstract interface ThreadPredicate
  {
    public abstract boolean test(Thread paramThread);
  }
  
  static final class a
    implements ThreadUtils.ThreadGroupPredicate, ThreadUtils.ThreadPredicate
  {
    public final boolean test(Thread paramThread)
    {
      return true;
    }
    
    public final boolean test(ThreadGroup paramThreadGroup)
    {
      return true;
    }
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/org/apache/commons/lang3/ThreadUtils.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */