|
/**
* Custom ID Generator
* ID generation rule: ID up to 64 bits
*
* | 41 bits: Timestamp (MS) | 3 bits: Region (Data Center) | 10 bits: Machine ID | 10 bits: Serial Number |
*/
Public class GameUUID {
// Reference time
Private long twepoch = 1288834974657L; // Thu, 04 Nov 2010 01:42:54 GMT
// Number of bits in the region
Private final static long regionIdBits = 3L;
// Number of machine Identification Digits
Private final static long workerIdBits = 10L;
// Number of serial numbers
Private final static long sequenceBits = 10L;
// Maximum region flag ID
Private final static long maxRegionId =-1L ^ (-1L <regionIdBits );
// Maximum Machine ID
Private final static long maxWorkerId =-1L ^ (-1L <workerIdBits );
// Maximum Serial Number ID
Private final static long sequenceMask =-1L ^ (-1L <sequenceBits );
// Shift the machine ID left by 10 digits
Private final static long workerIdShift = sequenceBits;
// Shifts the Business ID by 20 digits to the left
Private final static long regionIdShift = sequenceBits + workerIdBits;
// The time is shifted to 23 digits in milliseconds
Private final static long timestampLeftShift = sequenceBits + workerIdBits + regionIdBits;
Private static long lastTimestamp =-1L;
Private long sequence = 0L;
Private final long workerId;
Private final long regionId;
Public GameUUID (long workerId, long regionId ){
// Throw an exception if it exceeds the specified range.
If (workerId> maxWorkerId | workerId <0 ){
Throw new IllegalArgumentException ("worker Id can't be greater than % d or less than 0 ");
}
If (regionId> maxRegionId | regionId <0 ){
Throw new IllegalArgumentException ("datacenter Id can't be greater than % d or less than 0 ");
}
This. workerId = workerId;
This. regionId = regionId;
}
Public GameUUID (long workerId ){
// Throw an exception if it exceeds the specified range.
If (workerId> maxWorkerId | workerId <0 ){
Throw new IllegalArgumentException ("worker Id can't be greater than % d or less than 0 ");
}
This. workerId = workerId;
This. regionId = 0;
}
Public long generate (){
Return this. nextId (false, 0 );
}
/**
* Actually generated code
*
* @ Param isPadding
* @ Param busId
* @ Return
*/
Private synchronized long nextId (boolean isPadding, long busId ){
Long timestamp = timeGen ();
Long paddingnum = regionId;
If (isPadding ){
Paddingnum = busId;
}
If (timestamp <lastTimestamp ){
Try {
Throw new Exception ("Clock moved backwards. Refusing to generate id for" + (lastTimestamp-timestamp) + "milliseconds ");
} Catch (Exception e ){
E. printStackTrace ();
}
}
// If the last generation time is the same as the current time
If (lastTimestamp = timestamp ){
// Auto-increment of sequence, because sequence only has 10 bits, so the sequenceMask and sequenceMask should be separated from the high position.
Sequence = (sequence + 1) & sequenceMask;
// Determine whether the event exists, that is, the value exceeds 1024 every millisecond. When the value is 1024, the sequence is equal to 0 in the phase with sequenceMask.
If (sequence = 0 ){
// The spin waits until the next millisecond.
Timestamp = tailNextMillis (lastTimestamp );
}
} Else {
// If different from the last generation time, reset the sequence, that is, starting from the next millisecond, and the sequence count is accumulated again from 0,
// To ensure that the tail is more random, set a random number for the last digit.
Sequence = new SecureRandom (). nextInt (10 );
}
LastTimestamp = timestamp;
Return (timestamp-twepoch) <timestampLeftShift) | (paddingnum <regionIdShift) | (workerId <workerIdShift) | sequence;
}
// Prevent the generated time from being smaller than the previous time (due to issues such as NTP callback) and maintain the incremental trend.
Private long tailNextMillis (final long lastTimestamp ){
Long timestamp = this. timeGen ();
While (timestamp <= lastTimestamp ){
Timestamp = this. timeGen ();
}
Return timestamp;
}
// Obtain the current Timestamp
Protected long timeGen (){
Return System. currentTimeMillis ();
}
} |