com.pholser.junit.quickcheck.generator
Class Generator<T>

java.lang.Object
  extended by com.pholser.junit.quickcheck.generator.Generator<T>
Type Parameters:
T - type of theory parameter to apply this generator's values to
Direct Known Subclasses:
ArrayGenerator, ComponentizedGenerator, CompositeGenerator, EnumGenerator, LambdaGenerator, ZilchGenerator

public abstract class Generator<T>
extends Object

Produces values for theory parameters.


Constructor Summary
protected Generator(Class<T> type)
           
protected Generator(List<Class<T>> types)
          Used for generators of primitives/their wrappers.
 
Method Summary
 void addComponentGenerators(List<Generator<?>> components)
          Adds component generators to this generator.
 boolean canGenerateForParametersOfTypes(List<org.javaruntype.type.TypeParameter<?>> typeParameters)
           
 boolean canRegisterAsType(Class<?> type)
          Tells whether this generator is allowed to be used for theory parameters of the given type.
static boolean compatibleWithTypeParameter(org.javaruntype.type.TypeParameter<?> parameter, Class<?> clazz)
           
 void configure(Map<Class<? extends Annotation>,Annotation> configurationsByType)
          Tells this generator to configure itself using annotations from a theory parameter.
abstract  T generate(SourceOfRandomness random, GenerationStatus status)
          Produces a value for a theory parameter.
 boolean hasComponents()
           
 int numberOfNeededComponents()
           
 List<Class<T>> types()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Generator

protected Generator(Class<T> type)
Parameters:
type - class token for type of theory parameter this generator is applicable to

Generator

protected Generator(List<Class<T>> types)
Used for generators of primitives/their wrappers. For example, a Generator<Integer> can be used for theory parameters of type Integer or int.

Parameters:
types - class tokens for type of theory parameter this generator is applicable to
Method Detail

types

public List<Class<T>> types()
Returns:
class tokens for the types of theory parameters this generator is applicable to

canRegisterAsType

public boolean canRegisterAsType(Class<?> type)
Tells whether this generator is allowed to be used for theory parameters of the given type.

Parameters:
type - type against which to test this generator
Returns:
true if the generator is allowed to participate in generating values for theory parameters of type.

generate

public abstract T generate(SourceOfRandomness random,
                           GenerationStatus status)

Produces a value for a theory parameter.

A generator may raise an unchecked exception if some condition exists which would lead to a confusing generation -- for example, if a generator honored a range configuration, and the endpoints were transposed.

Parameters:
random - a source of randomness to be used when generating the value
status - an object that the generator can use to influence the value it produces. For example, a generator for lists can use the size method to generate lists with a given number of elements.
Returns:
the generated value

hasComponents

public boolean hasComponents()
Returns:
whether this generator has component generators, such as for those generators that produce lists or arrays.
See Also:
addComponentGenerators(java.util.List)

numberOfNeededComponents

public int numberOfNeededComponents()
Returns:
how many component generators this generator needs
See Also:
addComponentGenerators(java.util.List)

addComponentGenerators

public void addComponentGenerators(List<Generator<?>> components)

Adds component generators to this generator.

Some generators need component generators to create proper values. For example, lists require a single component generator in order to generate elements that have the type of the list parameter's type argument.

Parameters:
components - component generators to add

canGenerateForParametersOfTypes

public boolean canGenerateForParametersOfTypes(List<org.javaruntype.type.TypeParameter<?>> typeParameters)
Parameters:
typeParameters - a list of generic type parameters
Returns:
whether this generator can be considered for generating values for theory parameters that have the given type parameters in their signatures

compatibleWithTypeParameter

public static boolean compatibleWithTypeParameter(org.javaruntype.type.TypeParameter<?> parameter,
                                                  Class<?> clazz)
Parameters:
parameter - a generic type parameter
clazz - a type
Returns:
whether the type is compatible with the generic type parameter
See Also:
canGenerateForParametersOfTypes(List)

configure

public void configure(Map<Class<? extends Annotation>,Annotation> configurationsByType)

Tells this generator to configure itself using annotations from a theory parameter.

The annotations fed to this method will be those annotations on the theory parameter that are themselves marked with GeneratorConfiguration.

By default, the generator will configure itself by:

Parameters:
configurationsByType - a map of configuration annotations, keyed by annotation type


Copyright © 2013. All Rights Reserved.