All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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 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 fn) {
        return frequencyMapBuilderN(result.fmap(fn));
    }

    @Override
    public FrequencyMapBuilder multiply(int positiveFactor) {
        return frequencyMapBuilderN(result.multiply(positiveFactor));
    }

    @SuppressWarnings("unchecked")
    static  FrequencyMapBuilderN frequencyMapBuilderN(FrequencyMap frequencyMap) {
        return new FrequencyMapBuilderN<>((FrequencyMap) frequencyMap);
    }
}