package electrosphere.game.client.terrain.cache;

import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author amaterasu
 */
public class LoadingChunkCache {
    ConcurrentHashMap<String,LoadingChunk> cacheMap = new ConcurrentHashMap();
    CopyOnWriteArrayList<String> cacheList = new CopyOnWriteArrayList();
    
    
    public LoadingChunkCache(){
    }
    
    
    //USED TO ADD macro values
    public void addLoadingChunkToCache(LoadingChunk chunk){
        cacheMap.put(getKey(chunk.worldX, chunk.worldY),chunk);
    }
    
    public LoadingChunk fetch(String key){
        return cacheMap.get(key);
    }
    
    
    public String getKey(int worldX, int worldY){
        return worldX + "-" + worldY;
    }
    
    public boolean containsKey(String key){
        return cacheMap.containsKey(key);
    }
    
    public Collection<LoadingChunk> getChunks(){
        return cacheMap.values();
    }
    
    public void remove(LoadingChunk chunk){
        cacheMap.remove(getKey(chunk.worldX,chunk.worldY));
        cacheList.remove(getKey(chunk.worldX,chunk.worldY));
    }
    
}
