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

com.robertboothby.djenni.util.CollectionSupplierBuilder 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.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * Builder for all supplier types based on {@link Collection}. It relies on instances of the {@link CollectionType} interface to instantiate
 * the collections being built.
 * 

* There are static methods for most types of Collection on the CollectionType interface or its descendents covering most * if not all the collections shipped with the JVM. * @param The type of the Collection. * @param The type of the values to be held in the collection. */ public class CollectionSupplierBuilder, U> implements SupplierBuilder { private final CollectionType collectionType; private Supplier> contentSupplier; /** * Create an instance of the builder for the given collection type. * @param collectionType the type of collection to build. */ private CollectionSupplierBuilder(CollectionType collectionType) { this.collectionType = collectionType; } @Override public StreamableSupplier build() { return () -> collectionType.instance(contentSupplier.get()); } /** * Set the supplier for the content. * @param contentSupplier the content supplier. * @return The builder for further configuration. */ public CollectionSupplierBuilder withContent(Supplier> contentSupplier) { this.contentSupplier = contentSupplier; return this; } /** * Set the supplier for the content using a builder. * @param contentSupplierBuilder a content supplier builder that will immediately be built. * @return The builder for further configuration. */ public CollectionSupplierBuilder withContent(SupplierBuilder> contentSupplierBuilder) { this.contentSupplier = contentSupplierBuilder.build(); return this; } /** * Get an instance of the builder configured to create instances of a particular Collection type. *

* This method will need extra Generic typing information * (

CollectionSupplierBuilder.<LinkedList<String>, String>collection(linkedList())
) * to be used successfully. * @param collectionType The type of Collection to build. * @param The type of the collection. * @param The type of the values to be held in the collection. * @return a configured collection builder. */ public static , U> CollectionSupplierBuilder collection(CollectionType collectionType) { return new CollectionSupplierBuilder<>(collectionType); } /** * Get an instance of the builder configured to create instances of a particular Collection type. *

* By taking the class as a parameter this method reduces the generic typing boiler plate. * @param collectionType The type of Collection to build. * @param tClass The class of the value to be held in the collection. * @param The type of the collection. * @param The type of the values to be held in the collection. * @return a configured collection builder. */ public static , U> CollectionSupplierBuilder collection(CollectionType collectionType, Class tClass) { return new CollectionSupplierBuilder<>(collectionType); } /** * Get an instance of the builder configured to create instances of a particular collection type. * @param collectionType The type of Collection to build. * @param valueSupplier the value supplier to use. * @param The type of the collection. * @param The type of the values to be held in the collection. * @return a configured collection builder. */ public static , U> CollectionSupplierBuilder collection(CollectionType collectionType, Supplier valueSupplier) { return new CollectionSupplierBuilder<>(collectionType); } /** * Create a Supplier using the passed in configuration. * @param collectionType The type of Collection to supply. * @param configuration The configuration to use. * @param The generic type of Collections supplied. * @param The generic type of the values contained in the Collections. * @return The configured supplier. */ public static , U> Supplier collection(CollectionType collectionType, Consumer> configuration){ CollectionSupplierBuilder builder = collection(collectionType); configuration.accept(builder); return builder.build(); } }