I have a List with entities. Each entity has the attribute uuid and some others. Now I want to create a Entity Manager. The Manager can create new entities and has to look for a new unused id for the entity based on a List of all entities. This is my current approach:
@AllArgsConstructor
public class Entity {
@Getter @Setter
private long id;
}
public class EntityManager {
private static final MAX_AMOUNT_UUID = Setting.getInstance().getAmountUUID();
private List<Entity> entities;
private long lowestUnassignedEid;
public EntityManager() {
entities = new ArrayList<>();
lowestUnassignedEid = 0;
}
public long generateNewEid(){
if (lowestUnassignedEid < MAX_AMOUNT_UUID ) {
return lowestUnassignedEid++;
} else {
// How to check after this.
// a lot of the entities will be removed so there are a lot of free uuid
// but how can find out ?
}
}
public Entity createEntity(){
Entity e = new Entity(generateNewEid());
entities.add(e);
return e;
}
public void removeEntity(Entity entity){
this.entities.remove(entity);
}
}
The more I think about it, it tend to use a Map instead. So I can check if a Number is in the KeySet. Or is there a proper way with a List?