Skip to content

Instantly share code, notes, and snippets.

@repomaa
Created May 2, 2012 16:11
Show Gist options
  • Save repomaa/2577853 to your computer and use it in GitHub Desktop.
Save repomaa/2577853 to your computer and use it in GitHub Desktop.
package logic;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
public class TileFactory<T extends AbstractTile> {
private TilesSet<T> tiles;
private Class<T> clazz;
public TileFactory(Class<T> clazz) {
tiles = new TilesSet<T>();
this.clazz = clazz;
}
public T getTile(int x, int y, int zoom) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
T candidate = tiles.get(x, y, zoom);
if(candidate == null) {
candidate = clazz.getConstructor(new Class[]{int.class, int.class, int.class}).newInstance(x,y,zoom);
tiles.add(candidate);
}
return candidate;
}
private class TilesSet<T extends AbstractTile> extends HashSet<T> {
public T get(int x, int y, int zoom) {
for(T current : this)
if(current.x == x && current.y == y && current.zoom == zoom)
return current;
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment