Package net.tangly.gleam.model
Record Class TsvEntity<T>
java.lang.Object
java.lang.Record
net.tangly.gleam.model.TsvEntity<T>
- Type Parameters:
T
- class of the entity- Record Components:
clazz
- class of the entityfields
- fields of the entity with the mapping rulesimports
- import factory function to create a new instance of the class with values of the CSV recordexports
- export factory method to create a CSV record for an instance of the clas
public record TsvEntity<T>(Class<T> clazz, List<TsvProperty<T,?>> fields, Function<org.apache.commons.csv.CSVRecord,T> imports, BiConsumer<T,org.apache.commons.csv.CSVPrinter> exports)
extends Record
Defines an entity mapped to a row of TSV values written to a TSV file.
A special case is if the entity is owned through one to multiple relation. The reference to the owner is stored in the TSV file. The extraction process returns a tuple with the the entity and the identifier of the owner.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionTsvEntity
(Class<T> clazz, List<TsvProperty<T, ?>> fields, Function<org.apache.commons.csv.CSVRecord, T> imports, BiConsumer<T, org.apache.commons.csv.CSVPrinter> exports) Creates an instance of aTsvEntity
record class. -
Method Summary
Modifier and TypeMethodDescriptionclazz()
Returns the value of theclazz
record component.final boolean
Indicates whether some other object is "equal to" this one.void
exportRelation
(@NotNull TsvRelation<T> relation, @NotNull org.apache.commons.csv.CSVPrinter printer) Export the relation to the TSV file.BiConsumer
<T, org.apache.commons.csv.CSVPrinter> exports()
Returns the value of theexports
record component.void
Export an entity as a row in a TSV file.List
<TsvProperty<T, ?>> fields()
Returns the value of thefields
record component.static String
final int
hashCode()
Returns a hash code value for this object.headers()
Return the ordered list of column header names for the TSV record header.importRelation
(@NotNull org.apache.commons.csv.CSVRecord record) imports()
Returns the value of theimports
record component.imports
(@NotNull org.apache.commons.csv.CSVRecord record) Import an entity from a TSV record.static <T> TsvEntity
<T> of
(Class<T> clazz, List<TsvProperty<T, ?>> properties, Function<org.apache.commons.csv.CSVRecord, T> imports) Defines an entity for a class with a set of property and a function creating an instance of the class with values from the CSV record.static <T> TsvEntity
<T> of
(Class<T> clazz, List<TsvProperty<T, ?>> properties, Supplier<T> factory) Defines an entity with a default constructor and no special mapping rules to CSV record.final String
toString()
Returns a string representation of this record class.
-
Field Details
-
OWNER_FOID
- See Also:
-
-
Constructor Details
-
TsvEntity
public TsvEntity(Class<T> clazz, List<TsvProperty<T, ?>> fields, Function<org.apache.commons.csv.CSVRecord, T> imports, BiConsumer<T, org.apache.commons.csv.CSVPrinter> exports) Creates an instance of aTsvEntity
record class.
-
-
Method Details
-
of
public static <T> TsvEntity<T> of(Class<T> clazz, List<TsvProperty<T, ?>> properties, Supplier<T> factory) Defines an entity with a default constructor and no special mapping rules to CSV record. The class is a standard Java POJO.- Type Parameters:
T
- type of the class to import from or export to a TSV file.- Parameters:
clazz
- class of the entityproperties
- properties of the entityfactory
- default constructor method of the class- Returns:
- a new instance of the class
-
of
public static <T> TsvEntity<T> of(Class<T> clazz, List<TsvProperty<T, ?>> properties, Function<org.apache.commons.csv.CSVRecord, T> imports) Defines an entity for a class with a set of property and a function creating an instance of the class with values from the CSV record. This approach is often used for records. Records only provides constructors with all properties as parameters. The second case is when the class constructor takes internal or external identifier as parameters.- Type Parameters:
T
- type of the class to import from or export to a TSV file.- Parameters:
clazz
- class of the entityproperties
- properties of the entityimports
- factory method to create an instance of the class with some of the CSV record values.- Returns:
- a new instance of the class
-
get
-
headers
-
exports
Export an entity as a row in a TSV file. Upon completion of the export, the caller shall call. If defined, the export lambda is executed; otherwise the exports lambdas for all fields are executed. The exclusive operation modes are due to the constraints of the underlying CSV Apache Commons library.- Parameters:
entity
- entity to be exported as TSV record. The value can be nullprinter
- printer to write TSV record
-
exportRelation
public void exportRelation(@NotNull @NotNull TsvRelation<T> relation, @NotNull @NotNull org.apache.commons.csv.CSVPrinter printer) Export the relation to the TSV file. First, the owner identifier is written in the first column having the headerOWNER_FOID
. Second. the referenced entity is written to the TSV record.- Parameters:
relation
- relation to exportprinter
- target TSV file
-
imports
Import an entity from a TSV record. First if defined the imports lambda is executed otherwise the factory is used to create a new Java domain object. When all fields with a defined setter will have their imports lambda executed.- Parameters:
record
- TSV record containing the TSV representation of the entity- Returns:
- entity base on the TSV record
-
importRelation
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object)
. -
clazz
-
fields
-
imports
-
exports
-