package ch.epfl.isochrone.tiledmap;

import java.util.LinkedHashMap;
import java.util.Map;

public class TileCache {
    public static final int MAX_SIZE=100; 
    private LinkedHashMap<Long, Tile> cache =
            new LinkedHashMap<Long, Tile>() {
                /**
                 * 
                 */
                private static final long serialVersionUID = 1L;

                @Override
                protected boolean removeEldestEntry(Map.Entry<Long,Tile> e){
                    return size() > MAX_SIZE;
                }
        };
    
    public void put(int zoom, int x, int y, Tile tile)
    {
        cache.put(toLong(zoom, x, y), tile);
    }
    
    public Tile get(int zoom, int x, int y)
    {
        return cache.get(toLong(zoom, x, y));
    }
    
    private long toLong(int zoom, int x, int y)
    {
        return y + x * 1048576 + zoom * 1048576*1048576; 
    }
}
