generate method

dynamic generate()

generate a new id

Implementation

generate() {
  // judge the current low sequence value is max or not
  if (lowSequence == lowSequenceMax) {
    // reject to generate a new id cause the machine number is out of range
    if (machine > machineMax) {
      throw Exception('the timestamp is out of range');
    }
    // judge the current high sequence value is max or not
    if (highSequence == highSequenceMax) {
      // judge the current timestamp value is max or not
      if (timestamp == timestampMax) {
        return 0;
      } else {
        // timestamp adds 1, and the high sequence value to be 0
        timestamp++;
        highSequence = 0;
      }
    } else {
      highSequence++;
    }
    lowSequence = 0;
  } else {
    lowSequence++;
  }
  var id = timestamp << timeStampShift |
      highSequence << highSequenceShift |
      machine << machineShift |
      lowSequence;

  return id;
}