-
-
Notifications
You must be signed in to change notification settings - Fork 110
Variant V7 #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Variant V7 #48
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f2092b
First cut of V7
Hellblazer 35d056b
think this is correct
Hellblazer 6e99d95
get all random bytes required at once
Hellblazer f4197d9
everything but the check for time
Hellblazer b01f24b
add variant 7 time check.
Hellblazer 1b84520
Think this is correct
Hellblazer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/fasterxml/uuid/impl/TimeBasedEpochGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.fasterxml.uuid.impl; | ||
|
||
|
||
import java.nio.ByteBuffer; | ||
import java.security.SecureRandom; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import com.fasterxml.uuid.NoArgGenerator; | ||
import com.fasterxml.uuid.UUIDType; | ||
import com.fasterxml.uuid.impl.RandomBasedGenerator.LazyRandom; | ||
|
||
/** | ||
* Implementation of UUID generator that uses time/location based generation | ||
* method field from the Unix Epoch timestamp source - the number of | ||
* milliseconds seconds since midnight 1 Jan 1970 UTC, leap seconds excluded | ||
* <p> | ||
* As all JUG provided implementations, this generator is fully thread-safe. | ||
* Additionally it can also be made externally synchronized with other instances | ||
* (even ones running on other JVMs); to do this, use | ||
* {@link com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer} (or | ||
* equivalent). | ||
* | ||
* @since 3.1 | ||
*/ | ||
public class TimeBasedEpochGenerator extends NoArgGenerator | ||
{ | ||
|
||
/* | ||
/********************************************************************** | ||
/* Configuration | ||
/********************************************************************** | ||
*/ | ||
|
||
|
||
/** | ||
* Random number generator that this generator uses. | ||
*/ | ||
protected final Random _random; | ||
|
||
/* | ||
/********************************************************************** | ||
/* Construction | ||
/********************************************************************** | ||
*/ | ||
|
||
/** | ||
* @param rnd Random number generator to use for generating UUIDs; if null, | ||
* shared default generator is used. Note that it is strongly recommend to | ||
* use a <b>good</b> (pseudo) random number generator; for example, JDK's | ||
* {@link SecureRandom}. | ||
*/ | ||
|
||
public TimeBasedEpochGenerator(Random rnd) | ||
{ | ||
if (rnd == null) { | ||
rnd = LazyRandom.sharedSecureRandom(); | ||
} | ||
_random = rnd; | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Access to config | ||
/********************************************************************** | ||
*/ | ||
|
||
@Override | ||
public UUIDType getType() { return UUIDType.TIME_BASED_EPOCH; } | ||
|
||
/* | ||
/********************************************************************** | ||
/* UUID generation | ||
/********************************************************************** | ||
*/ | ||
|
||
@Override | ||
public UUID generate() | ||
{ | ||
ByteBuffer buff = ByteBuffer.allocate(2 * 8); | ||
final long rawTimestamp = System.currentTimeMillis(); | ||
final byte[] buffer = new byte[10]; | ||
_random.nextBytes(buffer); | ||
buff.position(6); | ||
buff.put(buffer); | ||
buff.position(0); | ||
buff.putLong(rawTimestamp << 16); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
buff.flip(); | ||
return UUIDUtil.constructUUID(UUIDType.TIME_BASED_EPOCH, buff.array()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
taking the cowards way out with a byte buffer