Created
May 2, 2012 16:11
-
-
Save repomaa/2577853 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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