Class CodeFormat

  • All Implemented Interfaces:
    java.lang.Cloneable

    public final class CodeFormat
    extends java.lang.Object
    implements java.lang.Cloneable
    Encapsulates some common Java source code formatting options. The class can also be used as a buffer for formatted Java code.
    Author:
    Abe White
    • Constructor Summary

      Constructors 
      Constructor Description
      CodeFormat()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      CodeFormat afterSection()
      Append the given value to the internal buffer.
      CodeFormat append​(boolean val)
      Append the given value to the internal buffer.
      CodeFormat append​(byte val)
      Append the given value to the internal buffer.
      CodeFormat append​(char val)
      Append the given value to the internal buffer.
      CodeFormat append​(double val)
      Append the given value to the internal buffer.
      CodeFormat append​(float val)
      Append the given value to the internal buffer.
      CodeFormat append​(int val)
      Append the given value to the internal buffer.
      CodeFormat append​(long val)
      Append the given value to the internal buffer.
      CodeFormat append​(short val)
      Append the given value to the internal buffer.
      CodeFormat append​(java.lang.Object val)
      Append the given value to the internal buffer.
      void clear()
      Clear the internal code buffer.
      java.lang.Object clone()
      Make a copy of this code format object with all the same formatting settings.
      CodeFormat closeBrace​(int tabLevel)
      Append the given value to the internal buffer.
      CodeFormat closeParen()
      Append the given value to the internal buffer.
      CodeFormat endl()
      Append the given value to the internal buffer.
      CodeFormat endl​(int num)
      Append the given value to the internal buffer.
      CodeFormat endl​(int num, int tabs)
      Append the given value to the internal buffer.
      CodeFormat extendsDec​(int tabLevel)
      Append the given value to the internal buffer.
      CodeFormat fieldName​(java.lang.String name)
      Append the given value to the internal buffer.
      java.lang.String getAfterSection()
      Return {#getLinesBetweenSections} + 1 new line characters.
      boolean getBraceAtSameTabLevel()
      Whether to place braces at the same tab level as the code within the block.
      boolean getBraceOnSameLine()
      Whether to place opening braces on the same line as the block declaration, or on the next line.
      java.lang.StringBuffer getBuffer()
      Return the internal code buffer.
      java.lang.String getCloseBrace​(int tabLevel)
      Close brace string.
      java.lang.String getCloseParen()
      Close parentheses string.
      java.lang.String getEndl()
      Return a new line character.
      java.lang.String getEndl​(int num)
      Return the given number of new line characters.
      java.lang.String getEndl​(int num, int tabs)
      Return the given number of new line characters, followed by the given tab level indentation.
      java.lang.String getExtendsDec​(int tabLevel)
      Extends declaration.
      java.lang.String getFieldName​(java.lang.String fieldName)
      Return the field name for given suggested name, possibly adding leading underscore.
      java.lang.String getImplementsDec​(int tabLevel)
      Implements declaration.
      int getLinesBetweenSections()
      The number of empty lines between code sections.
      java.lang.String getOpenBrace​(int tabLevel)
      Open brace string.
      java.lang.String getOpenParen​(boolean methodOrIf)
      Open parentheses string.
      java.lang.String getParametrizedType​(java.lang.String[] typenames)
      Returns parametrized type string for given type(s).
      java.lang.String getParens()
      Paired parentheses for empty method parameters.
      boolean getScoreBeforeFieldName()
      Whether to place an underscore before private field names.
      boolean getSpaceBeforeParen()
      Whether to place a space before parentheses.
      boolean getSpaceInParen()
      Whether to place a space within parentheses.
      java.lang.String getTab()
      Tab string.
      java.lang.String getTab​(int tabLevel)
      Tab string.
      int getTabSpaces()
      The number of spaces to use for tabs; 0 means to use actual tab characters.
      java.lang.String getThrowsDec​(int tabLevel)
      Throws declaration.
      CodeFormat implementsDec​(int tabLevel)
      Append the given value to the internal buffer.
      int length()
      Return the length of the internal buffer.
      CodeFormat openBrace​(int tabLevel)
      Append the given value to the internal buffer.
      CodeFormat openParen​(boolean methodOrIf)
      Append the given value to the internal buffer.
      CodeFormat parens()
      Append the given value to the internal buffer.
      void setBraceAtSameTabLevel​(boolean braceAtSameTabLevel)
      Whether to place braces at the same tab level as the code within the block.
      void setBraceOnSameLine​(boolean braceOnSameLine)
      Whether to place opening braces on the same line as the block declaration, or on the next line.
      void setLinesBetweenSections​(int linesBetweenSections)
      The number of empty lines between sections.
      void setScoreBeforeFieldName​(boolean scoreBeforeFieldName)
      Whether to place an underscore before private field names.
      void setSpaceBeforeParen​(boolean spaceBeforeParen)
      Whether to place a space before parentheses.
      void setSpaceInParen​(boolean spaceInParen)
      Whether to place a space within parentheses.
      void setTabSpaces​(int tab)
      The number of spaces to use for tabs; 0 means to use actual tab characters.
      CodeFormat tab()
      Append the given value to the internal buffer.
      CodeFormat tab​(int tabLevel)
      Append the given value to the internal buffer.
      CodeFormat throwsDec​(int tabLevel)
      Append the given value to the internal buffer.
      java.lang.String toString()
      Return the internal buffer as a string.
      • Methods inherited from class java.lang.Object

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

      • CodeFormat

        public CodeFormat()
    • Method Detail

      • getTabSpaces

        public int getTabSpaces()
        The number of spaces to use for tabs; 0 means to use actual tab characters. Defaults to 0.
      • setTabSpaces

        public void setTabSpaces​(int tab)
        The number of spaces to use for tabs; 0 means to use actual tab characters. Defaults to 0.
      • getSpaceBeforeParen

        public boolean getSpaceBeforeParen()
        Whether to place a space before parentheses. Defaults to false.
      • setSpaceBeforeParen

        public void setSpaceBeforeParen​(boolean spaceBeforeParen)
        Whether to place a space before parentheses. Defaults to false.
      • getSpaceInParen

        public boolean getSpaceInParen()
        Whether to place a space within parentheses. Defaults to false.
      • setSpaceInParen

        public void setSpaceInParen​(boolean spaceInParen)
        Whether to place a space within parentheses. Defaults to false.
      • getBraceOnSameLine

        public boolean getBraceOnSameLine()
        Whether to place opening braces on the same line as the block declaration, or on the next line. Defaults to same line.
      • setBraceOnSameLine

        public void setBraceOnSameLine​(boolean braceOnSameLine)
        Whether to place opening braces on the same line as the block declaration, or on the next line. Defaults to same line.
      • getBraceAtSameTabLevel

        public boolean getBraceAtSameTabLevel()
        Whether to place braces at the same tab level as the code within the block. Defaults to false.
      • setBraceAtSameTabLevel

        public void setBraceAtSameTabLevel​(boolean braceAtSameTabLevel)
        Whether to place braces at the same tab level as the code within the block. Defaults to false.
      • getScoreBeforeFieldName

        public boolean getScoreBeforeFieldName()
        Whether to place an underscore before private field names. Defaults to false.
      • setScoreBeforeFieldName

        public void setScoreBeforeFieldName​(boolean scoreBeforeFieldName)
        Whether to place an underscore before private field names. Defaults to false.
      • getLinesBetweenSections

        public int getLinesBetweenSections()
        The number of empty lines between code sections. Defaults to 1.
      • setLinesBetweenSections

        public void setLinesBetweenSections​(int linesBetweenSections)
        The number of empty lines between sections. Defaults to 1.
      • getEndl

        public java.lang.String getEndl()
        Return a new line character.
      • getEndl

        public java.lang.String getEndl​(int num)
        Return the given number of new line characters.
      • getEndl

        public java.lang.String getEndl​(int num,
                                        int tabs)
        Return the given number of new line characters, followed by the given tab level indentation.
      • getAfterSection

        public java.lang.String getAfterSection()
        Return {#getLinesBetweenSections} + 1 new line characters.
      • getOpenParen

        public java.lang.String getOpenParen​(boolean methodOrIf)
        Open parentheses string. Users can choose to place spaces before and within parentheses.
      • getCloseParen

        public java.lang.String getCloseParen()
        Close parentheses string. Users can choose to place spaces within parentheses.
      • getParens

        public java.lang.String getParens()
        Paired parentheses for empty method parameters. Users can choose to place spaces before parentheses.
      • getOpenBrace

        public java.lang.String getOpenBrace​(int tabLevel)
        Open brace string. Users can choose to place braces on the same line, or on a new line, and can choose the indenting level.
        Parameters:
        tabLevel - the tab level of code within the brace
      • getCloseBrace

        public java.lang.String getCloseBrace​(int tabLevel)
        Close brace string. Users can choose to place braces on the same line, or on a new line, and can choose the indenting level.
        Parameters:
        tabLevel - the tab level of code within the brace
      • getExtendsDec

        public java.lang.String getExtendsDec​(int tabLevel)
        Extends declaration. Uses configuration of openBrace(int), but prints "extends" instead of a brace.
      • getImplementsDec

        public java.lang.String getImplementsDec​(int tabLevel)
        Implements declaration. Uses configuration of openBrace(int), but prints "implements" instead of a brace.
      • getThrowsDec

        public java.lang.String getThrowsDec​(int tabLevel)
        Throws declaration. Uses configuration of openBrace(int), but prints "throws" instead of a brace.
      • getTab

        public java.lang.String getTab()
        Tab string. Users can choose to use spaces or tab characters.
      • getTab

        public java.lang.String getTab​(int tabLevel)
        Tab string. Users can choose to use spaces or tab characters.
        Parameters:
        tabLevel - the number of tabs
      • getParametrizedType

        public java.lang.String getParametrizedType​(java.lang.String[] typenames)
        Returns parametrized type string for given type(s).
      • getFieldName

        public java.lang.String getFieldName​(java.lang.String fieldName)
        Return the field name for given suggested name, possibly adding leading underscore.
      • getBuffer

        public java.lang.StringBuffer getBuffer()
        Return the internal code buffer.
      • append

        public CodeFormat append​(boolean val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(byte val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(char val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(double val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(float val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(int val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(long val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(short val)
        Append the given value to the internal buffer.
      • append

        public CodeFormat append​(java.lang.Object val)
        Append the given value to the internal buffer.
      • endl

        public CodeFormat endl()
        Append the given value to the internal buffer.
        See Also:
        getEndl()
      • endl

        public CodeFormat endl​(int num)
        Append the given value to the internal buffer.
        See Also:
        getEndl(int)
      • parens

        public CodeFormat parens()
        Append the given value to the internal buffer.
        See Also:
        getParens()
      • tab

        public CodeFormat tab()
        Append the given value to the internal buffer.
        See Also:
        getTab()
      • tab

        public CodeFormat tab​(int tabLevel)
        Append the given value to the internal buffer.
        See Also:
        getTab()
      • clear

        public void clear()
        Clear the internal code buffer.
      • toString

        public java.lang.String toString()
        Return the internal buffer as a string.
        Overrides:
        toString in class java.lang.Object
      • length

        public int length()
        Return the length of the internal buffer.
      • clone

        public java.lang.Object clone()
        Make a copy of this code format object with all the same formatting settings.
        Overrides:
        clone in class java.lang.Object