public abstract class Reader<T> extends Object
<int-chromosome length="3">
<min>-2147483648</min>
<max>2147483647</max>
<alleles>
<allele>-1878762439</allele>
<allele>-957346595</allele>
<allele>-88668137</allele>
</alleles>
</int-chromosome>
final Reader<IntegerChromosome> reader =
elem(
(Object[] v) -> {
final int length = (int)v[0];
final int min = (int)v[1];
final int max = (int)v[2];
final List<Integer> alleles = (List<Integer>)v[3];
assert alleles.size() == length;
return IntegerChromosome.of(
alleles.stream()
.map(value -> IntegerGene.of(value, min, max)
.toArray(IntegerGene[]::new)
);
},
"int-chromosome",
attr("length").map(Integer::parseInt),
elem("min", text().map(Integer::parseInt)),
elem("max", text().map(Integer::parseInt)),
elem("alleles",
elems(elem("allele", text().map(Integer::parseInt)))
)
);
Modifier and Type | Method and Description |
---|---|
static Reader<String> |
attr(String name)
Return a
Reader for reading an attribute of an element. |
static <T> Reader<T> |
elem(Function<Object[],T> generator,
String name,
Reader<?>... children)
Return a
Reader for reading an object of type T from the
XML element with the given name . |
static <T> Reader<T> |
elem(String name,
Reader<? extends T> reader)
Return a
Reader which reads the value from the child elements of
the given parent element name . |
static <T> Reader<List<T>> |
elems(Reader<? extends T> reader)
Return a
Reader which collects the elements, read by the given
child reader , and returns it as list of these elements. |
<B> Reader<B> |
map(Function<? super T,? extends B> mapper)
Create a new reader for the new mapped type
B . |
abstract T |
read(XMLStreamReader xml)
Read the given type from the underlying XML stream
reader . |
static Reader<String> |
text()
Return a
Reader for reading the text of an element. |
String |
toString() |
public abstract T read(XMLStreamReader xml) throws XMLStreamException
reader
.
try (AutoCloseableXMLStreamReader xml = XML.reader(in)) {
// Move XML stream to first element.
xml.next();
return reader.read(xml);
}
xml
- the underlying XML stream reader
null
XMLStreamException
- if an error occurs while reading the valueNullPointerException
- if the given xml
stream reader is
null
public <B> Reader<B> map(Function<? super T,? extends B> mapper)
B
.B
- the target type of the new readermapper
- the mapper functionNullPointerException
- if the given mapper
function is
null
public static Reader<String> attr(String name)
Reader
for reading an attribute of an element.
XML
<element length="3"/>
Reader definition
final Reader<Integer> reader =
elem(
v -> (Integer)v[0],
"element",
attr("length").map(Integer::parseInt)
);
name
- the attribute nameNullPointerException
- if the given name
is null
public static Reader<String> text()
Reader
for reading the text of an element.
XML
<element>1234<element>
Reader definition
final Reader<Integer> reader =
elem(
v -> (Integer)v[0],
"element",
text().map(Integer::parseInt)
);
public static <T> Reader<T> elem(Function<Object[],T> generator, String name, Reader<?>... children)
Reader
for reading an object of type T
from the
XML element with the given name
.
XML
<property name="size">1234<property>
Reader definition
final Reader<Property> reader =
elem(
v -> {
final String name = (String)v[0];
final Integer value = (Integer)v[1];
return Property.of(name, value);
},
"property",
attr("name"),
text().map(Integer::parseInt)
);
T
- the reader result typegenerator
- the generator function, which build the result object
from the given parameter arrayname
- the name of the root (sub-tree) elementchildren
- the child element reader, which creates the values
forwarded to the generator
functionNullPointerException
- if one of the given arguments is null
IllegalArgumentException
- if the given child readers contains more
than one text readerpublic static <T> Reader<T> elem(String name, Reader<? extends T> reader)
Reader
which reads the value from the child elements of
the given parent element name
.
XML
<min><property name="size">1234<property></min>
Reader definition
final Reader<Property> reader =
elem("min",
elem(
v -> {
final String name = (String)v[0];
final Integer value = (Integer)v[1];
return Property.of(name, value);
},
"property",
attr("name"),
text().map(Integer::parseInt)
)
);
T
- the result typename
- the parent element namereader
- the child elements readerNullPointerException
- if one of the given arguments is null
public static <T> Reader<List<T>> elems(Reader<? extends T> reader)
Reader
which collects the elements, read by the given
child reader
, and returns it as list of these elements.
XML
<properties length="3">
<property>-1878762439</property>
<property>-957346595</property>
<property>-88668137</property>
</properties>
Reader definition
Reader<List<Integer>> reader =
elem(
v -> (List<Integer>)v[0],
"properties",
elems(elem("property", text().map(Integer::parseInt)))
);
T
- the element typereader
- the child element reader© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)