Package io.kojan.xml
Class Entity<Type,Bean>
java.lang.Object
io.kojan.xml.Entity<Type,Bean>
- Type Parameters:
Type
- data type of entityBean
- type of bean associated with the entity
An entity type. Type of things about which the data should be stored.
When stored in XML form, an entity is represented by an XML element with a specified tag. Nested child elements represent entity attributes, entity relationships and possibly other custom entity properties.
An entity has an associated base data type, which may be immutable or mutable - the library code makes no assumptions about the mutability of the base type.
In addition to its main type, an entity also has a bean type, which is always mutable.
Conversion from beans to the base data type is done by supplied converter method, or by
implementing the Builder
interface. In the case where the main type is mutable, it is
acceptable for the main type and the bean type to be the same type.
- Author:
- Mikolaj Izdebski
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDeserializes entity from XML format.Obtains a converter function that converts entity bean to entity object.Obtains a factory used to create initial entity bean.Get entity properties, such as attributes, relationships and other custom properties.getTag()
Determines XML element tag name used to serialize the entity in XML form.static <Type,
Bean>
Entity<Type, Bean> of
(String tag, Factory<Bean> beanFactory, Converter<Bean, Type> converter, Property<Type, Bean, ?>... properties) Creates an entity using a converter method for converting entity beans to entity objects.Creates an entity using a bean class implementing theBuilder
interface.static <Type> Entity
<Type, Type> Creates an entity over a mutable data type that does not need conversion from bean type.readFromXML
(Reader reader) Deserializes entity from XML format, reading XML data from givenReader
.readFromXML
(Path path) Deserializes entity from XML format, reading XML data from file at givenPath
.Serializes entity into XML format.void
writeToXML
(Writer writer, Type object) Serializes entity into XML format, writing XML data to givenWriter
.void
writeToXML
(Path path, Type object) Serializes entity into XML format, writing XML data to file at givenPath
.
-
Constructor Details
-
Entity
public Entity(String tag, Factory<Bean> beanFactory, Converter<Bean, Type> converter, List<Property<Type, Bean, ?>> properties) Creates an entity.- Parameters:
tag
- XML element tag name used to serialize the property in XML form (seegetTag()
)beanFactory
- factory used to create initial entity beanconverter
- converter function that converts entity bean to entity objectproperties
- one or more entity properties
-
-
Method Details
-
of
@SafeVarargs public static <Type,Bean> Entity<Type,Bean> of(String tag, Factory<Bean> beanFactory, Converter<Bean, Type> converter, Property<Type, Bean, ?>... properties) Creates an entity using a converter method for converting entity beans to entity objects.- Type Parameters:
Type
- data type of entityBean
- type of bean associated with the entity- Parameters:
tag
- XML element tag name used to serialize the property in XML form (seegetTag()
)beanFactory
- factory used to create initial entity beanconverter
- converter function that converts entity bean to entity objectproperties
- one or more entity properties- Returns:
- created entity
-
of
@SafeVarargs public static <Type,Bean extends Builder<Type>> Entity<Type,Bean> of(String tag, Factory<Bean> beanFactory, Property<Type, Bean, ?>... properties) Creates an entity using a bean class implementing theBuilder
interface.- Type Parameters:
Type
- data type of entityBean
- type of bean associated with the entity- Parameters:
tag
- XML element tag name used to serialize the property in XML form (seegetTag()
)beanFactory
- factory used to create initial entity beanproperties
- one or more entity properties- Returns:
- created entity
-
ofMutable
@SafeVarargs public static <Type> Entity<Type,Type> ofMutable(String tag, Factory<Type> factory, Property<Type, Type, ?>... properties) Creates an entity over a mutable data type that does not need conversion from bean type.- Type Parameters:
Type
- mutable data type of entity- Parameters:
tag
- XML element tag name used to serialize the property in XML form (seegetTag()
)factory
- factory used to create initial entity objectproperties
- one or more entity properties- Returns:
- created entity
-
getTag
Determines XML element tag name used to serialize the entity in XML form.- Returns:
- XML element tag name
-
getBeanFactory
Obtains a factory used to create initial entity bean.- Returns:
- factory used to create initial entity bean
-
getBeanConverter
Obtains a converter function that converts entity bean to entity object.- Returns:
- converter converter function that converts entity bean to entity object
-
getProperties
Get entity properties, such as attributes, relationships and other custom properties.- Returns:
- unmodifiable list of properties
-
readFromXML
Deserializes entity from XML format, reading XML data from givenReader
.- Parameters:
reader
- the source to read XML data from- Returns:
- deserialized entity object
- Throws:
XMLException
- in case exception occurs during XML deserialization
-
readFromXML
Deserializes entity from XML format, reading XML data from file at givenPath
.- Parameters:
path
- path to the source file from which XML data is read- Returns:
- deserialized entity object
- Throws:
IOException
- in case I/O exception occurs when reading form the fileXMLException
- in case exception occurs during XML deserialization
-
fromXML
Deserializes entity from XML format.- Parameters:
xml
- serialized entity in XML format- Returns:
- deserialized entity object
- Throws:
XMLException
- in case exception occurs during XML deserialization
-
writeToXML
Serializes entity into XML format, writing XML data to givenWriter
.- Parameters:
writer
- the sink to write XML data toobject
- entity object to serialize- Throws:
XMLException
- in case exception occurs during XML serialization
-
writeToXML
Serializes entity into XML format, writing XML data to file at givenPath
.- Parameters:
path
- path to the sink file to write XML data toobject
- entity object to serialize- Throws:
IOException
- in case I/O exception occurs when writing to the fileXMLException
- in case exception occurs during XML serialization
-
toXML
Serializes entity into XML format.- Parameters:
object
- entity object to serialize- Returns:
- serialized entity in XML format
- Throws:
XMLException
- in case exception occurs during XML serialization
-