org.safehaus.uuid
public final class UUIDGenerator extends Object
generateTimeBasedUUID()
function.
Modifier and Type | Method and Description |
---|---|
UUID |
generateNameBasedUUID(UUID nameSpaceUUID,
String name)
Method similar to the previous one; the difference being that a
shared MD5 digest instance will be used.
|
UUID |
generateNameBasedUUID(UUID nameSpaceUUID,
String name,
MessageDigest digest)
Method for generating name-based UUIDs, using the standard
name-based generation method described in the UUID specs,
and the caller supplied hashing method.
|
UUID |
generateRandomBasedUUID()
Method for generating (pseudo-)random based UUIDs, using the
default (shared) SecureRandom object.
|
UUID |
generateRandomBasedUUID(Random randomGenerator)
Method for generating (pseudo-)random based UUIDs, using the
specified SecureRandom object.
|
UUID |
generateTagURIBasedUUID(TagURI name)
Method for generating UUIDs using tag URIs.
|
UUID |
generateTagURIBasedUUID(TagURI name,
MessageDigest hasher)
Method for generating UUIDs using tag URIs.
|
UUID |
generateTimeBasedUUID()
Method for generating time based UUIDs.
|
UUID |
generateTimeBasedUUID(EthernetAddress addr)
Method for generating time based UUIDs.
|
EthernetAddress |
getDummyAddress()
Method that returns a randomly generated dummy ethernet address.
|
MessageDigest |
getHashAlgorithm() |
static UUIDGenerator |
getInstance()
Method used for accessing the singleton generator instance.
|
Random |
getRandomNumberGenerator()
Method for getting the shared random number generator used for
generating the UUIDs.
|
static void |
main(String[] args)
A simple test harness is added to make (automated) testing of the
class easier.
|
void |
setRandomNumberGenerator(Random r)
Method that can be called to specify alternative random
number generator to use.
|
void |
synchronizeExternally(TimestampSynchronizer sync)
Method that can (and should) be called once right after getting
the instance, to ensure that system time stamp values used are
valid (with respect to values used earlier by JUG instances), and
to use file-lock based synchronization mechanism to prevent multiple
JVMs from running conflicting instances of JUG (first one to be
started wins on contention).
|
public static UUIDGenerator getInstance()
public void synchronizeExternally(TimestampSynchronizer sync) throws IOException
Caller needs to instantiate an instance of
TimestampSynchronizer
; currently the only standard
implementation is
FileBasedTimestampSynchronizer
(which
is JDK 1.4+).
Note: since the generator instance is a singleton, calling this method will always cause all generation to be synchronized using the specified method.
sync
- Synchronizer instance to use for synchronization.IOException
public EthernetAddress getDummyAddress()
public Random getRandomNumberGenerator()
public void setRandomNumberGenerator(Random r)
SecureRandom
that is used by default.
Note that to avoid first-time initialization penalty
of using SecureRandom
, this method has to be called
before generating the first random-number based UUID.
public MessageDigest getHashAlgorithm()
public UUID generateRandomBasedUUID()
public UUID generateRandomBasedUUID(Random randomGenerator)
randomGenerator
- Random number generator to use for getting the
random number from which UUID will be composed.public UUID generateTimeBasedUUID()
Note that since the dummy address is only to be created once and shared from there on, there is some synchronization overhead.
public UUID generateTimeBasedUUID(EthernetAddress addr)
addr
- Hardware address (802.1) to use for generating
spatially unique part of UUID. If system has more than one NIC,
any address is usable. If no NIC is available (or its address
not accessible; often the case with java apps), a randomly
generated broadcast address is acceptable. If so, use the
alternative method that takes no arguments.public UUID generateNameBasedUUID(UUID nameSpaceUUID, String name, MessageDigest digest)
nameSpaceUUID
- UUID of the namespace, as defined by the
spec. UUID has 4 pre-defined "standard" name space strings
that can be passed to UUID constructor (see example below).
Note that this argument is optional; if no namespace is needed
(for example when name includes namespace prefix), null may be
passed.name
- Name to base the UUID on; for example,
IP-name ("www.w3c.org") of the system for UUID.NAMESPACE_DNS,
URL ("http://www.w3c.org/index.html") for UUID.NAMESPACE_URL
and so on.digest
- Instance of MessageDigest to use for hashing the name
value. hash.reset() will be called before calculating the has
value, to make sure digest state is not random and UUID will
not be randomised.
UUID uuid = gen.generateNameBasedUUID(
new UUID(UUID.NAMESPACE_DNS, "www.w3c.org"));
public UUID generateNameBasedUUID(UUID nameSpaceUUID, String name)
public UUID generateTagURIBasedUUID(TagURI name)
name
- tag URI to base UUID on.public UUID generateTagURIBasedUUID(TagURI name, MessageDigest hasher)
name
- tag URI to base UUID on.hasher
- Hashing algorithm to use. Note that the caller has to
make sure that it's thread-safe to use 'hasher', either by never
calling this method from multiple threads, or by explicitly sync'ing
the calls.public static void main(String[] args)