dev.marksman.kraftwerk.choice.ChoiceBuilder6 Maven / Gradle / Ivy
Show all versions of kraftwerk Show documentation
package dev.marksman.kraftwerk.choice;
import com.jnape.palatable.lambda.adt.choice.Choice6;
import com.jnape.palatable.lambda.adt.choice.Choice7;
import dev.marksman.kraftwerk.Generator;
import dev.marksman.kraftwerk.Generators;
import dev.marksman.kraftwerk.ToGenerator;
import dev.marksman.kraftwerk.Weighted;
import dev.marksman.kraftwerk.frequency.FrequencyMap;
import static dev.marksman.kraftwerk.Generators.constant;
import static dev.marksman.kraftwerk.choice.ChoiceBuilder7.choiceBuilder7;
/**
* A builder to facilitate the construction of {@link Generator}s that yield {@link Choice6} values.
*
* Use one of the {@link ChoiceBuilder6#or} methods to add more choices, and call {@link ChoiceBuilder6#toGenerator} to
* build the final {@link Generator}.
*
* All instances of {@code ChoiceBuilder} are immutable and can be reused, even after calling {@code toGenerator}.
*
* @param the type of the first choice
* @param the type of the second choice
* @param the type of the third choice
* @param the type of the fourth choice
* @param the type of the fifth choice
* @param the type of the sixth choice
*/
public final class ChoiceBuilder6 implements ToGenerator> {
private final FrequencyMap> frequencyMap;
private ChoiceBuilder6(FrequencyMap> frequencyMap) {
this.frequencyMap = frequencyMap;
}
static ChoiceBuilder6 choiceBuilder6(FrequencyMap> frequencyMap) {
return new ChoiceBuilder6<>(frequencyMap);
}
/**
* Builds the final {@link Generator}.
*/
@Override
public Generator> toGenerator() {
return frequencyMap.toGenerator();
}
/**
* Adds another choice.
*
* @param weightedGenerator a weighted {@code Generator} for the next choice
* @param the type of the next choice
* @return a {@code ChoiceBuilder7}
*/
public ChoiceBuilder7 or(Weighted extends Generator extends G>> weightedGenerator) {
FrequencyMap> newFrequencyMap = frequencyMap
.>fmap(c6 ->
c6.match(Choice7::a, Choice7::b, Choice7::c, Choice7::d, Choice7::e, Choice7::f))
.add(weightedGenerator.fmap(gen -> gen.fmap(Choice7::g)));
return choiceBuilder7(newFrequencyMap);
}
/**
* Adds another choice.
*
* @param gen a {@code Generator} for the next choice
* @param the type of the next choice
* @return a {@code ChoiceBuilder7}
*/
public ChoiceBuilder7 or(Generator gen) {
return or(gen.weighted());
}
/**
* Adds another choice.
*
* @param weightedValue a weighted value for the next choice
* @param the type of the next choice
* @return a {@code ChoiceBuilder7}
*/
public ChoiceBuilder7 orValue(Weighted extends G> weightedValue) {
return or(weightedValue.fmap(Generators::constant));
}
/**
* Adds another choice.
*
* @param value a value for the next choice
* @param the type of the next choice
* @return a {@code ChoiceBuilder7}
*/
public ChoiceBuilder7 orValue(G value) {
return or(constant(value).weighted());
}
}