org.pfsw.text
Class ObjectIdGenerator

java.lang.Object
  extended by org.pfsw.text.ObjectIdGenerator
All Implemented Interfaces:
org.pfsw.bif.identifier.IObjectIdGenerator, StringGenerator

public class ObjectIdGenerator
extends java.lang.Object
implements org.pfsw.bif.identifier.IObjectIdGenerator, StringGenerator

Generates numeric IDs that are left padded with zeros.


Field Summary
protected static int DEFAULT_LENGTH
           
protected static char DEFAULT_PAD_CHAR
           
protected static long DEFAULT_START_ID
           
 
Constructor Summary
ObjectIdGenerator()
          Initialize the new instance with default values.
ObjectIdGenerator(int idLength)
          Initialize the new instance with the length for the generated identifiers.
ObjectIdGenerator(long startId, int idLength)
          Initialize the new instance with the length for the generated identifiers and the id to start with.
 
Method Summary
 java.lang.String generateString()
          Returns a new string.
 java.lang.String generateString(int len)
          Generates a new string (i.e. identifier) with the specified length.
protected  int getDefaultLength()
           
protected  char getDefaultPadChar()
           
protected  long getDefaultStartId()
           
 int getFullLength()
          Returns the full length of the generated identifiers.
 int getLength()
          Returns the length to which the IDs (the numeric part without prefix)) are filled up (left padded).
protected  long getNextId()
          Returns the next identifier value without incrementing.
protected  java.util.concurrent.atomic.AtomicLong getNextIdHolder()
           
 char getPadChar()
          Return the character that is used to fill up the IDs to the full length.
 java.lang.String getPrefix()
          Returns the prefix that will be prepended to every generated ID.
protected  int getPrefixLength()
           
 boolean hasPadding()
          Returns whether or not padding for the identifiers is activated.
 boolean hasPrefix()
          Returns whether or not a prefix is defined.
protected  java.lang.String leftPad(long id)
           
protected  java.lang.String leftPad(long id, int stringLength)
           
 java.lang.String newIdentifier()
          Returns a new unique identifier as string.
 long nextIdentifier()
          Returns a new identifier which is different to the last one and automatically increments the internal value for the next id.
 void setLength(int len)
          Sets the length to which the IDs (numeric part without prefix) must be filled-up with the defined padding character.
protected  void setNextId(long id)
          Sets the next identifier to the given value.
 void setPadChar(char fillChar)
          Set the character that is used to fill up the IDs to the full length.
 void setPrefix(java.lang.String newValue)
          Set the prefix that will be prepended to every generated ID.
protected  StringUtil str()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PAD_CHAR

protected static final char DEFAULT_PAD_CHAR
See Also:
Constant Field Values

DEFAULT_START_ID

protected static final long DEFAULT_START_ID
See Also:
Constant Field Values

DEFAULT_LENGTH

protected static final int DEFAULT_LENGTH
See Also:
Constant Field Values
Constructor Detail

ObjectIdGenerator

public ObjectIdGenerator()
Initialize the new instance with default values. That is an ID length of 10 and a start ID of 1 with fill character '0'.


ObjectIdGenerator

public ObjectIdGenerator(int idLength)
Initialize the new instance with the length for the generated identifiers.

Parameters:
idLength - The length to which Ids are filled up with leading zeros (must be > 0)

ObjectIdGenerator

public ObjectIdGenerator(long startId,
                         int idLength)
Initialize the new instance with the length for the generated identifiers and the id to start with.

Parameters:
startId - The first id to be generated
idLength - The length to which Ids are filled up with leading zeros
Method Detail

newIdentifier

public java.lang.String newIdentifier()
Returns a new unique identifier as string.

Specified by:
newIdentifier in interface org.pfsw.bif.identifier.IObjectIdGenerator

nextIdentifier

public long nextIdentifier()
Returns a new identifier which is different to the last one and automatically increments the internal value for the next id.


hasPadding

public boolean hasPadding()
Returns whether or not padding for the identifiers is activated.


getFullLength

public int getFullLength()
Returns the full length of the generated identifiers. That includes the length of the prefix if one is defined.

See Also:
getPrefix(), getLength()

getLength

public int getLength()
Returns the length to which the IDs (the numeric part without prefix)) are filled up (left padded).


setLength

public void setLength(int len)
Sets the length to which the IDs (numeric part without prefix) must be filled-up with the defined padding character. Since all IDs are based on data type long a value greater than 20 is not really reasonable, however it is allowed here.

Parameters:
len - The new length. If len <= 0 no padding will be done!
See Also:
getPadChar(), setPadChar(char)

setPadChar

public void setPadChar(char fillChar)
Set the character that is used to fill up the IDs to the full length.

Parameters:
fillChar - The fill character

getPadChar

public char getPadChar()
Return the character that is used to fill up the IDs to the full length.


getPrefix

public java.lang.String getPrefix()
Returns the prefix that will be prepended to every generated ID.


setPrefix

public void setPrefix(java.lang.String newValue)
Set the prefix that will be prepended to every generated ID.


hasPrefix

public boolean hasPrefix()
Returns whether or not a prefix is defined.


generateString

public java.lang.String generateString()
Returns a new string. It is a new identifier as returned by newIdentifier().

Specified by:
generateString in interface StringGenerator
Returns:
A new string containing a left padded number with the currently defined length, plus the optional prefix.

generateString

public java.lang.String generateString(int len)
Generates a new string (i.e. identifier) with the specified length. The contents of the string is a number left padded with the currently defined padding character and prefixed with the defined prefix (if any is set).

The specified length will be used only for this single string generation. That is, the defined length (see getLength() and setLength(int)) of this object will not be used or changed by this method.

Specified by:
generateString in interface StringGenerator
Parameters:
len - The length of the string to generate.
Returns:
A new string with the given length.
Throws:
java.lang.IllegalArgumentException - if the given length is negative.

getPrefixLength

protected int getPrefixLength()

leftPad

protected java.lang.String leftPad(long id)

leftPad

protected java.lang.String leftPad(long id,
                                   int stringLength)

getNextId

protected long getNextId()
Returns the next identifier value without incrementing.


setNextId

protected void setNextId(long id)
Sets the next identifier to the given value.


getDefaultStartId

protected long getDefaultStartId()

getDefaultLength

protected int getDefaultLength()

getDefaultPadChar

protected char getDefaultPadChar()

getNextIdHolder

protected java.util.concurrent.atomic.AtomicLong getNextIdHolder()

str

protected StringUtil str()