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

com.robertboothby.djenni.util.SimpleMapSupplierBuilder 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.util;

import com.robertboothby.djenni.SupplierBuilder;
import com.robertboothby.djenni.core.StreamableSupplier;

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

import static com.robertboothby.djenni.core.SupplierHelper.fix;
import static java.util.stream.Collectors.toMap;

/**
 * This SupplierBuilder creates a supplier of {@link java.util.Map}s. This can be used standalone or with the
 * {@link }MapSupplierHelper} and {@link MapTypes} to create a specific Map implementation.
 * @param  The type of the keys,
 * @param  The type of the values.
 */
public class SimpleMapSupplierBuilder implements SupplierBuilder> {

    private StreamableSupplier> entrySupplier;
    private Supplier numberOfEntries = fix(0);

    @Override
    public StreamableSupplier> build() {
        return () -> entrySupplier.stream(numberOfEntries.get()).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    /**
     * Provide a Supplier of entries for the simple map. If the entry supplier may create entries with duplicate keys then
     * the number of entries in the maps produced may be reduced.
     * @param entrySupplier The Supplier.
     * @param  The type of the entry that will be supplied.
     * @return the SupplierBuilder for further configuration.
     */
    public > SimpleMapSupplierBuilder withEntries(StreamableSupplier entrySupplier) {
        this.entrySupplier = entrySupplier;
        return this;
    }

    /**
     * Provide an Integer Supplier for the number of entries to be created in the map. If the entry supplier provides
     * duplicate keys then the numbe of entries may be reduced.
     * @param numberOfEntries the Supplier for the number of entries.
     * @return The builder for further configuration.
     */
    public SimpleMapSupplierBuilder withNumberOfEntries(Supplier numberOfEntries) {
        this.numberOfEntries = numberOfEntries;
        return this;
    }

    /**
     * Convenience method creating the SupplierBuilder with no configuration.
     * @param  The type of the keys in the supplied maps.
     * @param  The type of the values in the supplied maps.
     * @return The SupplierBuilder for configuration.
     */
    public static  SimpleMapSupplierBuilder map(){
        return new SimpleMapSupplierBuilder<>();
    }

    /**
     * Convenience method, creating a Supplier based on the passed in configuration.
     * @param configuration a consumer that will apply the configuration.
     * @param  The type of the keys in the supplied maps.
     * @param  The type of the values in the supplied maps.
     * @return The configured supplier.
     */
    public static  Supplier> configure(Consumer> configuration){
        return SupplierBuilder.buildConfig(map(), configuration);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy