So today I learnt some generics Maybe someone will find this useful!
@SuppressWarnings("unchecked")
public <T extends Entity> T getEntityWithTag(Class<T> type, String tag) {
for (int i = 0; i < entities.size(); i++) {
Entity entity = entities.get(i);
if (entity.tag.equals(tag) && entity.getClass().isAssignableFrom(type)) return (T) entity;
}
return null;
}
Quick explanation:
Entity.tag is a String;
isAssignableFrom(Class) basically means instanceof
What this method does, is basically take a class that extends entity, and some kind of tag. Check all the existing entities to see if there is an entity which is instanceof class specified and with tag specified and return the class specified if it finds that entity. No idea if I explained properly, but here you go