package ch.epfl.isochrone.tiledmap;

import java.util.HashSet;
import java.util.Set;

public class CachedTileProvider implements TileProvider{
    private TileProvider tp;
    private Set<Tile> tileDejaChargee;
    
    public CachedTileProvider(TileProvider tp)
    {
        this.tp=tp;
        this.tileDejaChargee=new HashSet<Tile>();
    }

    @Override
    public Tile tileAt(int zoom, int x, int y) {
        Tile t = tilePresente(zoom, x, y);
        
        if (t==null)
        {
            t = tp.tileAt(zoom, x, y);
            tileDejaChargee.add(t);
        }
        
        return t;
    }
    
    private Tile tilePresente(int zoom, int x, int y)
    {
        for (Tile t : tileDejaChargee)
        {
            if (t.x()==x && t.y()==y && t.zoom()==zoom)
            {
                return t;
            }
        }
        return null;
    }
}
