code: private long getNextId() {
          // It should be all but impossible for the event cache to grow bigger than Long.MAX_VALUE,
          // but just for the sake of safe coding practices, check anyway.
          if (events.size() > Long.MAX_VALUE) {
              throw new IllegalStateException("Event store exceeded maximum size");
          }
  
          // Iterate through IDs, starting with the next ID counter, until an unused one is found.
          long id = nextId;
          while (events.get(id) != null) {
              id++;
          }
  
          // Set the next ID to the ID that was found plus one, then return the ID.
          nextId = id + 1;
          return id;
      }
docs: Gets the next ID to use as a handle for a stored event. This implementation just checks for
  the next unused ID based on an incrementing counter.
  <p/>
  NOTE: For long-running processes it's possible that the nextId field will overflow. Hence it
  is necessary to handle collisions gracefully by skipping them.
  
  @return The next ID that should be used to store an event.