getId method

int getId()

genreate the snowflake id

Implementation

int getId() {
  int currentTimestamp = _getCurrentTimestamp();
  if (_tolerateTimestampBackwardsIfNeed(currentTimestamp)) {
    currentTimestamp = _getCurrentTimestamp();
  }
  // if current timestamp equals to previous one, we try to increase sequence
  if (lastTimestamp == currentTimestamp) {
    if (_sequenceIncreaseIfReachimitReset()) {
      currentTimestamp = _waitUntilNextTime(currentTimestamp);
    }
  } else {
    sequence = 0;
  }
  lastTimestamp = currentTimestamp;
  return ((currentTimestamp - epoch) << timestampEftShiftBits) |
      (workerId << workerIdEftShiftBits) |
      sequence;
}