org.almendra.commons.util.globalId
Class UniqueIdGenerator

java.lang.Object
  extended by org.almendra.commons.util.globalId.UniqueIdGenerator

public class UniqueIdGenerator
extends java.lang.Object

Generates globally unique ids. These ids contain the name of the local machine which is assumed to be unique within a network, a running counter and a timestamp. The timestamp is used to be sure that the id is unique even after the running counter has had overflow and therefore has been reset to 0. The counter has meanwhile been changed from an int to a BigInteger. Overflow is now very unlikely to occur. The timestamp is kept as there can be several JVMs on the same machine using the same code to generate unique ids.

Author:
Oliver Plohmann

Field Summary
protected static java.math.BigInteger Count
          The running counter which is part of an id.
protected static java.lang.String HostName
          The name of the local machine assumed unique within a network.
protected static java.util.Random random
           
 
Constructor Summary
UniqueIdGenerator()
          Creates a new UniqueIdGenerator object.
 
Method Summary
protected static java.lang.String getHostName()
          Retrieve the name of the local machine which is part of an id.
static java.lang.String getMillisWithRandom()
          Return a new counter that has not been used before.
static java.lang.String getNewCount()
          Return a new counter that has not been used before.
static UniqueId next()
          Return a new unique id.
static UniqueId next(java.lang.String prefix)
          Return a new unique id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

Count

protected static java.math.BigInteger Count
The running counter which is part of an id.


HostName

protected static java.lang.String HostName
The name of the local machine assumed unique within a network.


random

protected static java.util.Random random
Constructor Detail

UniqueIdGenerator

public UniqueIdGenerator()
Creates a new UniqueIdGenerator object.

Method Detail

getHostName

protected static java.lang.String getHostName()
Retrieve the name of the local machine which is part of an id.

Returns:

next

public static UniqueId next()
Return a new unique id.

Returns:

next

public static UniqueId next(java.lang.String prefix)
Return a new unique id. The user can specify a different prefix than the local machine name. The user is then responsible for the id to be unique.

Parameters:
prefix - the id prefix used instead of the local machine name
Returns:
the unique id

getNewCount

public static java.lang.String getNewCount()
Return a new counter that has not been used before.

Returns:

getMillisWithRandom

public static java.lang.String getMillisWithRandom()
Return a new counter that has not been used before.

Returns: