CircularArray.java 4.05 KB
package android.support.v4.util;

public final class CircularArray<E>
{
  private E[] a;
  private int b;
  private int c;
  private int d;
  
  public CircularArray()
  {
    this(8);
  }
  
  public CircularArray(int paramInt)
  {
    if (paramInt <= 0) {
      throw new IllegalArgumentException("capacity must be >= 1");
    }
    if (paramInt > 1073741824) {
      throw new IllegalArgumentException("capacity must be <= 2^30");
    }
    int i = paramInt;
    if (Integer.bitCount(paramInt) != 1) {
      i = Integer.highestOneBit(paramInt - 1) << 1;
    }
    this.d = (i - 1);
    this.a = ((Object[])new Object[i]);
  }
  
  private void a()
  {
    int i = this.a.length;
    int j = i - this.b;
    int k = i << 1;
    if (k < 0) {
      throw new RuntimeException("Max array capacity exceeded");
    }
    Object[] arrayOfObject = new Object[k];
    System.arraycopy(this.a, this.b, arrayOfObject, 0, j);
    System.arraycopy(this.a, 0, arrayOfObject, j, this.b);
    this.a = ((Object[])arrayOfObject);
    this.b = 0;
    this.c = i;
    this.d = (k - 1);
  }
  
  public final void addFirst(E paramE)
  {
    this.b = (this.b - 1 & this.d);
    this.a[this.b] = paramE;
    if (this.b == this.c) {
      a();
    }
  }
  
  public final void addLast(E paramE)
  {
    this.a[this.c] = paramE;
    this.c = (this.c + 1 & this.d);
    if (this.c == this.b) {
      a();
    }
  }
  
  public final void clear()
  {
    removeFromStart(size());
  }
  
  public final E get(int paramInt)
  {
    if ((paramInt < 0) || (paramInt >= size())) {
      throw new ArrayIndexOutOfBoundsException();
    }
    return (E)this.a[(this.b + paramInt & this.d)];
  }
  
  public final E getFirst()
  {
    if (this.b == this.c) {
      throw new ArrayIndexOutOfBoundsException();
    }
    return (E)this.a[this.b];
  }
  
  public final E getLast()
  {
    if (this.b == this.c) {
      throw new ArrayIndexOutOfBoundsException();
    }
    return (E)this.a[(this.c - 1 & this.d)];
  }
  
  public final boolean isEmpty()
  {
    return this.b == this.c;
  }
  
  public final E popFirst()
  {
    if (this.b == this.c) {
      throw new ArrayIndexOutOfBoundsException();
    }
    Object localObject = this.a[this.b];
    this.a[this.b] = null;
    this.b = (this.b + 1 & this.d);
    return (E)localObject;
  }
  
  public final E popLast()
  {
    if (this.b == this.c) {
      throw new ArrayIndexOutOfBoundsException();
    }
    int i = this.c - 1 & this.d;
    Object localObject = this.a[i];
    this.a[i] = null;
    this.c = i;
    return (E)localObject;
  }
  
  public final void removeFromEnd(int paramInt)
  {
    if (paramInt <= 0) {}
    do
    {
      return;
      if (paramInt > size()) {
        throw new ArrayIndexOutOfBoundsException();
      }
      i = 0;
      if (paramInt < this.c) {
        i = this.c - paramInt;
      }
      int j = i;
      while (j < this.c)
      {
        this.a[j] = null;
        j += 1;
      }
      i = this.c - i;
      paramInt -= i;
      this.c -= i;
    } while (paramInt <= 0);
    this.c = this.a.length;
    int i = this.c - paramInt;
    paramInt = i;
    while (paramInt < this.c)
    {
      this.a[paramInt] = null;
      paramInt += 1;
    }
    this.c = i;
  }
  
  public final void removeFromStart(int paramInt)
  {
    if (paramInt <= 0) {}
    int i;
    do
    {
      return;
      if (paramInt > size()) {
        throw new ArrayIndexOutOfBoundsException();
      }
      int j = this.a.length;
      i = j;
      if (paramInt < j - this.b) {
        i = this.b + paramInt;
      }
      j = this.b;
      while (j < i)
      {
        this.a[j] = null;
        j += 1;
      }
      j = i - this.b;
      i = paramInt - j;
      this.b = (j + this.b & this.d);
    } while (i <= 0);
    paramInt = 0;
    while (paramInt < i)
    {
      this.a[paramInt] = null;
      paramInt += 1;
    }
    this.b = i;
  }
  
  public final int size()
  {
    return this.c - this.b & this.d;
  }
}


/* Location:              /home/merong/decompile/hackery-dex2jar.jar!/android/support/v4/util/CircularArray.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */