Package guru.mikelue.jdut.datagrain
Class DataRow.Builder
- java.lang.Object
-
- guru.mikelue.jdut.datagrain.DataRow.Builder
-
- Enclosing class:
- DataRow
public class DataRow.Builder extends Object
Used withDataRow.build(Consumer<DataRow.Builder>).
Infinite recursion while using
In following code snippet, the builder will set-up a field which cause infinite recursion while getting data:Supplierfor value of field
Since the lazy evaluation of lambda expression, the new lambda expression of "ct_1" would be a self-reference to builder.builder -> builder.fieldOfValueSupplier( "ct_1", () -> 30 + builder.getDataSupplier("ct_1").get().get() )
Instead, you should keep "the old instance of lambda expression":builder -> { final Supplier<Integer> oldSupplier = builder.getDataSupplier("ct_1").get(); builder.fieldOfValueSupplier("ct_1", () -> 30 + oldSupplier.get()); }
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DataRow.Builderdata(Map<String,DataField<?>> newData)Sets data of row.<T> DataRow.BuilderfieldOfValue(String columnName, T value)Sets value of a field.<T> DataRow.BuilderfieldOfValueSupplier(String columnName, Supplier<T> valueSupplier)Sets value supplier of a field.<T> Optional<T>getData(String columnName)Gets value of data.<T> Optional<DataField<T>>getDataField(String columnName)Gets field of data.<T> Optional<Supplier<T>>getDataSupplier(String columnName)Gets supplier of data.Stream<DataField<?>>getStreamOfFields()Gets the fields asStream.SchemaTablegetTable()Gets definition of table.StringgetTableName()Gets name of table.booleangetValidated()Whether the data row is validated or not.DataRow.BuildertableSchema(SchemaTable newTableSchema)Sets the table schema.voidvalidate()Validates this row.
-
-
-
Method Detail
-
tableSchema
public DataRow.Builder tableSchema(SchemaTable newTableSchema)
Sets the table schema.- Parameters:
newTableSchema- The table schema- Returns:
- cascading self
-
data
public DataRow.Builder data(Map<String,DataField<?>> newData)
Sets data of row.- Parameters:
newData- the data of row- Returns:
- cascading self
-
fieldOfValue
public <T> DataRow.Builder fieldOfValue(String columnName, T value)
Sets value of a field.- Type Parameters:
T- The type of value- Parameters:
columnName- The name of columnvalue- The value instance- Returns:
- cascading self
-
fieldOfValueSupplier
public <T> DataRow.Builder fieldOfValueSupplier(String columnName, Supplier<T> valueSupplier)
Sets value supplier of a field.- Type Parameters:
T- The type of value- Parameters:
columnName- The name of columnvalueSupplier- The value instance- Returns:
- cascading self
-
getValidated
public boolean getValidated()
Whether the data row is validated or not.- Returns:
- true if validated
-
validate
public void validate() throws DataRowExceptionValidates this row.- Throws:
DataRowException- if there is error in data or schema
-
getTableName
public String getTableName()
Gets name of table.- Returns:
- the name of table
- See Also:
getTable()
-
getTable
public SchemaTable getTable()
Gets definition of table.- Returns:
- definition of table
- See Also:
getTableName()
-
getData
public <T> Optional<T> getData(String columnName)
Gets value of data.- Type Parameters:
T- The type of data- Parameters:
columnName- The name of column- Returns:
- empty option if the column is not existing or the value is empty
-
getDataSupplier
public <T> Optional<Supplier<T>> getDataSupplier(String columnName)
Gets supplier of data.
Note: be careful of lazy building, which may cause infinite recursion
Following code will cause infinite recursion
Instead, you should:Supplier<Integer> wrappedSupplier = () -> 20 + builder.<Integer>getDataSupplier("ct_1").get().get(); builder.fieldOfValueSupplier("col_1", wrappedSupplier);// Keep the reference of supplier in current lambda of builder Supplier<Integer> sourceSupplier = builder.<Integer>getDataSupplier("ct_1").get(); Supplier<Integer> wrappedSupplier = () -> 20 + sourceSupplier.get(); builder.fieldOfValueSupplier("col_1", wrappedSupplier);- Type Parameters:
T- The type of data for the supplier- Parameters:
columnName- The name of column- Returns:
- empty option if the column is not existing or the value supplier is empty
-
getDataField
public <T> Optional<DataField<T>> getDataField(String columnName)
Gets field of data.- Type Parameters:
T- The data type of field- Parameters:
columnName- The name of column- Returns:
- empty option if the column is not existing
-
-