dev.marksman.composablerandom.frequency.FrequencyMapBuilderN Maven / Gradle / Ivy
package dev.marksman.composablerandom.frequency;
import com.jnape.palatable.lambda.functions.Fn1;
import dev.marksman.composablerandom.Generator;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
class FrequencyMapBuilderN implements FrequencyMapBuilder {
private final FrequencyMap result;
@Override
public FrequencyMapBuilder add(int weight, Generator extends A> generator) {
if (weight > 0) {
return frequencyMapBuilderN(result.add(weight, generator));
} else {
return this;
}
}
@Override
public FrequencyMapBuilder combine(FrequencyMap other) {
return frequencyMapBuilderN(result.combine(other));
}
@Override
public FrequencyMap build() {
return result;
}
@Override
public FrequencyMapBuilder fmap(Fn1 super A, ? extends B> fn) {
return frequencyMapBuilderN(result.fmap(fn));
}
@Override
public FrequencyMapBuilder multiply(int positiveFactor) {
return frequencyMapBuilderN(result.multiply(positiveFactor));
}
@SuppressWarnings("unchecked")
static FrequencyMapBuilderN frequencyMapBuilderN(FrequencyMap extends A> frequencyMap) {
return new FrequencyMapBuilderN<>((FrequencyMap) frequencyMap);
}
}