Class Doc

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Doc.Alternatives
      Represents a choice between a flattened and expanded layout for a single Doc.
      static class  Doc.Append
      Represents the concatenation of two Docs.
      static class  Doc.CloseLink
      Represents the ANSI escape code sequence for closing a hyperlink.
      static class  Doc.Empty
      Represents an empty Doc.
      static class  Doc.Escape
      Represents an ANSI escape code sequence.
      static class  Doc.Indent
      Represents an indented Doc.
      static class  Doc.Line
      Represents a line break which cannot be flattened into a more compact layout.
      static class  Doc.LineOr
      Represents a line break which can be flattened into an alternative document altDoc.
      static class  Doc.LineOrEmpty
      Represents a line break which can be flattened into an empty document.
      static class  Doc.LineOrSpace
      Represents a line break which can be flattened into a single space character.
      static class  Doc.Link
      Represents a Doc that acts as a hyperlink to a given URI.
      static class  Doc.Margin
      Represents a Doc within which every new line is prefixed by a margin.
      static class  Doc.OpenLink
      Represents the ANSI escape code sequence for opening a hyperlink.
      static class  Doc.Param
      Represents a placeholder for a Doc that will be provided as a parameter.
      static class  Doc.Reset
      Represents the end of a Doc that is styled with an ANSI escape code sequence.
      static class  Doc.Styled
      Represents a Doc styled with ANSI escape codes.
      static class  Doc.Text
      Represents an atomic piece of text.
      static class  Doc.WrapText
      Represents a long text string which may be wrapped to fit within the preferred rendering width.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      Doc append​(Doc other)
      Append the other Doc to this one.
      Doc appendLine​(Doc other)
      Append the other Doc to this one, separated by a line break which cannot be flattened.
      Doc appendLineOr​(Doc altDoc, Doc other)
      Append the other Doc to this one, separated by a line break which may be flattened into the altDoc document.
      Doc appendLineOr​(String altText, Doc other)
      Append the other Doc to this one, separated by a line break which may be flattened into the altText String.
      Doc appendLineOrEmpty​(Doc other)
      Append the other Doc to this one, separated by a line break which may be flattened into an empty document.
      Doc appendLineOrSpace​(Doc other)
      Append the other Doc to this one, separated by a line break which may be flattened into a space character.
      Doc appendSpace​(Doc other)
      Append the other Doc to this one, separated by a space character.
      Doc bind​(Object... bindings)
      Bind named parameters to the name-to-Doc pairs provided via bindings.
      abstract Doc bind​(String name, Doc value)
      Bind a named parameter to the Doc provided via value.
      Doc bind​(String name, String value)
      Bind a named parameter to the String provided via value.
      abstract Doc bind​(Map<String,​Doc> bindings)
      Bind named parameters to the Docs provided via bindings.
      Doc bracket​(int indent, Doc left, Doc right)
      Bracket the current document by the left and right documents, indented by indent spaces.
      Doc bracket​(int indent, Doc lineDoc, Doc left, Doc right)
      Bracket the current document by the left and right documents, indented by indent spaces.
      Doc bracket​(int indent, Doc lineDoc, Doc marginDoc, Doc left, Doc right)
      Bracket the current document by the left and right documents, indented by indent spaces, applying the margin document margin.
      Doc bracket​(int indent, Doc lineDoc, String left, String right)
      Bracket the current document by the left and right Strings, indented by indent spaces.
      Doc bracket​(int indent, String left, String right)
      Bracket the current document by the left and right Strings, indented by indent spaces.
      static Doc empty()
      Creates an empty Doc.
      static Doc fold​(Collection<Doc> documents, BinaryOperator<Doc> fn)
      Reduce a collection of documents using the binary operator fn, returning an empty document if the collection is empty.
      static Doc fold​(Stream<Doc> documents, BinaryOperator<Doc> fn)
      Reduce a stream of documents using the binary operator fn, returning an empty document if the stream is empty.
      static Doc group​(Doc doc)
      Creates a Doc which represents a group that can be flattened into a more compact layout.
      Doc indent​(int indent)
      Indent the current Doc by indent spaces.
      static Doc indent​(int indent, Doc doc)
      Indent the input Doc by indent spaces.
      static Doc intersperse​(Doc separator, Collection<Doc> documents)
      Intersperse a separator document in between the elements of a collection of documents.
      static Doc intersperse​(Doc separator, Stream<Doc> documents)
      Intersperse a separator document in between the elements of a stream of documents.
      static Doc line()
      Creates a Doc representing a line break which cannot be flattened.
      static Doc lineOr​(Doc altDoc)
      Creates a Doc representing a line break which may be flattened into an alternative document altDoc.
      static Doc lineOr​(String altText)
      Creates a Doc representing a line break which may be flattened into the alternative text altText.
      static Doc lineOrEmpty()
      Creates a Doc representing a line break which may be flattened into an empty document.
      static Doc lineOrSpace()
      Creates a Doc representing a line break which may be flattened into a single space character.
      static Doc link​(Doc doc, URI uri)
      Converts the input Doc into a hyperlink to the uri provided.
      Doc link​(URI uri)
      Converts the current Doc into a hyperlink to the uri provided.
      Doc margin​(Doc margin)
      Apply the margin document margin to the current Doc, emitting the margin at the start of every new line from the start of this document until the end of the document.
      static Doc margin​(Doc margin, Doc doc)
      Apply the margin document margin to the current Doc, emitting the margin at the start of every new line from the start of this document until the end of the document.
      static Doc param​(String name)
      Creates a Doc which acts as a placeholder for an argument Doc that will be provided by binding parameters prior to rendering.
      String render()
      Renders the current Doc into a String, attempting to lay out the document according to the default rendering options.
      String render​(int width)
      Renders the current Doc into a String, attempting to lay out the document with at most width characters on each line.
      void render​(int width, Appendable output)
      Renders the current Doc into an Appendable, attempting to lay out the document with at most width characters on each line.
      static String render​(Doc doc)
      Renders the input Doc into a String, attempting to lay out the document according to the default rendering options.
      static String render​(Doc doc, RenderOptions options)
      Renders the input Doc into a String, attempting to lay out the document according to the rendering options.
      static void render​(Doc doc, RenderOptions options, Appendable output)
      Renders the input Doc into an Appendable, attempting to lay out the document according to the rendering options.
      static void render​(Doc doc, Appendable output)
      Renders the input Doc into an Appendable, attempting to lay out the document according to the default rendering options.
      String render​(RenderOptions options)
      Renders the current Doc into a String, attempting to lay out the document according to the rendering options.
      void render​(RenderOptions options, Appendable output)
      Renders the current Doc into an Appendable, attempting to lay out the document according to the rendering options.
      void render​(Appendable output)
      Renders the current Doc into an Appendable, attempting to lay out the document according to the default rendering options.
      Doc styled​(Styles.StylesOperator... styles)
      Styles the current Doc using the styles provided via styles.
      static Doc styled​(Doc doc, Styles.StylesOperator... styles)
      Styles the input Doc using the styles provided via styles.
      static Doc text​(String text)
      Construct a Doc from the text.
      static Doc wrapText​(String text)
      Construct a Doc which attempts to wrap the input text to fit within the preferred rendering width.
    • Method Detail

      • bind

        public abstract Doc bind​(String name,
                                 Doc value)
        Bind a named parameter to the Doc provided via value.
        Parameters:
        name - the name of the parameter.
        value - the value to use to replace the parameter placeholder.
        Returns:
        this Doc with all instances of the named parameter replaced by value.
      • bind

        public abstract Doc bind​(Map<String,​Doc> bindings)
        Bind named parameters to the Docs provided via bindings.
        Parameters:
        bindings - the bindings to use to replace named parameters.
        Returns:
        this Doc with all matching named parameters replaced by their corresponding values.
      • bind

        public Doc bind​(Object... bindings)
        Bind named parameters to the name-to-Doc pairs provided via bindings.
        Parameters:
        bindings - the bindings to use to replace named parameters.
        Returns:
        this Doc with all matching named parameters replaced by their corresponding values.
      • bind

        public Doc bind​(String name,
                        String value)
        Bind a named parameter to the String provided via value.
        Returns:
        this Doc with all instances of the named parameter replaced by value.
      • append

        public Doc append​(Doc other)
        Append the other Doc to this one.
        Parameters:
        other - the other document.
        Returns:
        the concatenated Doc.
      • appendSpace

        public Doc appendSpace​(Doc other)
        Append the other Doc to this one, separated by a space character.
        Parameters:
        other - the other document.
        Returns:
        the concatenated Doc.
      • appendLine

        public Doc appendLine​(Doc other)
        Append the other Doc to this one, separated by a line break which cannot be flattened.
        Parameters:
        other - the other document.
        Returns:
        the concatenated Doc.
      • appendLineOrSpace

        public Doc appendLineOrSpace​(Doc other)
        Append the other Doc to this one, separated by a line break which may be flattened into a space character.
        Parameters:
        other - the other document.
        Returns:
        the concatenated Doc.
      • appendLineOrEmpty

        public Doc appendLineOrEmpty​(Doc other)
        Append the other Doc to this one, separated by a line break which may be flattened into an empty document.
        Parameters:
        other - the other document.
        Returns:
        the concatenated Doc.
      • appendLineOr

        public Doc appendLineOr​(String altText,
                                Doc other)
        Append the other Doc to this one, separated by a line break which may be flattened into the altText String.
        Parameters:
        altText - the alternative text to display if the line break is flattened.
        other - the other document.
        Returns:
        the concatenated Doc.
      • appendLineOr

        public Doc appendLineOr​(Doc altDoc,
                                Doc other)
        Append the other Doc to this one, separated by a line break which may be flattened into the altDoc document.
        Parameters:
        altDoc - the alternative document to display if the line break is flattened.
        other - the other document.
        Returns:
        the concatenated Doc.
      • indent

        public Doc indent​(int indent)
        Indent the current Doc by indent spaces.
        Parameters:
        indent - the number of spaces of indent to apply.
        Returns:
        the indented document.
      • bracket

        public Doc bracket​(int indent,
                           String left,
                           String right)
        Bracket the current document by the left and right Strings, indented by indent spaces.

        When collapsed, line separators are replaced by spaces.

        Parameters:
        indent - the number of spaces of indent to apply.
        left - the left-hand bracket String.
        right - the right-hand bracket String.
        Returns:
        the bracketed document.
      • bracket

        public Doc bracket​(int indent,
                           Doc left,
                           Doc right)
        Bracket the current document by the left and right documents, indented by indent spaces.

        When collapsed, line separators are replaced by spaces.

        Parameters:
        indent - the number of spaces of indent to apply.
        left - the left-hand bracket document.
        right - the right-hand bracket document.
        Returns:
        the bracketed document.
      • bracket

        public Doc bracket​(int indent,
                           Doc lineDoc,
                           String left,
                           String right)
        Bracket the current document by the left and right Strings, indented by indent spaces.

        When collapsed, line separators are replaced by the lineDoc.

        Parameters:
        indent - the number of spaces of indent to apply.
        lineDoc - the line separator document.
        left - the left-hand bracket document.
        right - the right-hand bracket document.
        Returns:
        the bracketed document.
      • bracket

        public Doc bracket​(int indent,
                           Doc lineDoc,
                           Doc left,
                           Doc right)
        Bracket the current document by the left and right documents, indented by indent spaces.

        When collapsed, line separators are replaced by the lineDoc.

        Parameters:
        indent - the number of spaces of indent to apply.
        lineDoc - the line separator document.
        left - the left-hand bracket document.
        right - the right-hand bracket document.
        Returns:
        the bracketed document.
      • bracket

        public Doc bracket​(int indent,
                           Doc lineDoc,
                           Doc marginDoc,
                           Doc left,
                           Doc right)
        Bracket the current document by the left and right documents, indented by indent spaces, applying the margin document margin.

        When collapsed, line separators are replaced by the lineDoc.

        Parameters:
        indent - the number of spaces of indent to apply.
        lineDoc - the line separator document.
        marginDoc - the margin document.
        left - the left-hand bracket document.
        right - the right-hand bracket document.
        Returns:
        the bracketed document.
      • margin

        public Doc margin​(Doc margin)
        Apply the margin document margin to the current Doc, emitting the margin at the start of every new line from the start of this document until the end of the document.

        Note that line separators are forbidden inside the margin document.

        This is because each line separator causes the margin document to be produced.

        If the margin document in turn contained a line separator, rendering would never terminate.

        Parameters:
        margin - the margin document to apply at the start of every line.
        Returns:
        a document which prefixes every new line with the margin document.
        Throws:
        IllegalArgumentException - if the margin document contains line separators.
      • styled

        public final Doc styled​(Styles.StylesOperator... styles)
        Styles the current Doc using the styles provided via styles.
        Parameters:
        styles - the styles to use to decorate the input doc.
        Returns:
        a Doc decorated with the ANSI styles provided.
        See Also:
        Styles, Color
      • render

        public String render()
        Renders the current Doc into a String, attempting to lay out the document according to the default rendering options.
        Returns:
        the document laid out as a String.
      • render

        public String render​(RenderOptions options)
        Renders the current Doc into a String, attempting to lay out the document according to the rendering options.
        Parameters:
        options - the options to use for rendering.
        Returns:
        the document laid out as a String.
      • render

        public String render​(int width)
        Renders the current Doc into a String, attempting to lay out the document with at most width characters on each line.
        Parameters:
        width - the preferred maximum rendering width.
        Returns:
        the document laid out as a String.
      • render

        public void render​(int width,
                           Appendable output)
                    throws IOException
        Renders the current Doc into an Appendable, attempting to lay out the document with at most width characters on each line.
        Parameters:
        width - the preferred maximum rendering width.
        output - the output to render into.
        Throws:
        IOException - if the Appendable output throws when appended.
      • render

        public void render​(RenderOptions options,
                           Appendable output)
                    throws IOException
        Renders the current Doc into an Appendable, attempting to lay out the document according to the rendering options.
        Parameters:
        options - the options to use for rendering.
        output - the output to render into.
        Throws:
        IOException - if the Appendable output throws when appended.
      • text

        public static Doc text​(String text)
        Construct a Doc from the text.
        Parameters:
        text - the input String.
        Returns:
        a Doc representing that String.
      • wrapText

        public static Doc wrapText​(String text)
        Construct a Doc which attempts to wrap the input text to fit within the preferred rendering width.
        Parameters:
        text - the input String.
        Returns:
        a Doc representing that String.
      • indent

        public static Doc indent​(int indent,
                                 Doc doc)
        Indent the input Doc by indent spaces.
        Parameters:
        indent - the number of spaces of indent to apply.
        doc - the input document
        Returns:
        the indented document.
      • margin

        public static Doc margin​(Doc margin,
                                 Doc doc)
        Apply the margin document margin to the current Doc, emitting the margin at the start of every new line from the start of this document until the end of the document.

        Note that line separators are forbidden inside the margin document.

        This is because each line separator causes the margin document to be produced.

        If the margin document in turn contained a line separator, rendering would never terminate.

        Parameters:
        margin - the margin document to apply at the start of every line.
        doc - the input document.
        Returns:
        a document which prefixes every new line with the margin document.
        Throws:
        IllegalArgumentException - if the margin document contains line separators.
      • line

        public static Doc line()
        Creates a Doc representing a line break which cannot be flattened.
        Returns:
        a Doc representing a line break which cannot be flattened.
      • lineOrEmpty

        public static Doc lineOrEmpty()
        Creates a Doc representing a line break which may be flattened into an empty document.
        Returns:
        a Doc representing a line break which may be flattened into an empty document.
      • lineOrSpace

        public static Doc lineOrSpace()
        Creates a Doc representing a line break which may be flattened into a single space character.
        Returns:
        a Doc representing a line break which may be flattened into a single space character.
      • empty

        public static Doc empty()
        Creates an empty Doc.
        Returns:
        an empty Doc.
      • lineOr

        public static Doc lineOr​(Doc altDoc)
        Creates a Doc representing a line break which may be flattened into an alternative document altDoc.
        Parameters:
        altDoc - the alternative document to use if the line break is flattened.
        Returns:
        a Doc representing a line break which may be flattened into an alternative document altDoc.
      • lineOr

        public static Doc lineOr​(String altText)
        Creates a Doc representing a line break which may be flattened into the alternative text altText.
        Parameters:
        altText - the alternative text to use if the line break is flattened.
        Returns:
        a Doc representing a line break which may be flattened into the alternative text altText.
      • styled

        public static Doc styled​(Doc doc,
                                 Styles.StylesOperator... styles)
        Styles the input Doc using the styles provided via styles.
        Parameters:
        doc - the input document.
        styles - the styles to use to decorate the input doc.
        Returns:
        a Doc decorated with the ANSI styles provided.
        See Also:
        Styles, Color
      • param

        public static Doc param​(String name)
        Creates a Doc which acts as a placeholder for an argument Doc that will be provided by binding parameters prior to rendering.
        Parameters:
        name - the name of the parameter.
        Returns:
        a parameter Doc.
      • fold

        public static Doc fold​(Collection<Doc> documents,
                               BinaryOperator<Doc> fn)
        Reduce a collection of documents using the binary operator fn, returning an empty document if the collection is empty.
        Parameters:
        documents - the collection of documents.
        fn - the binary operator for combining documents.
        Returns:
        a document built by reducing the documents using the operator fn.
      • fold

        public static Doc fold​(Stream<Doc> documents,
                               BinaryOperator<Doc> fn)
        Reduce a stream of documents using the binary operator fn, returning an empty document if the stream is empty.
        Parameters:
        documents - the stream of documents.
        fn - the binary operator for combining documents.
        Returns:
        a document built by reducing the documents using the operator fn.
      • intersperse

        public static Doc intersperse​(Doc separator,
                                      Collection<Doc> documents)
        Intersperse a separator document in between the elements of a collection of documents.
        Parameters:
        separator - the separator document.
        documents - the collection of documents.
        Returns:
        a document containing the concatenation of documents separated by the separator.
      • intersperse

        public static Doc intersperse​(Doc separator,
                                      Stream<Doc> documents)
        Intersperse a separator document in between the elements of a stream of documents.
        Parameters:
        separator - the separator document.
        documents - the stream of documents.
        Returns:
        a document containing the concatenation of documents separated by the separator.
      • group

        public static Doc group​(Doc doc)
        Creates a Doc which represents a group that can be flattened into a more compact layout.
        Parameters:
        doc - the document which is declared as a group which may be flattened.
        Returns:
        a Doc which represents a group that can be flattened into a more compact layout.
      • render

        public static void render​(Doc doc,
                                  RenderOptions options,
                                  Appendable output)
                           throws IOException
        Renders the input Doc into an Appendable, attempting to lay out the document according to the rendering options.
        Parameters:
        doc - the document to be rendered.
        options - the options to use for rendering.
        output - the output to render into.
        Throws:
        IOException - if the Appendable output throws when appended.
      • render

        public static void render​(Doc doc,
                                  Appendable output)
                           throws IOException
        Renders the input Doc into an Appendable, attempting to lay out the document according to the default rendering options.
        Parameters:
        doc - the document to be rendered.
        output - the output to render into.
        Throws:
        IOException - if the Appendable output throws when appended.
      • render

        public static String render​(Doc doc,
                                    RenderOptions options)
        Renders the input Doc into a String, attempting to lay out the document according to the rendering options.
        Parameters:
        doc - the document to be rendered.
        options - the options to use for rendering.
        Returns:
        the document laid out as a String.
      • render

        public static String render​(Doc doc)
        Renders the input Doc into a String, attempting to lay out the document according to the default rendering options.
        Parameters:
        doc - the document to be rendered.
        Returns:
        the document laid out as a String.