synchronized public BigInteger generateId128 ( ) { long timestamp = System . currentTimeMillis ( ) ; long sequence = 0 ; boolean done = false ; while ( ! done ) { done = true ; while ( timestamp < lastTimestampMillisec . get ( ) ) { timestamp = waitTillNextMillisec ( timestamp ) ; } if ( timestamp == lastTimestampMillisec . get ( ) ) { // increase sequence sequence = sequenceMillisec . incrementAndGet ( ) ; if ( sequence > MAX_SEQUENCE_128 ) { // reset sequence sequenceMillisec . set ( sequence = 0 ) ; timestamp = waitTillNextMillisec ( timestamp ) ; done = false ; } } } sequenceMillisec . set ( sequence ) ; lastTimestampMillisec . set ( timestamp ) ; BigInteger biSequence = BigInteger . valueOf ( sequence & MASK_SEQUENCE_128 ) ; BigInteger biResult = BigInteger . valueOf ( timestamp ) ; biResult = biResult . shiftLeft ( ( int ) SHIFT_TIMESTAMP_128 ) ; biResult = biResult . or ( template128 ) . or ( biSequence ) ; return biResult ; }
Generates a 128 - bit id .