@ThreadSafe public final class TimeBasedKeys extends Object
Each long key value contains the time (in milliseconds) at which the key is generated, left-shifted by 16 bits. Those 16 bits are then used to store a counter value that is unique for each millisecond. When a new key is needed, the current time is compared to the last time for which a key was generated. If the times are different, then the counter is reset to 0; otherwise, the counter is incremented.
Use a single generator instance for each sequence of keys.
To obtain a unique value, simply call:
TimeBasedKeys keys = TimeBasedKeys.create(); ... long key = keys.nextKey();Because the keys are time-based, the generator can also identify the range of keys that were created before or after a given instant in time, or within a range of times. For example, all keys obtained after January 10, 2014 at 12:12:41.845-06:00 (which has a
System.currentTimeMillis()
value of 1389378406
) will be greater than or equal to the following
value:
long timeInMillis = 1389378406L; long minKey = keys.getKeyStartingAt(timeInMillis);Using this and similar methods, one can obtain all counter values that might have been generated, for example, sometime during 2012:
long janFirst2012 = 1325397600L; // Jan 1 2012 at 00:00.000 UTC long janFirst2013 = 1357020000L; // Jan 1 2013 at 00:00.000 UTC long smallestKey = keys.getKeyStartingAt(janFirst2012); long justLargerKey = keys.getKeyStartingAt(janFirst2013);Then all keys generated during 2012 will therefore satisfy:
smallestKey >= key && key < justLargerKey
Modifier and Type | Field and Description |
---|---|
static short |
DEFAULT_BITS_IN_COUNTER
By default each
TimeBasedKeys instance will use 16 bits for the counter. |
Modifier | Constructor and Description |
---|---|
protected |
TimeBasedKeys(short bitsUsedInCounter)
Create a new key generator.
|
Modifier and Type | Method and Description |
---|---|
static TimeBasedKeys |
create()
Create a new generator that uses 16 bits for the counter portion of the keys.
|
static TimeBasedKeys |
create(int bitsUsedInCounter)
Create a new generator that uses the specified number of bits for the counter portion of the keys.
|
long |
getCounterEndingAfter(long millisInUtc)
Obtain the first (earliest) key that would have been generated after the specified UTC time.
|
long |
getCounterEndingAt(long millisInUtc)
Obtain the largest (latest) key that would have been generated at the specified UTC time.
|
long |
getCounterStartingAt(long millisInUtc)
Obtain the first (earliest) key that would have been generated at the specified UTC time.
|
long |
getTimeGenerated(long key)
Obtain the milliseconds since epoch in UTC that the supplied key was generated.
|
long |
nextKey()
Get the next key for the current time in UTC.
|
public static final short DEFAULT_BITS_IN_COUNTER
TimeBasedKeys
instance will use 16 bits for the counter. That results in a maximum of of 65535
distinct values per millisecond (which is likely sufficient), while also leaving enough bits in the long to store a value
well past the year 10,000.protected TimeBasedKeys(short bitsUsedInCounter)
bitsUsedInCounter
- the number of bits to be used in the counter.public static TimeBasedKeys create()
public static TimeBasedKeys create(int bitsUsedInCounter)
bitsUsedInCounter
- the number of bits in the counter portion of the keys; must be a positive number for which theere
is enough space to left shift without overflowing.public long nextKey()
public long getCounterStartingAt(long millisInUtc)
millisInUtc
- the number of milliseconds (in UTC) past epoch, and the time at which nextKey()
might have been
calledpublic long getCounterEndingAt(long millisInUtc)
millisInUtc
- the number of milliseconds (in UTC) past epoch, and the time at which nextKey()
might have been
calledpublic long getCounterEndingAfter(long millisInUtc)
millisInUtc
- the number of milliseconds (in UTC) past epoch, and the time at which nextKey()
might have been
calledpublic long getTimeGenerated(long key)
System.currentTimeMillis()
when the key was generated.key
- the keyCopyright © 2008–2016 JBoss, a division of Red Hat. All rights reserved.