package com.worklight.location.internal.events.storage;

import java.util.ArrayList;
import java.util.List;

public class ChunkStorage
  implements IChunkStorage
{
  private final List<Chunk> closedChunks = new ArrayList();
  private long currentMemSize;
  private long maxMemSize;
  private final IChunkStorage persistentStorage;

  public ChunkStorage(long paramLong, IChunkStorage paramIChunkStorage)
  {
    if (paramLong < 0L)
      throw new IllegalArgumentException("Maximum memory size must be a positive integer: " + paramLong);
    this.maxMemSize = paramLong;
    if (paramIChunkStorage == null)
      throw new IllegalArgumentException("persistentStorage is null");
    this.persistentStorage = paramIChunkStorage;
  }

  private void fillFromPersistentToMemory()
  {
    while (true)
    {
      Chunk localChunk;
      if (!this.persistentStorage.isEmpty())
      {
        localChunk = this.persistentStorage.getFirst();
        if ((this.closedChunks.isEmpty()) || (this.currentMemSize + localChunk.currentChunkSize() <= this.maxMemSize));
      }
      else
      {
        return;
      }
      this.closedChunks.add(localChunk);
      this.persistentStorage.removeFirst();
      this.currentMemSize += localChunk.currentChunkSize();
    }
  }

  public IChunkStorage forTestOnly_getPersistentStorage()
  {
    return this.persistentStorage;
  }

  public Chunk getChunk(long paramLong)
  {
    if (paramLong < 0L)
      return null;
    if (paramLong < this.closedChunks.size())
      return (Chunk)this.closedChunks.get((int)paramLong);
    return this.persistentStorage.getChunk(paramLong - this.closedChunks.size());
  }

  public Chunk getFirst()
  {
    if (!this.closedChunks.isEmpty())
      return (Chunk)this.closedChunks.get(0);
    return this.persistentStorage.getFirst();
  }

  public long getNumChunks()
  {
    return this.closedChunks.size() + this.persistentStorage.getNumChunks();
  }

  public long getNumChunksPersisted()
  {
    return this.persistentStorage.getNumChunks();
  }

  public boolean isEmpty()
  {
    return (this.closedChunks.isEmpty()) && (this.persistentStorage.isEmpty());
  }

  public void purgeAll()
  {
    this.closedChunks.clear();
    this.persistentStorage.purgeAll();
  }

  public void removeFirst()
  {
    if (!this.closedChunks.isEmpty())
    {
      Chunk localChunk = (Chunk)this.closedChunks.remove(0);
      this.currentMemSize -= localChunk.currentChunkSize();
    }
    while (true)
    {
      fillFromPersistentToMemory();
      return;
      this.persistentStorage.removeFirst();
    }
  }

  public void setMaxmiumMemorySize(long paramLong)
  {
    if (paramLong < 0L)
      throw new IllegalArgumentException("Maximum memory size must be a non-negative integer, received: " + paramLong);
    this.maxMemSize = paramLong;
    if (this.currentMemSize > paramLong)
      while (this.closedChunks.size() > 1)
      {
        Chunk localChunk = (Chunk)this.closedChunks.remove(-1 + this.closedChunks.size());
        this.persistentStorage.store(localChunk);
        this.currentMemSize -= localChunk.currentChunkSize();
      }
    fillFromPersistentToMemory();
  }

  public void store(Chunk paramChunk)
  {
    if ((!this.persistentStorage.isEmpty()) || (this.currentMemSize + paramChunk.currentChunkSize() > this.maxMemSize))
    {
      this.persistentStorage.store(paramChunk);
      return;
    }
    this.closedChunks.add(paramChunk);
    this.currentMemSize += paramChunk.currentChunkSize();
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.location.internal.events.storage.ChunkStorage
 * JD-Core Version:    0.6.2
 */