Interface Generator<A>
-
- Type Parameters:
A
- the output type
- All Superinterfaces:
com.jnape.palatable.lambda.functor.Applicative<A,Generator<?>>
,com.jnape.palatable.lambda.functor.Functor<A,Generator<?>>
,com.jnape.palatable.lambda.monad.Monad<A,Generator<?>>
,ToGenerator<A>
- All Known Subinterfaces:
FloatingPointGenerator<A>
public interface Generator<A> extends com.jnape.palatable.lambda.monad.Monad<A,Generator<?>>, ToGenerator<A>
A strategy for generating random values of typeA
.To use a
Generator
, call one of itsrun
methods (run()
,run(Seed)
,run(GeneratorParameters)
, orrun(GeneratorParameters, Seed)
). These will give you aValueSupply
, which is an infiniteIterable
with some additional convenience methods.Several built-in
Generator
s are provided inGenerators
. These can be used as is or composed with others to buildGenerator
s that yield more complex types.All
Generator
s are immutable and contain no state. Any method that appears to mutate aGenerator
actually creates a newGenerator
, while leaving the original unchanged.Since a
Generator
is aFunctor
, its output can be mapped usingfmap(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends B>)
. It is aMonad
as well, so it can be composed with otherGenerator
s usingflatMap(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends com.jnape.palatable.lambda.monad.Monad<B, software.kes.kraftwerk.Generator<?>>>)
.Generator
s, if properly designed, obey the functor and monad laws.Generator
s can not, however, be filtered. AGenerator
provides the guarantee that when invoked for anySeed
, it will eventually yield a value. Filtering would remove that guarantee. If you need to filter the values you are interested in, either change the design of your generator to only produce those values, or filter the resultingValueSupply
instead.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Generator<java.util.ArrayList<A>>
arrayList()
Creates a newGenerator
that yieldsArrayList
s of various sizes, with thisGenerator
generating the elements.default Generator<java.util.ArrayList<A>>
arrayListOfSize(int size)
Creates a newGenerator
that yieldsArrayList
s of a specific size, with thisGenerator
generating the elements.default Generator<java.util.ArrayList<A>>
arrayListOfSize(IntRange sizeRange)
Creates a newGenerator
that yieldsArrayList
s of various sizes within a specific range, with thisGenerator
generating the elements.default Generator<A>
attachApplicationData(java.lang.Object applicationData)
Creates a newGenerator
that is the same as this one, with the attached application changed to the object provided.GenerateFn<A>
createGenerateFn(GeneratorParameters generatorParameters)
Creates aGenerateFn
using the givenGeneratorParameters
.default <B> Generator<B>
flatMap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends com.jnape.palatable.lambda.monad.Monad<B,Generator<?>>> f)
Creates a newGenerator
that, when invoked, feeds the output of thisGenerator
to a function that returns anotherGenerator
, and invokes that.default <B> Generator<B>
fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> fn)
Creates a newGenerator
by mapping the output of thisGenerator
.default com.jnape.palatable.lambda.adt.Maybe<java.lang.Object>
getApplicationData()
Returns the application-specific opaque object associated with thisGenerator
, if any.default com.jnape.palatable.lambda.adt.Maybe<java.lang.String>
getLabel()
Returns an optional label used in diagnostics.default Generator<A>
injectSpecialValue(A specialValue)
Creates a newGenerator
with a special value mixed into the output of this one.default Generator<A>
injectSpecialValues(software.kes.enhancediterables.NonEmptyFiniteIterable<A> values)
Creates a newGenerator
with special values mixed into the output of this one.default Generator<com.jnape.palatable.lambda.adt.Maybe<A>>
just()
Converts thisGenerator<A>
into aGenerator<Maybe<A>>
that always yieldsjust
.default Generator<A>
labeled(java.lang.String label)
Creates a newGenerator
that is the same as this one, with the label changed to the one provided.default <R> Generator<com.jnape.palatable.lambda.adt.Either<A,R>>
left()
Converts thisGenerator<A>
into aGenerator<Either<A, R>>
that always yieldsleft
.default Generator<com.jnape.palatable.lambda.adt.Maybe<A>>
maybe()
Converts thisGenerator<A>
into aGenerator<Maybe<A>>
that usually yieldsjust
, but occasionally yieldsnothing
.default Generator<com.jnape.palatable.lambda.adt.Maybe<A>>
maybe(MaybeWeights weights)
Converts thisGenerator<A>
into aGenerator<Maybe<A>>
with custom probabilities forjust
vs.default Generator<java.util.ArrayList<A>>
nonEmptyArrayList()
Creates a newGenerator
that yields non-emptyArrayList
s of various sizes, with thisGenerator
generating the elements.default Generator<software.kes.collectionviews.ImmutableNonEmptyVector<A>>
nonEmptyVector()
Creates a newGenerator
that yieldsNonEmptyVector
s of various sizes, with thisGenerator
generating the elements.default Generator<software.kes.collectionviews.ImmutableNonEmptyVector<A>>
nonEmptyVectorOfSize(int size)
Creates a newGenerator
that yieldsNonEmptyVector
s of a specific size, with thisGenerator
generating the elements.default Generator<software.kes.collectionviews.ImmutableNonEmptyVector<A>>
nonEmptyVectorOfSize(IntRange sizeRange)
Creates a newGenerator
that yieldsNonEmptyVector
s of various sizes within a specific range, with thisGenerator
generating the elements.default Generator<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,A>>
pair()
Creates a newGenerator
that yields pairs of values generated by this one.default <B> Generator<B>
pure(B b)
Thepure
method to satisfy theMonad
interface.default <L> Generator<com.jnape.palatable.lambda.adt.Either<L,A>>
right()
Converts thisGenerator<A>
into aGenerator<Either<L, A>>
that always yieldsright
.default ValueSupply<A>
run()
Creates aValueSupply
from a random initial seed, using the built-in defaultGeneratorParameters
.default ValueSupply<A>
run(GeneratorParameters generatorParameters)
Creates aValueSupply
from customGeneratorParameters
, and a random initial seed.default ValueSupply<A>
run(GeneratorParameters generatorParameters, Seed initialSeed)
default ValueSupply<A>
run(Seed initialSeed)
default Generator<A>
toGenerator()
default Generator<com.jnape.palatable.lambda.adt.hlist.Tuple3<A,A,A>>
triple()
Creates a newGenerator
that yields triples of values generated by this one.default Generator<software.kes.collectionviews.ImmutableVector<A>>
vector()
Creates a newGenerator
that yieldsVector
s of various sizes, with thisGenerator
generating the elements.default Generator<software.kes.collectionviews.ImmutableVector<A>>
vectorOfSize(int size)
Creates a newGenerator
that yieldsVector
s of a specific size, with thisGenerator
generating the elements.default Generator<software.kes.collectionviews.ImmutableVector<A>>
vectorOfSize(IntRange sizeRange)
Creates a newGenerator
that yieldsVector
s of various sizes within a specific range, with thisGenerator
generating the elements.default Weighted<Generator<A>>
weighted()
Creates aWeighted
instance of thisGenerator
with a weight of 1.default Weighted<Generator<A>>
weighted(int weight)
Creates aWeighted
instance of thisGenerator
with a custom weight.default Generator<A>
withNulls()
Creates a newGenerator
that is the same as this one, but occasionally yieldsnull
.default Generator<A>
withNulls(NullWeights weights)
Creates a newGenerator
that is the same as this one, but occasionally yieldsnull
, with a custom probability.default <B,C>
Generator<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, Generator<B> other)
Creates a newGenerator
that combines the output of anotherGenerator
using a function to yield the final output.
-
-
-
Method Detail
-
createGenerateFn
GenerateFn<A> createGenerateFn(GeneratorParameters generatorParameters)
Creates aGenerateFn
using the givenGeneratorParameters
. This is the only method that aGenerator
needs to implement. Since this is a low level method, users may wish to call once of the higher levelrun
methods instead.Implementers must follow these rules: - createGenerateFn must be referentially transparent; it must yield the equivalent
GenerateFn
for all calls for a givenSeed
- TheGenerateFn
must pure and referentially transparent - TheGenerateFn
must be guaranteed to yield a value - TheGenerateFn
must not mutate the inputSeed
; it must return a newSeed
- Parameters:
generatorParameters
- theGeneratorParameters
- Returns:
- a
GenerateFn<A>
- See Also:
run()
,run(Seed)
,run(GeneratorParameters)
,run(GeneratorParameters, Seed)
-
run
default ValueSupply<A> run(GeneratorParameters generatorParameters, Seed initialSeed)
- Parameters:
generatorParameters
- theGeneratorParameters
initialSeed
- the initialSeed
- Returns:
- a
ValueSupply<A>
- See Also:
run()
,run(Seed)
,run(GeneratorParameters)
-
run
default ValueSupply<A> run(Seed initialSeed)
- Parameters:
initialSeed
- the initialSeed
- Returns:
- a
ValueSupply<A>
- See Also:
run()
,run(GeneratorParameters)
,run(GeneratorParameters, Seed)
-
run
default ValueSupply<A> run(GeneratorParameters generatorParameters)
Creates aValueSupply
from customGeneratorParameters
, and a random initial seed. Calling this will result in a differentValueSupply
each time.- Returns:
- a
ValueSupply<A>
- See Also:
run()
,run(Seed)
,run(GeneratorParameters, Seed)
-
run
default ValueSupply<A> run()
Creates aValueSupply
from a random initial seed, using the built-in defaultGeneratorParameters
. Calling this will result in a differentValueSupply
each time.- Returns:
- a
ValueSupply<A>
- See Also:
run(Seed)
,run(GeneratorParameters)
,run(GeneratorParameters, Seed)
-
fmap
default <B> Generator<B> fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> fn)
Creates a newGenerator
by mapping the output of thisGenerator
.- Specified by:
fmap
in interfacecom.jnape.palatable.lambda.functor.Applicative<A,Generator<?>>
- Specified by:
fmap
in interfacecom.jnape.palatable.lambda.functor.Functor<A,Generator<?>>
- Specified by:
fmap
in interfacecom.jnape.palatable.lambda.monad.Monad<A,Generator<?>>
- Type Parameters:
B
- the new output type- Parameters:
fn
- the mapping function- Returns:
- a
Generator<B>
-
flatMap
default <B> Generator<B> flatMap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends com.jnape.palatable.lambda.monad.Monad<B,Generator<?>>> f)
Creates a newGenerator
that, when invoked, feeds the output of thisGenerator
to a function that returns anotherGenerator
, and invokes that.
-
pure
default <B> Generator<B> pure(B b)
Thepure
method to satisfy theMonad
interface.- Specified by:
pure
in interfacecom.jnape.palatable.lambda.functor.Applicative<A,Generator<?>>
- Specified by:
pure
in interfacecom.jnape.palatable.lambda.monad.Monad<A,Generator<?>>
- See Also:
Generators.constant(A)
-
toGenerator
default Generator<A> toGenerator()
- Specified by:
toGenerator
in interfaceToGenerator<A>
-
getLabel
default com.jnape.palatable.lambda.adt.Maybe<java.lang.String> getLabel()
Returns an optional label used in diagnostics.- Returns:
a Maybe<String>
- See Also:
labeled(String)
-
getApplicationData
default com.jnape.palatable.lambda.adt.Maybe<java.lang.Object> getApplicationData()
Returns the application-specific opaque object associated with thisGenerator
, if any.- Returns:
a Maybe<Object>
- See Also:
attachApplicationData(Object)
-
labeled
default Generator<A> labeled(java.lang.String label)
Creates a newGenerator
that is the same as this one, with the label changed to the one provided.- Parameters:
label
- the label- Returns:
- a
Generator<A>
- See Also:
getLabel()
-
attachApplicationData
default Generator<A> attachApplicationData(java.lang.Object applicationData)
Creates a newGenerator
that is the same as this one, with the attached application changed to the object provided.- Parameters:
applicationData
- an opaque application-defined object of any type to be used as metadata- Returns:
- a
Generator<A>
- See Also:
getApplicationData()
-
pair
default Generator<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,A>> pair()
Creates a newGenerator
that yields pairs of values generated by this one.- Returns:
- a
Generator<Tuple2<A, A>>
-
triple
default Generator<com.jnape.palatable.lambda.adt.hlist.Tuple3<A,A,A>> triple()
Creates a newGenerator
that yields triples of values generated by this one.- Returns:
- a
Generator<Tuple3<A, A, A>>
-
weighted
default Weighted<Generator<A>> weighted()
Creates aWeighted
instance of thisGenerator
with a weight of 1.- Returns:
- a
Weighted<Generator<A>>
-
weighted
default Weighted<Generator<A>> weighted(int weight)
Creates aWeighted
instance of thisGenerator
with a custom weight.- Parameters:
weight
- the weight value; must be >= 0- Returns:
- a
Weighted<Generator<A>>
-
just
default Generator<com.jnape.palatable.lambda.adt.Maybe<A>> just()
Converts thisGenerator<A>
into aGenerator<Maybe<A>>
that always yieldsjust
.- Returns:
- a
Generator<Maybe<A>>
-
maybe
default Generator<com.jnape.palatable.lambda.adt.Maybe<A>> maybe()
Converts thisGenerator<A>
into aGenerator<Maybe<A>>
that usually yieldsjust
, but occasionally yieldsnothing
.- Returns:
- a
Generator<Maybe<A>>
-
maybe
default Generator<com.jnape.palatable.lambda.adt.Maybe<A>> maybe(MaybeWeights weights)
Converts thisGenerator<A>
into aGenerator<Maybe<A>>
with custom probabilities forjust
vs.nothing
.- Returns:
- a
Generator<Maybe<A>>
-
left
default <R> Generator<com.jnape.palatable.lambda.adt.Either<A,R>> left()
Converts thisGenerator<A>
into aGenerator<Either<A, R>>
that always yieldsleft
.- Returns:
- a
Generator<Either<A, R>>
-
right
default <L> Generator<com.jnape.palatable.lambda.adt.Either<L,A>> right()
Converts thisGenerator<A>
into aGenerator<Either<L, A>>
that always yieldsright
.- Returns:
- a
Generator<Either<L, A>>
-
withNulls
default Generator<A> withNulls()
Creates a newGenerator
that is the same as this one, but occasionally yieldsnull
.- Returns:
- a
Generator<Maybe<A>>
-
withNulls
default Generator<A> withNulls(NullWeights weights)
Creates a newGenerator
that is the same as this one, but occasionally yieldsnull
, with a custom probability.- Returns:
- a
Generator<Maybe<A>>
-
arrayList
default Generator<java.util.ArrayList<A>> arrayList()
Creates a newGenerator
that yieldsArrayList
s of various sizes, with thisGenerator
generating the elements.- Returns:
- a
Generator<ArrayList<A>>
-
nonEmptyArrayList
default Generator<java.util.ArrayList<A>> nonEmptyArrayList()
Creates a newGenerator
that yields non-emptyArrayList
s of various sizes, with thisGenerator
generating the elements.- Returns:
- a
Generator<ArrayList<A>>
-
arrayListOfSize
default Generator<java.util.ArrayList<A>> arrayListOfSize(int size)
Creates a newGenerator
that yieldsArrayList
s of a specific size, with thisGenerator
generating the elements.- Parameters:
size
- the size of theArrayList
s generated; must be >=0- Returns:
- a
Generator<ArrayList<A>>
-
arrayListOfSize
default Generator<java.util.ArrayList<A>> arrayListOfSize(IntRange sizeRange)
Creates a newGenerator
that yieldsArrayList
s of various sizes within a specific range, with thisGenerator
generating the elements.- Parameters:
sizeRange
- theIntRange
of the sizes ofArrayList
s generated- Returns:
- a
Generator<ArrayList<A>>
-
vector
default Generator<software.kes.collectionviews.ImmutableVector<A>> vector()
Creates a newGenerator
that yieldsVector
s of various sizes, with thisGenerator
generating the elements.- Returns:
- a
Generator<ImmutableVector<A>>
-
vectorOfSize
default Generator<software.kes.collectionviews.ImmutableVector<A>> vectorOfSize(int size)
Creates a newGenerator
that yieldsVector
s of a specific size, with thisGenerator
generating the elements.- Parameters:
size
- the size of theVector
s generated; must be >=0- Returns:
- a
Generator<ImmutableVector<A>>
-
vectorOfSize
default Generator<software.kes.collectionviews.ImmutableVector<A>> vectorOfSize(IntRange sizeRange)
Creates a newGenerator
that yieldsVector
s of various sizes within a specific range, with thisGenerator
generating the elements.- Parameters:
sizeRange
- theIntRange
of the sizes ofVector
s generated- Returns:
- a
Generator<ImmutableVector<A>>
-
nonEmptyVector
default Generator<software.kes.collectionviews.ImmutableNonEmptyVector<A>> nonEmptyVector()
Creates a newGenerator
that yieldsNonEmptyVector
s of various sizes, with thisGenerator
generating the elements.- Returns:
- a
Generator<ImmutableNonEmptyVector<A>>
-
nonEmptyVectorOfSize
default Generator<software.kes.collectionviews.ImmutableNonEmptyVector<A>> nonEmptyVectorOfSize(int size)
Creates a newGenerator
that yieldsNonEmptyVector
s of a specific size, with thisGenerator
generating the elements.- Parameters:
size
- the size of theNonEmptyVector
s generated; must be >=1- Returns:
- a
Generator<ImmutableNonEmptyVector<A>>
-
nonEmptyVectorOfSize
default Generator<software.kes.collectionviews.ImmutableNonEmptyVector<A>> nonEmptyVectorOfSize(IntRange sizeRange)
Creates a newGenerator
that yieldsNonEmptyVector
s of various sizes within a specific range, with thisGenerator
generating the elements.- Parameters:
sizeRange
- theIntRange
of the sizes ofNonEmptyVector
s generated- Returns:
- a
Generator<ImmutableNonEmptyVector<A>>
-
zipWith
default <B,C> Generator<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, Generator<B> other)
Creates a newGenerator
that combines the output of anotherGenerator
using a function to yield the final output.- Type Parameters:
B
- the output type of the otherGenerator
C
- the new output type- Parameters:
fn
- a function that takes the output of thisGenerator
and the output ofother
, and returns the final outputother
- the otherGenerator
- Returns:
- a
Generator<C>
-
injectSpecialValues
default Generator<A> injectSpecialValues(software.kes.enhancediterables.NonEmptyFiniteIterable<A> values)
Creates a newGenerator
with special values mixed into the output of this one. Special values will be represented more frequently than non-special values.- Parameters:
values
- the special values to mix in- Returns:
- a
Generator<A>
-
injectSpecialValue
default Generator<A> injectSpecialValue(A specialValue)
Creates a newGenerator
with a special value mixed into the output of this one. Special values will be represented more frequently than non-special values.- Parameters:
specialValue
- the special value- Returns:
- a
Generator<A>
-
-