org.limitless.util
Class RandomStringGenerator

java.lang.Object
  extended by org.limitless.util.RandomStringGenerator

public class RandomStringGenerator
extends java.lang.Object

A quick way to get a random string of letters and numbers (without strength needed for strong cryptographic implementations). Typical usages:

 String result = RandomStringGenerator.Instance().get();
 RandomStringGenerator gen = new RandomStringGenerator();
 String result2 = gen.get();
 String big = gen.get( 200 ); // 200 byte string exactly
 
NOTE: Default size is between 10 and 20 bytes inclusive. The class is _meant_ to be over-ridden... The singleton is always a default implementation.

Version:
$Header: /repository/nmc/java/org/limitless/util/RandomStringGenerator.java,v 1.1 2004/05/09 23:58:09 brianj Exp $
Author:
Brian Jones
See Also:
Resetting min size, resetting max size, reseting sequence of characters used

Field Summary
protected static int DefaultGrowthSize
          default growth - the resulting string will always be at least min bytes long, but up to min+growth bytes -- the length is randomized as well.
protected static int DefaultMinSize
          default minimum string size for control of base class
protected static java.lang.String letters
          Default set of symbols
(package private) static org.apache.log4j.Logger logger
          Log4J Logger
private  java.util.Random random
          This holds the random number generated used for the given object.
private static RandomStringGenerator singletonInstance
          The default instance to avoid object creation for the normal default case
 
Constructor Summary
RandomStringGenerator()
          Create a default generator
RandomStringGenerator(java.util.Random r)
          Create with an existing source of random numbers for repeatable behavior
 
Method Summary
protected  java.lang.String generate(int bytesInString)
          The default generate routine chooses random characters from a provided string.
 java.lang.String get()
          Fetch a random string with a random size between min and min + growth.
 java.lang.String get(int bytesInString)
          Fetch a random string of bytesInString size _exactly_.
protected  int getGrowthSize()
          This defines how much larger than min is allowed and can be over-ridden in the derived class
protected  int getMinSize()
          This returns the smallest the string can be, and is allowed to be over-ridden in a derived class
protected  java.lang.String getSymbolSet()
          This returns a string containing the only characters that will ever appear in the random set.
static RandomStringGenerator Instance()
          Singleton is used to access the "default" which avoids creation of objects.
static void main(java.lang.String[] args)
          Test entry, should spew random strings of the right lengths...pointless for "repeatable" testing, but a human being eyeballing the result should see "reasonable" different strings
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

static org.apache.log4j.Logger logger
Log4J Logger


letters

protected static final java.lang.String letters
Default set of symbols

See Also:
Constant Field Values

DefaultMinSize

protected static final int DefaultMinSize
default minimum string size for control of base class

See Also:
Constant Field Values

DefaultGrowthSize

protected static final int DefaultGrowthSize
default growth - the resulting string will always be at least min bytes long, but up to min+growth bytes -- the length is randomized as well. A derived class can avoid this by over-riding getGrowthSize() to return 0;

See Also:
Constant Field Values

singletonInstance

private static final RandomStringGenerator singletonInstance
The default instance to avoid object creation for the normal default case


random

private final java.util.Random random
This holds the random number generated used for the given object.

Constructor Detail

RandomStringGenerator

public RandomStringGenerator()
Create a default generator


RandomStringGenerator

public RandomStringGenerator(java.util.Random r)
Create with an existing source of random numbers for repeatable behavior

Method Detail

Instance

public static RandomStringGenerator Instance()
Singleton is used to access the "default" which avoids creation of objects.

Returns:
The object to fetch random strings from - do NOT store a reference to this as a later implementation may allow the object to be reset!!!

get

public java.lang.String get()
Fetch a random string with a random size between min and min + growth. This shouldn't be over-ridden as it's an invariant in the Template Method pattern (though it doesn't forbid being over-ridden if some special case demands it).


get

public java.lang.String get(int bytesInString)
Fetch a random string of bytesInString size _exactly_. This is an invariant method for the Template Method and shouldn't be over-ridden.


getMinSize

protected int getMinSize()
This returns the smallest the string can be, and is allowed to be over-ridden in a derived class


getGrowthSize

protected int getGrowthSize()
This defines how much larger than min is allowed and can be over-ridden in the derived class


getSymbolSet

protected java.lang.String getSymbolSet()
This returns a string containing the only characters that will ever appear in the random set.


generate

protected java.lang.String generate(int bytesInString)
The default generate routine chooses random characters from a provided string. This CAN be over-ridden, for instance if a desired sequence of "random" results was needed for test. Normally, simply providing a new starting, growth, or symbol-set will be easier if the end result is random sequences.

Returns:
The random string.

main

public static void main(java.lang.String[] args)
Test entry, should spew random strings of the right lengths...pointless for "repeatable" testing, but a human being eyeballing the result should see "reasonable" different strings



Copyright © 1995, 2004 Limitless Knowledge Association, All Rights Reserved.