@FunctionalInterface public interface Writer<T>
IntegerChromosome
.
<int-chromosome length="3">
<min>-2147483648</min>
<max>2147483647</max>
<alleles>
<allele>-1878762439</allele>
<allele>-957346595</allele>
<allele>-88668137</allele>
</alleles>
</int-chromosome>
The XML has been written by the following Writer
definition.
final Writer<IntegerChromosome> writer =
elem("int-chromosome",
attr("length").map(ch -> ch.length()),
elem("min", Writer.<Integer>text().map(ch -> ch.getMin())),
elem("max", Writer.<Integer>text().map(ch -> ch.getMax())),
elem("alleles",
elems("allele", Writer.<Integer>text())
.map(ch -> ch.toSeq().map(g -> g.getAllele()))
)
);
final IntegerChromosome ch = IntegerChromosome.of(MIN_VALUE, MAX_VALUE, 3);
try (AutoCloseableXMLStreamWriter xml = XML.writer(out, indent)) {
write(ch, xml);
}
Modifier and Type | Method and Description |
---|---|
static <T> Writer<T> |
attr(String name)
Writes the attribute with the given
name to the current
outer element. |
static <T> Writer<T> |
attr(String name,
Object value)
Writes the attribute with the given
name and a constant
value to the current outer element. |
static <T> Writer<T> |
doc(Writer<? super T> writer)
Adds a XML prolog element written by the given
writer . |
static <T> Writer<T> |
elem(String name,
Writer<? super T>... children)
Create a new
Writer , which writes a XML element with the given
name and writes the given children into it. |
static <T> Writer<Iterable<T>> |
elems(String name,
Writer<? super T> writer)
Creates a new
Writer , which writes the given children as
sub-elements, defined by the given childWriter . |
static <T> Writer<Iterable<T>> |
elems(Writer<? super T> writer)
Creates a new
Writer , which writes the given children as
sub-elements, defined by the given childWriter . |
default <B> Writer<B> |
map(Function<? super B,? extends T> mapper)
Maps this writer to a different base type.
|
static <T> Writer<T> |
text()
Create a new text
Writer , which writes the given data as string
to the outer element. |
void |
write(XMLStreamWriter xml,
T data)
Write the data of type
T to the given XML stream writer. |
void write(XMLStreamWriter xml, T data) throws XMLStreamException
T
to the given XML stream writer.xml
- the underlying XMLStreamWriter
, where the value is
written todata
- the value to writeXMLStreamException
- if writing the data failsNullPointerException
- if one of the arguments is null
default <B> Writer<B> map(Function<? super B,? extends T> mapper)
T
. E.g. the chromosome length or the
min
and max
value of an IntegerChromosome
.B
- the new data type of returned writermapper
- the mapper functionstatic <T> Writer<T> attr(String name)
name
to the current
outer element.
final Writer<String> writer1 = elem("element", attr("attribute"));
T
- the writer base typename
- the attribute nameNullPointerException
- if the attribute name
is null
attr(String, Object)
static <T> Writer<T> attr(String name, Object value)
name
and a constant
value
to the current outer element.
final Writer<MyObject> = elem("element", attr("version", "1.0"));
T
- the writer base typename
- the attribute namevalue
- the attribute valueNullPointerException
- if one of the name
is null
@SafeVarargs static <T> Writer<T> elem(String name, Writer<? super T>... children)
Writer
, which writes a XML element with the given
name and writes the given children into it.T
- the writer base typename
- the root element namechildren
- the XML child elementsNullPointerException
- if one of the arguments is null
static <T> Writer<T> text()
Writer
, which writes the given data as string
to the outer element.T
- the data type, which is written as string to the outer elementstatic <T> Writer<Iterable<T>> elems(String name, Writer<? super T> writer)
Writer
, which writes the given children
as
sub-elements, defined by the given childWriter
.T
- the writer base typename
- the enclosing element name used for each data valuewriter
- the sub-element writerNullPointerException
- if one of the arguments is null
static <T> Writer<Iterable<T>> elems(Writer<? super T> writer)
Writer
, which writes the given children
as
sub-elements, defined by the given childWriter
.T
- the writer base typewriter
- the sub-element writerNullPointerException
- if one of the arguments is null
static <T> Writer<T> doc(Writer<? super T> writer)
writer
. The default
values for encoding and version is set to "UTF-8" and "1.0", respectively.
<?xml version="1.0" encoding="UTF-8"?>
T
- the writer data typewriter
- the root element writer© 2007-2017 Franz Wilhelmstötter (2017-11-16 20:01)