Class IndentationUtils


  • public class IndentationUtils
    extends java.lang.Object
    Indentation utilities.
    Author:
    Holger Eichelberger
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IndentationUtils()
      Prevents external instantiation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static int advanceIfNextIsNewLine​(java.lang.CharSequence text, int pos)
      Advance the given position if it is a new line.
      static boolean allLinesStartWith​(java.lang.String text, int indent)
      Returns whether all lines start with the given indent.
      static java.lang.String appendWithLastIndentation​(java.lang.String string, java.lang.String text, boolean skipLastIndent)
      Appends the with last indentation to string.
      static int countLineEnds​(java.lang.CharSequence string, int startPos)
      Returns the number of line ends considering returns and new lines in string after startPos.
      private static int insertIndentation​(java.lang.StringBuilder text, int pos, int indentation)
      Inserts an indentation of indentation whitespaces at pos into text.
      static java.lang.String insertIndentation​(java.lang.String content, int indentation, boolean skipFirst)
      Inserts indentation into content, i.e., indentation whitespaces at each line start.
      static boolean isIndentationChar​(char ch)
      Returns whether ch is an indentation character.
      static boolean isIndentationString​(java.lang.String text)
      Does the given text just consist of indentation characters?
      static boolean isLineEnd​(char ch)
      Returns whether ch is a line end character.
      private static boolean nextIs​(java.lang.CharSequence text, int pos, char ch)
      Returns whether the next character in text after pos is ch.
      static java.lang.String removeIndentation​(java.lang.String content, int indentation, int tabEmu)
      Remove indentation in content, i.e., all whitespace/tab combinations matching the given indentation following a line start.
      static java.lang.String removeLastIndentation​(java.lang.String text)
      Removes the last indentation in text if present.
      private static int skipWhitespaces​(java.lang.StringBuilder text, int pos)
      Skips the whitespaces and tabs at pos in text.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IndentationUtils

        private IndentationUtils()
        Prevents external instantiation.
    • Method Detail

      • removeIndentation

        public static java.lang.String removeIndentation​(java.lang.String content,
                                                         int indentation,
                                                         int tabEmu)
        Remove indentation in content, i.e., all whitespace/tab combinations matching the given indentation following a line start.
        Parameters:
        content - the text to operate on
        indentation - the actual indentation in number of whitespaces (positive number)
        tabEmu - the tab emulation in whitespaces (0 if disabled, positive number else)
        Returns:
        content without indentation
      • nextIs

        private static boolean nextIs​(java.lang.CharSequence text,
                                      int pos,
                                      char ch)
        Returns whether the next character in text after pos is ch.
        Parameters:
        text - the text to analyze
        pos - the position
        ch - the character to look for
        Returns:
        true if the next character is ch, false else
      • advanceIfNextIsNewLine

        private static int advanceIfNextIsNewLine​(java.lang.CharSequence text,
                                                  int pos)
        Advance the given position if it is a new line.
        Parameters:
        text - the text to analyze
        pos - the position
        Returns:
        the (not) advanced position
      • insertIndentation

        public static java.lang.String insertIndentation​(java.lang.String content,
                                                         int indentation,
                                                         boolean skipFirst)
        Inserts indentation into content, i.e., indentation whitespaces at each line start.
        Parameters:
        content - the text to operate on
        indentation - the actual indentation in number of whitespaces (positive number)
        skipFirst - skip the first indentation
        Returns:
        content without indentation
      • isIndentationChar

        public static boolean isIndentationChar​(char ch)
        Returns whether ch is an indentation character.
        Parameters:
        ch - the character
        Returns:
        true for indentation character, false else
      • isLineEnd

        public static boolean isLineEnd​(char ch)
        Returns whether ch is a line end character.
        Parameters:
        ch - the character
        Returns:
        true for line end character, false else
      • skipWhitespaces

        private static int skipWhitespaces​(java.lang.StringBuilder text,
                                           int pos)
        Skips the whitespaces and tabs at pos in text.
        Parameters:
        text - the text to be considered
        pos - the start position
        Returns:
        the position of the first character in text after the whitespaces or the first position after the end of text
      • insertIndentation

        private static int insertIndentation​(java.lang.StringBuilder text,
                                             int pos,
                                             int indentation)
        Inserts an indentation of indentation whitespaces at pos into text.
        Parameters:
        text - the text to be modified
        pos - the insert position
        indentation - the number of whitespaces to be inserted
        Returns:
        the new position at the end of the indentation
      • appendWithLastIndentation

        public static java.lang.String appendWithLastIndentation​(java.lang.String string,
                                                                 java.lang.String text,
                                                                 boolean skipLastIndent)
        Appends the with last indentation to string.
        Parameters:
        string - the string to look into
        text - the text to append
        skipLastIndent - anyway skip the last indentation
        Returns:
        the string with appended indentation (if there was any)
      • countLineEnds

        public static int countLineEnds​(java.lang.CharSequence string,
                                        int startPos)
        Returns the number of line ends considering returns and new lines in string after startPos.
        Parameters:
        string - the string to analyze
        startPos - the start position within string
        Returns:
        the number of line ends
      • allLinesStartWith

        public static boolean allLinesStartWith​(java.lang.String text,
                                                int indent)
        Returns whether all lines start with the given indent.
        Parameters:
        text - the text to analyze
        indent - the indent to check for
        Returns:
        true if all lines start with the given indent, false else
      • removeLastIndentation

        public static java.lang.String removeLastIndentation​(java.lang.String text)
        Removes the last indentation in text if present. This includes (from end of text) line end characters and indentation characters, but no further characters.
        Parameters:
        text - the text to remove the last indentation for
        Returns:
        the text without last indentation
      • isIndentationString

        public static boolean isIndentationString​(java.lang.String text)
        Does the given text just consist of indentation characters?
        Parameters:
        text - the text
        Returns:
        true if just indentation characters, false else