1

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?

Meeresgott
  • 431
  • 3
  • 17
  • You'll **never** reach `Long.MAX_VALUE` so why care ? It's `2^64`. Even if you remove items and you have *free uuid* you may not care, you have enough space until MAX_VALUE – azro Jun 02 '20 at 10:50
  • Because I don't know if I have to lower this boundary. In this example it is hardcoded but in the actual code it is a property. I don't wont a kind of "overflow"-Bug in the feature If I could fix it right now – Meeresgott Jun 02 '20 at 10:55
  • 1
    You store in a `long` a value that is lwoer than `Long.MAX_VALUE` so it's ok. It would be a problem if the ID was an `int` yes because int is on 32b , here everything is OK – azro Jun 02 '20 at 10:56
  • you're right. The UUID could be in the Boundary of 0 to 2^64 but the List.size() should not be over the property. - But for my peace of mind I will log an error if the Boundary is reached. – Meeresgott Jun 02 '20 at 11:00
  • You're right the List is limited by Inter.MAX_VALUE https://stackoverflow.com/questions/3767979/how-much-data-can-a-list-can-hold-at-the-maximum but that' is 4 billion elements – azro Jun 02 '20 at 11:32

0 Answers0