Interface Writer<T>

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Writer<T>
XML writer interface, used for writing objects in XML format. The following XML will show the marshaled representation of an 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 -> ISeq.of(ch).map(g -> g.getAllele())) ) );
How to write the XML writing is shown by the next code snippet.
final IntegerChromosome ch = IntegerChromosome.of(MIN_VALUE, MAX_VALUE, 3); try (AutoCloseableXMLStreamWriter xml = XML.writer(out, indent)) { write(ch, xml); }
Since:
3.9
Version:
3.9
  • Method Summary

    Modifier and Type
    Method
    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 an 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 an XML element with the given name and writes the given children into it.
    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.
    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.
    default <B> Writer<B>
    map(Function<? super B,? extends T> mapper)
    Maps this writer to a different base type.
    static <T> Writer<T>
    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.
  • Method Details

    • write

      void write(XMLStreamWriter xml, T data) throws XMLStreamException
      Write the data of type T to the given XML stream writer.
      Parameters:
      xml - the underlying XMLStreamWriter, where the value is written to
      data - the value to write
      Throws:
      XMLStreamException - if writing the data fails
      NullPointerException - if one of the arguments is null
    • map

      default <B> Writer<B> map(Function<? super B,? extends T> mapper)
      Maps this writer to a different base type. Mapping to a different data type is necessary when you are going to write sub-objects of your basic data type T. E.g. the chromosome length or the min and max value of an IntegerChromosome.
      Type Parameters:
      B - the new data type of returned writer
      Parameters:
      mapper - the mapper function
      Returns:
      a writer with changed type
    • attr

      static <T> Writer<T> attr(String name)
      Writes the attribute with the given name to the current outer element.
      final Writer<String> writer1 = elem("element", attr("attribute"));
      Type Parameters:
      T - the writer base type
      Parameters:
      name - the attribute name
      Returns:
      a new writer instance
      Throws:
      NullPointerException - if the attribute name is null
      See Also:
    • attr

      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.
      final Writer<MyObject> = elem("element", attr("version", "1.0"));
      Type Parameters:
      T - the writer base type
      Parameters:
      name - the attribute name
      value - the attribute value
      Returns:
      a new writer instance
      Throws:
      NullPointerException - if one of the name is null
    • elem

      @SafeVarargs static <T> Writer<T> elem(String name, Writer<? super T>... children)
      Create a new Writer, which writes an XML element with the given name and writes the given children into it.
      Type Parameters:
      T - the writer base type
      Parameters:
      name - the root element name
      children - the XML child elements
      Returns:
      a new writer instance
      Throws:
      NullPointerException - if one of the arguments is null
    • text

      static <T> Writer<T> text()
      Create a new text Writer, which writes the given data as string to the outer element.
      Type Parameters:
      T - the data type, which is written as string to the outer element
      Returns:
      a new text writer
    • elems

      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.
      Type Parameters:
      T - the writer base type
      Parameters:
      name - the enclosing element name used for each data value
      writer - the sub-element writer
      Returns:
      a new writer instance
      Throws:
      NullPointerException - if one of the arguments is null
    • elems

      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.
      Type Parameters:
      T - the writer base type
      Parameters:
      writer - the sub-element writer
      Returns:
      a new writer instance
      Throws:
      NullPointerException - if one of the arguments is null
    • doc

      static <T> Writer<T> doc(Writer<? super T> writer)
      Adds an XML prolog element written by the given writer. The default values for encoding and version are set to "UTF-8" and "1.0", respectively.
       
       <?xml version="1.0" encoding="UTF-8"?>
       
      Type Parameters:
      T - the writer data type
      Parameters:
      writer - the root element writer
      Returns:
      a new writer instance