org.databene.benerator.primitive.number
Class RecurrenceRelationNumberGenerator<E extends java.lang.Number>

java.lang.Object
  extended by org.databene.benerator.util.AbstractGenerator<E>
      extended by org.databene.benerator.util.AbstractNonNullGenerator<E>
          extended by org.databene.benerator.primitive.number.AbstractNonNullNumberGenerator<E>
              extended by org.databene.benerator.primitive.number.RecurrenceRelationNumberGenerator<E>
All Implemented Interfaces:
java.io.Closeable, Generator<E>, NonNullGenerator<E>, org.databene.commons.Resettable, org.databene.commons.ThreadAware
Direct Known Subclasses:
FibonacciLongGenerator, PadovanLongGenerator

public abstract class RecurrenceRelationNumberGenerator<E extends java.lang.Number>
extends AbstractNonNullNumberGenerator<E>

Parent class for Number Generators that calculate numbers recursively. Child classes can define recursive sequences easily by defining a depth and implementing the methods a0(int) and aN().

The recursion depth needs to be specified in the constructor call, a0(int) needs to return the predefined initial value(s) of the sequence (f0, f1, ...) and aN() implements the recursion (fN = f(f(n-1), f(n-2), ...).

Example: The Fibonacci sequence is defined recursively by

For a Generator of Long values, this translates to an implementation with

depth = 2
  
 protected Long aN() {
     return aN(-1) + aN(-2);
 }

 protected Long a0(int n) {
     return (n == 0 ? 0L : 1L);
 }
 
Have a look at the FibonacciLongGenerator source code for the complete implementation.

Created: 13.10.2009 19:07:27

Since:
0.6.0
Author:
Volker Bergmann

Field Summary
 
Fields inherited from class org.databene.benerator.primitive.number.AbstractNonNullNumberGenerator
generatedType, granularity, max, min
 
Fields inherited from class org.databene.benerator.util.AbstractGenerator
context, logger, state
 
Constructor Summary
RecurrenceRelationNumberGenerator(java.lang.Class<E> targetType, int depth, E min, E max)
           
 
Method Summary
protected abstract  E a0(int n)
          Must be implemented by child classes to return the seed values of the recurrence relation.
protected abstract  E aN()
          Must be implemented by child classes to implement the recurrence relation.
protected  E aN(int offset)
          Provides the most recent calculated values.
protected  E calculateNext()
           
 void close()
          See Generator.close()
 E generate()
           
 int getDepth()
           
 int getN()
           
 void init(GeneratorContext context)
           
 void reset()
          See Resettable.reset()
protected  void resetMembers()
           
 
Methods inherited from class org.databene.benerator.primitive.number.AbstractNonNullNumberGenerator
getGeneratedType, getGranularity, getMax, getMin, isParallelizable, isThreadSafe, setGranularity, setMax, setMin
 
Methods inherited from class org.databene.benerator.util.AbstractNonNullGenerator
generate
 
Methods inherited from class org.databene.benerator.util.AbstractGenerator
assertInitialized, assertNotInitialized, getResultWrapper, toString, wasInitialized
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.databene.benerator.Generator
wasInitialized
 

Constructor Detail

RecurrenceRelationNumberGenerator

public RecurrenceRelationNumberGenerator(java.lang.Class<E> targetType,
                                         int depth,
                                         E min,
                                         E max)
Method Detail

getDepth

public int getDepth()

getN

public int getN()

init

public void init(GeneratorContext context)
Specified by:
init in interface Generator<E extends java.lang.Number>
Overrides:
init in class AbstractNonNullNumberGenerator<E extends java.lang.Number>

generate

public E generate()
Specified by:
generate in interface NonNullGenerator<E extends java.lang.Number>
Specified by:
generate in class AbstractNonNullGenerator<E extends java.lang.Number>

reset

public void reset()
See Resettable.reset()

Specified by:
reset in interface org.databene.commons.Resettable
Overrides:
reset in class AbstractGenerator<E extends java.lang.Number>

close

public void close()
See Generator.close()

Specified by:
close in interface java.io.Closeable
Specified by:
close in interface Generator<E extends java.lang.Number>
Overrides:
close in class AbstractGenerator<E extends java.lang.Number>

a0

protected abstract E a0(int n)
Must be implemented by child classes to return the seed values of the recurrence relation. These are the initial values which are defined as constants (a(0)..a(depth-1)).


aN

protected abstract E aN()
Must be implemented by child classes to implement the recurrence relation. It needs to use the aN(int) method to retrieve the most recent calculated values.


aN

protected final E aN(int offset)
Provides the most recent calculated values. The index is the relative index, -1 stands for a(N-1).


resetMembers

protected void resetMembers()

calculateNext

protected E calculateNext()


Copyright © 2013. All Rights Reserved.