org.pfsw.text
Class RandomStringGenerator

java.lang.Object
  extended by org.pfsw.text.RandomStringGenerator
All Implemented Interfaces:
StringGenerator

public class RandomStringGenerator
extends java.lang.Object
implements StringGenerator

A string generator that produces a new random string for each invocation. It can be configured regarding the string length and the allowed characters in the generated string.


Field Summary
static int DEFAULT_LENGTH
          The default length (10) being used if not specified differently.
 
Constructor Summary
RandomStringGenerator()
          Creates a generator with all default values set (length=10, chars=[A-Z,a-z,0-9]).
RandomStringGenerator(int defaultLength)
          Creates a generator with all default values set (length=10, chars=[A-Z,a-z,0-9]).
RandomStringGenerator(java.util.Random random)
          Creates a generator with all default values set (length=10, chars=[A-Z,a-z,0-9]) and a custom randomizer.
 
Method Summary
 java.lang.String generateString()
          Generates a new string.
 java.lang.String generateString(int length)
          Generates a new string with the specified length.
 java.lang.String getAllowedCharacters()
          Returns the set of characters that are used as base for the string generation.
protected  char[] getAllowedCharsArray()
           
 int getDefaultLength()
           
protected  java.util.Random getRandom()
           
protected  java.lang.Object nextChar()
           
protected  int nextIndex()
           
 void setAllowedCharacters(java.lang.String allowedChars)
          Sets the set of characters that are used as base for the string generation.
protected  void setAllowedCharsArray(char[] charArray)
           
 void setDefaultLength(int length)
          Sets the length to be used as default for generating new strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LENGTH

public static final int DEFAULT_LENGTH
The default length (10) being used if not specified differently.

See Also:
Constant Field Values
Constructor Detail

RandomStringGenerator

public RandomStringGenerator()
Creates a generator with all default values set (length=10, chars=[A-Z,a-z,0-9]).


RandomStringGenerator

public RandomStringGenerator(int defaultLength)
Creates a generator with all default values set (length=10, chars=[A-Z,a-z,0-9]).


RandomStringGenerator

public RandomStringGenerator(java.util.Random random)
Creates a generator with all default values set (length=10, chars=[A-Z,a-z,0-9]) and a custom randomizer.

Method Detail

generateString

public java.lang.String generateString(int length)
Description copied from interface: StringGenerator
Generates a new string with the specified length. The contents of the string is implementation specific. The length depends on the default length defined by the implementor.

Specified by:
generateString in interface StringGenerator
Parameters:
length - The length of the string to generate.
Returns:
A new string with the given length.

generateString

public java.lang.String generateString()
Description copied from interface: StringGenerator
Generates a new string. The contents of the string is implementation specific. The length depends on the default length defined by the implementor.

Specified by:
generateString in interface StringGenerator
Returns:
A new string with the default length.

getDefaultLength

public int getDefaultLength()

setDefaultLength

public void setDefaultLength(int length)
Sets the length to be used as default for generating new strings.

Parameters:
length - The new length value which must be positive.
Throws:
java.lang.IllegalArgumentException - If the given length is not positive.

getAllowedCharacters

public java.lang.String getAllowedCharacters()
Returns the set of characters that are used as base for the string generation.


setAllowedCharacters

public void setAllowedCharacters(java.lang.String allowedChars)
Sets the set of characters that are used as base for the string generation.

Parameters:
allowedChars - A string containing all characters that are possible in generated strings (must not be null).
Throws:
java.lang.IllegalArgumentException - If the given string is null.

nextChar

protected java.lang.Object nextChar()

nextIndex

protected int nextIndex()

getRandom

protected java.util.Random getRandom()

getAllowedCharsArray

protected char[] getAllowedCharsArray()

setAllowedCharsArray

protected void setAllowedCharsArray(char[] charArray)