com.robertboothby.djenni.lang.DoubleSupplierBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
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.
package com.robertboothby.djenni.lang;
import com.robertboothby.djenni.SupplierBuilder;
import com.robertboothby.djenni.core.StreamableSupplier;
import com.robertboothby.djenni.distribution.Distribution;
import com.robertboothby.djenni.distribution.simple.SimpleRandomDoubleDistribution;
import com.robertboothby.djenni.sugar.And;
import java.util.function.Consumer;
import static com.robertboothby.djenni.SupplierBuilder.buildConfig;
/**
* Simple implementation of a Double Supplier. It cannot supply the full range of values as it works on an exclusive basis
* and can only gwnerate a range of values constrained by {@link Double#MAX_VALUE}.
*/
public class DoubleSupplierBuilder implements SupplierBuilder {
private Distribution distribution = SimpleRandomDoubleDistribution.UNIFORM;
private Double minimumValue = 0.0D;
private Double range = 0.0D;
@Override
public StreamableSupplier build() {
return () -> minimumValue + distribution.generate(range);
}
/**
* Define the base distribution to use. Default is {@link SimpleRandomDoubleDistribution#UNIFORM}.
* @param distribution The distribution to use.
* @return The builder for further configuration,
*/
public DoubleSupplierBuilder withDistribution(Distribution distribution) {
this.distribution = distribution;
return this;
}
/**
* Define the range of values to be supplied between minimum value (inclusive) and maximum valuee exclusive.
* @param minimumValue The minimum value to use.
* @return The object on which to configure the maximum value.
*/
public And between(double minimumValue){
this.minimumValue = minimumValue;
return maximumValue -> {
this.range = maximumValue - minimumValue;
if(range < 0D){
throw new IllegalStateException("Invalid range of values defined.");
}
return this;
};
}
/**
* Get an instance of the DoubleSupplierBuilder created with reasonable defaults for further configuration.
* @return an instance of DoubleSupplierBuilder.
*/
public static DoubleSupplierBuilder doubles(){
return new DoubleSupplierBuilder();
}
/**
* Get an instance of the supplier based on the configuration and reasonable defaults.
* @param configuration The configuration to use.
* @return A configured StreamableSupplier of Doubles.
*/
public static StreamableSupplier doubles(Consumer configuration){
return buildConfig(doubles(), configuration);
}
}