public function addEntity(HasId $entity, $key = null)
    {

        if (is_null($key) === false) {
            $this->replaceEntity($entity, $key);
            return;
        }

        $entityId = $entity->getId();

        $this->pool[] = $entity;

        $this->indexed[$entityId] = $entity;
        $this->map[$entityId] = $this->retrieveLastPoolKey();
    }