TimedSemaphore.java 5.91 KB
package org.apache.commons.lang3.concurrent;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.Validate;

public class TimedSemaphore
{
  public static final int NO_LIMIT = 0;
  private final ScheduledExecutorService a;
  private final long b;
  private final TimeUnit c;
  private final boolean d;
  private ScheduledFuture<?> e;
  private long f;
  private long g;
  private int h;
  private int i;
  private int j;
  private boolean k;
  
  public TimedSemaphore(long paramLong, TimeUnit paramTimeUnit, int paramInt)
  {
    this(null, paramLong, paramTimeUnit, paramInt);
  }
  
  public TimedSemaphore(ScheduledExecutorService paramScheduledExecutorService, long paramLong, TimeUnit paramTimeUnit, int paramInt)
  {
    Validate.inclusiveBetween(1L, Long.MAX_VALUE, paramLong, "Time period must be greater than 0!");
    this.b = paramLong;
    this.c = paramTimeUnit;
    if (paramScheduledExecutorService != null) {
      this.a = paramScheduledExecutorService;
    }
    for (this.d = false;; this.d = true)
    {
      setLimit(paramInt);
      return;
      paramScheduledExecutorService = new ScheduledThreadPoolExecutor(1);
      paramScheduledExecutorService.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
      paramScheduledExecutorService.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
      this.a = paramScheduledExecutorService;
    }
  }
  
  private void b()
  {
    if (isShutdown()) {
      throw new IllegalStateException("TimedSemaphore is shut down!");
    }
    if (this.e == null) {
      this.e = startTimer();
    }
  }
  
  private boolean c()
  {
    if ((getLimit() <= 0) || (this.i < getLimit()))
    {
      this.i += 1;
      return true;
    }
    return false;
  }
  
  final void a()
  {
    try
    {
      this.j = this.i;
      this.f += this.i;
      this.g += 1L;
      this.i = 0;
      notifyAll();
      return;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public void acquire()
    throws InterruptedException
  {
    try
    {
      b();
      boolean bool;
      do
      {
        bool = c();
        if (!bool) {
          wait();
        }
      } while (!bool);
      return;
    }
    finally {}
  }
  
  public int getAcquireCount()
  {
    try
    {
      int m = this.i;
      return m;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public int getAvailablePermits()
  {
    try
    {
      int m = getLimit();
      int n = getAcquireCount();
      return m - n;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  /* Error */
  public double getAverageCallsPerPeriod()
  {
    // Byte code:
    //   0: aload_0
    //   1: monitorenter
    //   2: aload_0
    //   3: getfield 97	org/apache/commons/lang3/concurrent/TimedSemaphore:g	J
    //   6: lstore_3
    //   7: lload_3
    //   8: lconst_0
    //   9: lcmp
    //   10: ifne +9 -> 19
    //   13: dconst_0
    //   14: dstore_1
    //   15: aload_0
    //   16: monitorexit
    //   17: dload_1
    //   18: dreturn
    //   19: aload_0
    //   20: getfield 95	org/apache/commons/lang3/concurrent/TimedSemaphore:f	J
    //   23: l2d
    //   24: dstore_1
    //   25: aload_0
    //   26: getfield 97	org/apache/commons/lang3/concurrent/TimedSemaphore:g	J
    //   29: lstore_3
    //   30: dload_1
    //   31: lload_3
    //   32: l2d
    //   33: ddiv
    //   34: dstore_1
    //   35: goto -20 -> 15
    //   38: astore 5
    //   40: aload_0
    //   41: monitorexit
    //   42: aload 5
    //   44: athrow
    // Local variable table:
    //   start	length	slot	name	signature
    //   0	45	0	this	TimedSemaphore
    //   14	21	1	d1	double
    //   6	26	3	l	long
    //   38	5	5	localObject	Object
    // Exception table:
    //   from	to	target	type
    //   2	7	38	finally
    //   19	30	38	finally
  }
  
  protected ScheduledExecutorService getExecutorService()
  {
    return this.a;
  }
  
  public int getLastAcquiresPerPeriod()
  {
    try
    {
      int m = this.j;
      return m;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public final int getLimit()
  {
    try
    {
      int m = this.h;
      return m;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public long getPeriod()
  {
    return this.b;
  }
  
  public TimeUnit getUnit()
  {
    return this.c;
  }
  
  public boolean isShutdown()
  {
    try
    {
      boolean bool = this.k;
      return bool;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public final void setLimit(int paramInt)
  {
    try
    {
      this.h = paramInt;
      return;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
  
  public void shutdown()
  {
    try
    {
      if (!this.k)
      {
        if (this.d) {
          getExecutorService().shutdownNow();
        }
        if (this.e != null) {
          this.e.cancel(false);
        }
        this.k = true;
      }
      return;
    }
    finally {}
  }
  
  protected ScheduledFuture<?> startTimer()
  {
    getExecutorService().scheduleAtFixedRate(new Runnable()
    {
      public final void run()
      {
        TimedSemaphore.this.a();
      }
    }, getPeriod(), getPeriod(), getUnit());
  }
  
  public boolean tryAcquire()
  {
    try
    {
      b();
      boolean bool = c();
      return bool;
    }
    finally
    {
      localObject = finally;
      throw ((Throwable)localObject);
    }
  }
}


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