org.apache.commons.betwixt.io.id
Class RandomIDGenerator

java.lang.Object
  extended by org.apache.commons.betwixt.io.id.AbstractIDGenerator
      extended by org.apache.commons.betwixt.io.id.RandomIDGenerator
All Implemented Interfaces:
IDGenerator

public final class RandomIDGenerator
extends AbstractIDGenerator

Generates ID's at random. The random number source is java.util.Random.

Random ID's are very useful if you're inserting elements created by Betwixt into a stream with existing elements. Using random ID's should reduce the danger of collision with existing element ID's.

This class can generate positive-only ids (the default) or it can generate a mix of negative and postive ones. This behaviour can be set by setPositiveIds(boolean) or by using the RandomIDGenerator(boolean onlyPositiveIds) constructor.

Version:
$Revision: 1.4 $
Author:
Robert Burrell Donkin

Field Summary
private  boolean onlyPositiveIds
          Should only positive id's be generated?
private  java.util.Random random
          Use simple java.util.Random as the source for our numbers
 
Constructor Summary
RandomIDGenerator()
          Constructor sets the PositiveIds property to true.
RandomIDGenerator(boolean onlyPositiveIds)
          Constructor sets PositiveIds property.
 
Method Summary
 boolean getPositiveIds()
          Gets whether only positive ID's should be generated
 java.lang.String nextIdImpl()
          Generates a random ID
 void setPositiveIds(boolean onlyPositiveIds)
          Sets whether only positive ID's should be generated
 
Methods inherited from class org.apache.commons.betwixt.io.id.AbstractIDGenerator
getLastId, nextId
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

random

private java.util.Random random
Use simple java.util.Random as the source for our numbers


onlyPositiveIds

private boolean onlyPositiveIds
Should only positive id's be generated?

Constructor Detail

RandomIDGenerator

public RandomIDGenerator()
Constructor sets the PositiveIds property to true.


RandomIDGenerator

public RandomIDGenerator(boolean onlyPositiveIds)
Constructor sets PositiveIds property.

Parameters:
onlyPositiveIds - set PositiveIds property to this value
Method Detail

nextIdImpl

public java.lang.String nextIdImpl()

Generates a random ID

If the PositiveIds property is true, then this method will recursively call itself if the random ID is less than zero.

Specified by:
nextIdImpl in class AbstractIDGenerator
Returns:
a random integer (converted to a string)

getPositiveIds

public boolean getPositiveIds()
Gets whether only positive ID's should be generated

Returns:
whether only positive IDs should be generated

setPositiveIds

public void setPositiveIds(boolean onlyPositiveIds)
Sets whether only positive ID's should be generated

Parameters:
onlyPositiveIds - pass true if only positive IDs should be generated