public final class DataSetBuilder extends Object
Objects of this kind provide a convenient manner to define data sets programmatically.
Data builders can be used as follows:
JDBDT.builder(DataSource)
,
to create a fresh data set, or to DataSet.build()
to add data to an existing data set.
ColumnFiller
instance) per each column in the table,
using the set(String, ColumnFiller)
base method or one of several convenience methods e.g.,
the ones available for sequence or pseudo-random values.
generate(int)
causes a specified number of rows
to be added to the underlying data set, in line with the current
column fillers set.
data()
.
JDBDT.builder(DataSource)
,
DataSet.build()
Modifier and Type | Field and Description |
---|---|
static long |
MILLIS_PER_DAY
Milliseconds per day.
|
Constructor and Description |
---|
DataSetBuilder(DataSet data)
Constructs a new data set builder backed by an existing data set.
|
DataSetBuilder(DataSource source)
Constructs a new data set builder for the given data source.
|
Modifier and Type | Method and Description |
---|---|
DataSetBuilder |
allColumnsNull()
Set the NULL value filler for all columns.
|
DataSet |
data()
Get data set this builder associates to.
|
DataSetBuilder |
generate(int count)
Associate a number of rows to the data set, according to
the current column fillers' configuration.
|
DataSetBuilder |
nullValue(String column)
Set the NULL value filler for a column.
|
DataSetBuilder |
random(String column,
Date min,
Date max)
Set random filler using
Date values. |
DataSetBuilder |
random(String column,
double min,
double max)
Set random filler using
double values. |
DataSetBuilder |
random(String column,
float min,
float max)
Set random filler using
float values. |
DataSetBuilder |
random(String column,
Function<Random,?> gen)
Set customized random filler.
|
DataSetBuilder |
random(String column,
int min,
int max)
Set random filler using
int values. |
DataSetBuilder |
random(String column,
List<?> values)
Set random filler for column using a list of values.
|
DataSetBuilder |
random(String column,
long min,
long max)
Set random filler using
long values. |
<T> DataSetBuilder |
random(String column,
T... values)
Set random filler for column using an array of values.
|
DataSetBuilder |
random(String column,
Timestamp min,
Timestamp max)
Set random filler using
Timestamp values. |
DataSetBuilder |
random(String column,
Time min,
Time max)
Set random filler using
Time values. |
DataSetBuilder |
remainingColumnsNull()
Set the NULL value filler for all remaining columns.
|
void |
reset()
Disable all previous column filler settings.
|
DataSetBuilder |
sequence(String column,
BigInteger initial)
Set
BigInteger sequence filler for column. |
DataSetBuilder |
sequence(String column,
BigInteger initial,
BigInteger step)
Set
BigInteger sequence filler for column
with a specified step. |
DataSetBuilder |
sequence(String column,
Date initial,
int step)
Set
Date sequence filler for column
with a specified step in days. |
DataSetBuilder |
sequence(String column,
double initial,
double step)
Set
double sequence filler for column
with a specified step. |
DataSetBuilder |
sequence(String column,
float initial,
float step)
Set
float sequence filler for column
with a specified step. |
DataSetBuilder |
sequence(String column,
int initial)
Set
int value sequence filler for column. |
DataSetBuilder |
sequence(String column,
IntFunction<?> step)
Set sequence filler using a index-based step-function.
|
DataSetBuilder |
sequence(String column,
IntFunction<?> step,
int initial)
Set sequence filler using a index-based step-function.
|
DataSetBuilder |
sequence(String column,
int initial,
int step)
Set
int sequence filler for column
with a specified step. |
DataSetBuilder |
sequence(String column,
List<?> values)
Set sequence filler using a list of values.
|
DataSetBuilder |
sequence(String column,
long initial)
Set
long value sequence filler for column. |
DataSetBuilder |
sequence(String column,
long initial,
long step)
Set
long sequence filler for column
with a specified step. |
<T> DataSetBuilder |
sequence(String column,
T... values)
Set sequence filler using array values.
|
DataSetBuilder |
sequence(String column,
Time initial,
int step)
Set
Time sequence filler for column
with a specified step in seconds. |
DataSetBuilder |
sequence(String column,
Timestamp initial,
long step)
Set
Timestamp sequence filler for column
with a specified step in milliseconds. |
<T> DataSetBuilder |
sequence(String column,
T initial,
UnaryOperator<T> step)
Set a sequence filler using a step-function.
|
DataSetBuilder |
set(String column,
ColumnFiller<?> filler)
Set filler for column.
|
<E extends Enum<?>> |
value(String column,
E enumConstant)
Set a constant enum value for a column, converting the enumeration constant to a string.
|
<T> DataSetBuilder |
value(String column,
T constant)
Set a constant value filler for a column.
|
public static final long MILLIS_PER_DAY
public DataSetBuilder(DataSource source)
The builder will be backed by a fresh data set for the given source.
source
- Data source.public DataSetBuilder(DataSet data)
New rows generated using the builder will be added to the given data set. without clearing previously existing rows in it.
data
- Data set associated to builder.public DataSet data()
public DataSetBuilder generate(int count)
count
- Number of rows (a positive integer)ColumnFillerException
- if there is an error evaluating
a column filler.InvalidOperationException
- for an invalid row count, or if
there are columns with no associated fillers.public DataSetBuilder set(String column, ColumnFiller<?> filler)
column
- Column name.filler
- Column filler.public void reset()
After a call to this method, no column will have an associated filler.
public DataSetBuilder nullValue(String column)
column
- Column name.value(String, Object)
public DataSetBuilder remainingColumnsNull()
A call to this method sets the nullValue(java.lang.String)
filler
for all columns without an associated filler.
nullValue(String)
,
allColumnsNull()
public DataSetBuilder allColumnsNull()
A call to this method sets the nullValue(java.lang.String)
filler
for all columns.
nullValue(String)
,
remainingColumnsNull()
public <T> DataSetBuilder value(String column, T constant)
T
- Type of object.column
- Column name.constant
- Value of the constant .public <E extends Enum<?>> DataSetBuilder value(String column, E enumConstant)
A call to this method is shorthand for value(column, enumConstant.toString())
.
E
- Enumeration type.column
- Column name.enumConstant
- Value of the constant.public <T> DataSetBuilder sequence(String column, T initial, UnaryOperator<T> step)
The sequence of values generated by the filler starts
with the specified initial value, and subsequent values
are generated using the step function which takes as input the previous value.
The sequence will then be
s(0), s(1), ...
where s(0) = initial
and s(n+1) = step.apply(s(n))
for
all n >= 0
.
T
- Column datum type.column
- Column name.initial
- Initial value.step
- Step function.sequence(String, IntFunction)
public DataSetBuilder sequence(String column, IntFunction<?> step)
A call to this method is equivalent to
sequence(column, step, 0)
.
column
- Column name.step
- Step function.sequence(String, IntFunction, int)
public DataSetBuilder sequence(String column, IntFunction<?> step, int initial)
The sequence of values generated by the filler starts
with the specified initial value, and subsequent values
are generated using the step function which takes as input the
index of the row being generated, starting from the initial value.
The sequence will then be
s(start), s(start+1), ...
where s(i) = step.apply(i)
for all i >= start
.
column
- Column name.step
- Step function.initial
- Initial value fed to step function.sequence(String, Object, UnaryOperator)
@SafeVarargs public final <T> DataSetBuilder sequence(String column, T... values)
A call to this method is shorthand for
sequence(column, i -> values[i % values.length])
.
T
- Type of data.column
- Column name.values
- Sequence of values to use.sequence(String, List)
,
sequence(String,IntFunction)
public DataSetBuilder sequence(String column, List<?> values)
A call to this method is shorthand for
sequence(column, i -> values.get(i % values.size()))
.
column
- Column name.values
- Sequence of values to use.sequence(String,IntFunction)
public DataSetBuilder sequence(String column, int initial)
int
value sequence filler for column.
A call to this method is shorthand for
sequence(column, initial, 1)
.
column
- Column name.initial
- Initial sequence value.sequence(String, int, int)
public DataSetBuilder sequence(String column, int initial, int step)
int
sequence filler for column
with a specified step.
A call to this method is shorthand for
sequence(column, initial, n -> n + step)
.
column
- Column name.initial
- Initial sequence value.step
- Sequence step.sequence(String, Object, UnaryOperator)
,
sequence(String, int)
public DataSetBuilder sequence(String column, long initial)
long
value sequence filler for column.
A call to this method is shorthand for
sequence(column, initial, 1L)
.
column
- Column name.initial
- Initial sequence value.sequence(String, Object, UnaryOperator)
,
sequence(String, long, long)
public DataSetBuilder sequence(String column, long initial, long step)
long
sequence filler for column
with a specified step.
A call to this method is shorthand for
sequence(column, initial, n -> n + step)
.
column
- Column name.initial
- Initial sequence value.step
- Sequence step.sequence(String, Object, UnaryOperator)
,
sequence(String, long)
public DataSetBuilder sequence(String column, BigInteger initial)
BigInteger
sequence filler for column.
A call to this method is shorthand for
sequence(column, initial, BigInteger.ONE)
.
column
- Column name.initial
- Initial sequence value.sequence(String, Object, UnaryOperator)
,
sequence(String, BigInteger, BigInteger)
public DataSetBuilder sequence(String column, BigInteger initial, BigInteger step)
BigInteger
sequence filler for column
with a specified step.
A call to this method is shorthand for
sequence(column, initial, n -> n.add(step))
.
column
- Column name.initial
- Initial sequence value.step
- Sequence step.sequence(String, Object, UnaryOperator)
,
sequence(String, BigInteger)
public DataSetBuilder sequence(String column, float initial, float step)
float
sequence filler for column
with a specified step.
A call to this method is shorthand for
sequence(column, initial, x -> x + step)
.
column
- Column name.initial
- Initial sequence value.step
- Sequence step.sequence(String, Object, UnaryOperator)
,
sequence(String, double, double)
public DataSetBuilder sequence(String column, double initial, double step)
double
sequence filler for column
with a specified step.
A call to this method is shorthand for
sequence(column, initial, x -> x + step)
.
column
- Column name.initial
- Initial sequence value.step
- Sequence step.sequence(String, Object, UnaryOperator)
,
sequence(String, float, float)
public DataSetBuilder sequence(String column, Date initial, int step)
Date
sequence filler for column
with a specified step in days.
A call to this method is shorthand for
sequence(column, initial, d -> new Date(x.getTime() + step * MILLIS_PER_DAY))
.
column
- Column day.initial
- Initial date.step
- Step in days.sequence(String, Time, int)
,
sequence(String, Timestamp, long)
,
sequence(String, Object, UnaryOperator)
public DataSetBuilder sequence(String column, Time initial, int step)
Time
sequence filler for column
with a specified step in seconds.
A call to this method is shorthand for
sequence(column, initial, t -> new Time(t.getTime() + step * 1000L))
.
column
- Column day.initial
- Initial date.step
- Step in seconds.sequence(String, Date, int)
,
sequence(String, Timestamp, long)
,
sequence(String, Object, UnaryOperator)
public DataSetBuilder sequence(String column, Timestamp initial, long step)
Timestamp
sequence filler for column
with a specified step in milliseconds.
A call to this method is shorthand for
sequence(column, initial, ts -> new Timestamp(ts.getTime() + step))
.
column
- Column day.initial
- Initial date.step
- Step in milliseconds.sequence(String, Date, int)
,
sequence(String, Time, int)
,
sequence(String, Object, UnaryOperator)
@SafeVarargs public final <T> DataSetBuilder random(String column, T... values)
The specified column will be filled with values that are uniformly sampled from the given array.
T
- Type of data.column
- Column name.values
- Values to use.random(String, List)
,
sequence(String, Object...)
public DataSetBuilder random(String column, List<?> values)
The specified column will be filled with values that are uniformly sampled from the given list.
column
- Column name.values
- Values to use.random(String, Object...)
,
sequence(String, List)
public DataSetBuilder random(String column, int min, int max)
int
values.
The specified column will be filled with values that
are uniformly sampled from the interval [min,max]
.
column
- Column name.min
- Minimum value.max
- Maximum value.random(String, long, long)
,
random(String, float, float)
,
random(String, double, double)
public DataSetBuilder random(String column, long min, long max)
long
values.
The specified column will be filled with values that
are uniformly sampled from the interval [min,max]
.
column
- Column name.min
- Minimum value.max
- Maximum value.random(String, int, int)
,
random(String, float, float)
,
random(String, double, double)
public DataSetBuilder random(String column, float min, float max)
float
values.
The specified column will be filled with values that
are uniformly sampled from the interval [min,max]
.
column
- Column name.min
- Minimum value.max
- Maximum value.random(String, int, int)
,
random(String, long, long)
,
random(String, double, double)
public DataSetBuilder random(String column, double min, double max)
double
values.
The specified column will be filled with values that
are uniformly sampled from the interval [min,max]
.
column
- Column name.min
- Minimum value.max
- Maximum value.random(String, int, int)
,
random(String, long, long)
,
random(String, float, float)
public DataSetBuilder random(String column, Date min, Date max)
Date
values.
The specified column will be filled with values that
are uniformly sampled from the interval [min,max]
.
column
- Column name.min
- Minimum value.max
- Maximum value.random(String, Time, Time)
,
random(String, Timestamp, Timestamp)
public DataSetBuilder random(String column, Time min, Time max)
Time
values.
The specified column will be filled with values that
are uniformly sampled from the interval [min,max]
.
column
- Column name.min
- Minimum value.max
- Maximum value.random(String, Timestamp, Timestamp)
,
random(String, Date, Date)
public DataSetBuilder random(String column, Timestamp min, Timestamp max)
Timestamp
values.
The specified column will be filled with values that
are uniformly sampled from the interval [min,max]
.
column
- Column name.min
- Minimum value.max
- Maximum value.random(String, Time, Time)
,
random(String, Date, Date)
public DataSetBuilder random(String column, Function<Random,?> gen)
The specified column will be filled with values that are obtained
from a generator function. The generator function
takes a Random
instance as an argument and returns a value:
it should use the generator to produce column values in deterministic
fashion (in particular, the random number generator argument should not
be re-seeded).
Illustration of use
The filler below will yield strings
"ID_0", ..., "ID_9"
with an uniform
distribution:
DataSet ds = ...; ... ds.random("SomeColumn", rng -> "ID_" + rng.nextInt(10));
column
- Column name.gen
- Generator function.Random
,
random(String, int, int)
,
random(String, long, long)
,
random(String, float, float)
,
random(String, double, double)
,
random(String, Date, Date)
,
random(String, Time, Time)
,
random(String, Timestamp, Timestamp)
Copyright © 2016–2021 JDBDT. All rights reserved.