Class LineWrapper


  • public class LineWrapper
    extends java.lang.Object
    A LineWrapper breaks long strings into multiple lines. It is intended for use with fixed-width code-like text rather than natural language text, although it can be used with any string.
    Since:
    3.0
    Author:
    Chris Jennings
    • Constructor Summary

      Constructors 
      Constructor Description
      LineWrapper()
      Creates a line wrapper with a default configuration.
      LineWrapper​(java.lang.String breakText, int firstWrap, int afterWrap, int tolerance)
      Creates a new line wrapper with the specified wrapper behaviour.
    • Constructor Detail

      • LineWrapper

        public LineWrapper()
        Creates a line wrapper with a default configuration. This is equivalent to LineWrapper( null, 78, 78, 4 ).
      • LineWrapper

        public LineWrapper​(java.lang.String breakText,
                           int firstWrap,
                           int afterWrap,
                           int tolerance)
        Creates a new line wrapper with the specified wrapper behaviour.
        Parameters:
        breakText - the text to insert when breaking a line; if null then a newline character (\n) is used
        firstWrap - the optimal length for the first line
        afterWrap - the optimal length for lines after the first
        tolerance - the amount by which the remaining text must exceed the optimal length before a break is inserted (this allows you to avoid breaking a line when only tolerance or fewer characters would appear on the following line)
    • Method Detail

      • getBreakText

        public java.lang.String getBreakText()
        Returns the break text.
        Returns:
        the text inserted between wrapped lines
      • setBreakText

        public void setBreakText​(java.lang.String breakText)
        Sets the break text.
        Parameters:
        breakText - the non-null text inserted between wrapped lines
      • getFirstLineWrapLength

        public int getFirstLineWrapLength()
        Returns the line wrap length for the first line.
        Returns:
        the ideal line length
      • setFirstLineWrapLength

        public void setFirstLineWrapLength​(int firstWrap)
        Sets the line wrap length for the first line.
        Parameters:
        firstWrap - the positive ideal line length
      • getFollowingLineWrapLength

        public int getFollowingLineWrapLength()
        Returns the line wrap length for lines after the first.
        Returns:
        the ideal line length
      • setFollowingLineWrapLength

        public void setFollowingLineWrapLength​(int afterWrap)
      • getWrapLengthTolerance

        public int getWrapLengthTolerance()
      • setWrapLengthTolerance

        public void setWrapLengthTolerance​(int tolerance)
      • wrap

        public java.lang.String wrap​(java.lang.String s)
        Breaks a string into multiple lines, if necessary, using the current line-wrapping parameters. If the string needs no breaking, the original string is returned. Otherwise, a copy of the string is returned that has the current break text inserted at the selected break points.
        Parameters:
        s - the string to wrap into lines
        Returns:
        the wrapped string