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

com.robertboothby.djenni.lang.CharacterSupplierBuilder Maven / Gradle / Ivy

Go to download

This module holds the core components of the Djenni data generator framework. By itself it provides most of the components to create an efficient end to end data generation framework for a specific domain. It contains the core SupplierBuilder interfaces, implementations for the core Java data types and useful test components. It is intended to be used in conjunction with the source-generator module that will speed up delivery of domain specific data generation and also with common domain modules (TBD) that deliver standard generators for common frameworks such as JAXB.

There is a newer version: 0.2.0
Show newest version
package com.robertboothby.djenni.lang;

import com.robertboothby.djenni.SupplierBuilder;
import com.robertboothby.djenni.core.CharacterStrings;
import com.robertboothby.djenni.core.StreamableSupplier;
import com.robertboothby.djenni.distribution.Distribution;
import com.robertboothby.djenni.distribution.simple.SimpleRandomIntegerDistribution;

import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Supplier;

import static com.robertboothby.djenni.core.SupplierHelper.buildAn;
import static com.robertboothby.djenni.core.SupplierHelper.fix;
import static com.robertboothby.djenni.lang.IntegerSupplierBuilder.integerSupplier;
import static java.util.Arrays.copyOf;

/**
 * Builder intended to make it expressive and easy to configure a Supplier of Characters.
 * @author robertboothby
 */
public class CharacterSupplierBuilder implements SupplierBuilder, CharacterStrings {

    //The default values used by the generator
    public static final String DEFAULT_AVAILABLE_CHARACTERS = ENGLISH_ALPHABETIC_UPPER;
    public static final Distribution DEFAULT_CHARACTER_SELECTION_DISTRIBUTION = SimpleRandomIntegerDistribution.UNIFORM;

    private char[] characters = DEFAULT_AVAILABLE_CHARACTERS .toCharArray();
    private Distribution distribution = DEFAULT_CHARACTER_SELECTION_DISTRIBUTION;

    public StreamableSupplier build() {
        if(characters.length == 1){
            return fix(characters[0]);
        } else if (characters.length > 1) {
            char[] charactersCopy = Arrays.copyOf(characters, characters.length);
            Supplier positionGenerator = buildAn(
                    integerSupplier()
                            .between(0)
                            .and(characters.length)
                            .withDistribution(distribution));
            return () -> charactersCopy[positionGenerator.get()];
        } else {
            throw new IllegalArgumentException("There must be some characters to generate.");
        }
    }

    public CharacterSupplierBuilder withCharacters(char[] characters){
        this.characters = copyOf(characters, characters.length);
        return this;
    }

    public CharacterSupplierBuilder withCharacters(String characterString) {
        this.characters = characterString.toCharArray();
        return this;
    }

    public CharacterSupplierBuilder withDistribution(Distribution distribution) {
        this.distribution = distribution;
        return this;
    }

    public static CharacterSupplierBuilder characterSupplier(){
        return new CharacterSupplierBuilder();
    }

    public static StreamableSupplier characterSupplier(Consumer config){
        return SupplierBuilder.buildConfig(new CharacterSupplierBuilder(), config);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy