Class NumericLiteralNeedsUnderscoreCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class NumericLiteralNeedsUnderscoreCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck

    This check verifies that large numeric literals are spaced by underscores.

    Java 7 allows for underscores to delimit numeric literals to enhance readability because very large numeric literals are hard to read. For example:

    long creditCardNumber = 1234_5678_1234_5678L;

    is much easier to read than

    long creditCardNumber = 1234567812345678L;

    This check comes with the following parameters:

    "minDecimalSymbolLength" - The minimum number of symbols in a decimal literal (includes int, long, float, and double) before the check begins to look for underscores. Numeric literals with delimiters like decimal points or exponentials will be split before checking the length. Default is 7.

    "maxDecimalSymbolsUntilUnderscore" - The maximum number of symbols in a decimal literal allowed before the check demands an underscore. This does not take postfixes and delimiters like decimal points and exponentials into account. Default is 3.

    "minHexSymbolLength" - The minimum number of symbols in a hex literal before the check begins to look for underscores. Numeric literals with delimiters like decimal points or exponentials will be split before checking the length. Default is 5.

    "maxHexSymbolsUntilUnderscore" - The maximum number of symbols in a hex literal allowed before the check demands an underscore. This does not take the prefix 0x, delimiters like decimal points and exponentials, and postfixes into account. Default is 4.

    "minBinarySymbolLength" - The minimum number of symbols in a binary literal before the check begins to look for underscores. Default is 9.

    "maxBinarySymbolsUntilUnderscore" - The maximum number of symbols in a binary literal allowed before the check demands an underscore. This does not take the prefix 0b and postfixes into account. Default is 8.

    Examples (assuming default parameters):

     // Ignored because length of token is 6, which is less than minDecimalSymbolLength (7)
     int ignoredDecimal = 123456;
     // Ignored because the postfix L is not taken into account
     long ignoredDecimal = 123456L;
     // Ignored because each segment delimited by the decimal point has a length
     // less than minDecimalSymbolLength (7)
     float ignoredDecimal = 123456.123456f;
     // Failed because token does not have underscores every 3 characters
     // (maxDecimalSymbolsUntilUnderscore = 3)
     int failingDecimal = 1234567;
     double failingDecimal = 1.1234567e0d;
     int passingDecimal = 1_234_567;
     double passingDecimal = 123456.123456e0d;
     double passingDecimal = 1.123_456_7e0d;
     int ignoredHex = 0xFFFF;
     int failingHex = 0xFFFFFFFF;
     int passingHex = 0xFFFF_FFFF;
     float passingHex = 0xAAAA.BBBBp1f;
     int ignoredBinary = 0b01010101;
     int failingBinary = 0b0000000000000000;
     int passingBinary = 0b00000000_00000000;
     

    An example of how to configure parameters:

     <module name="NumericLiteralNeedsUnderscoreCheck">
        <property name="minDecimalSymbolLength" value="4"/>
        <property name="maxDecimalSymbolsUntilUnderscore" value="3"/>
        <property name="minHexSymbolLength" value="3"/>
        <property name="maxHexSymbolsUntilUnderscore" value="2"/>
        <property name="minBinarySymbolLength" value="5"/>
        <property name="maxBinarySymbolsUntilUnderscore" value="4"/>
     </module>
     

    Examples (assuming above parameters):

     // Ignored because length of token is 3, which is less than minDecimalSymbolLength (4)
     int ignoredDecimal = 123;
     // Ignored because each segment delimited by the decimal point has a length
     // less than minDecimalSymbolLength (4)
     float ignoredDecimal = 123.123f;
     // Failed because token does not have underscores every 3 characters
     // (maxDecimalSymbolsUntilUnderscore = 3)
     int failingDecimal = 1234;
     int passingDecimal = 1_234;
     int ignoredHex = 0xFF;
     int failingHex = 0xFFFF;
     int passingHex = 0xFF_FF;
     int ignoredBinary = 0b0101;
     int failingBinary = 0b00001111;
     int passingBinary = 0b0000_1111;
     
    Since:
    1.18.0
    Author:
    Cheng-Yu Pai
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static class  NumericLiteralNeedsUnderscoreCheck.NumericType
      Type of numeric literal.
      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      Key for error message.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      void setIgnoreFieldNamePattern​(String pattern)
      Sets the regexp pattern for field names to ignore.
      void setMaxBinarySymbolsUntilUnderscore​(int amount)
      Sets how many characters there can be until there must be an underscore (binary literals).
      void setMaxDecimalSymbolsUntilUnderscore​(int amount)
      Sets how many characters there can be until there must be an underscore (for decimal literals).
      void setMaxHexSymbolsUntilUnderscore​(int amount)
      Sets how many characters there can be until there must be an underscore (for hex literals).
      void setMinBinarySymbolLength​(int length)
      Sets how many characters in a byte literal there must be before it checks for an underscore.
      void setMinDecimalSymbolLength​(int length)
      Sets how many characters in a decimal literal there must be before it checks for an underscore.
      void setMinHexSymbolLength​(int length)
      Sets how many characters in a hex literal there must be before it checks for an underscore.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)  
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

        beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

        finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
      • Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Constructor Detail

      • NumericLiteralNeedsUnderscoreCheck

        public NumericLiteralNeedsUnderscoreCheck()
    • Method Detail

      • setMinDecimalSymbolLength

        public void setMinDecimalSymbolLength​(int length)
        Sets how many characters in a decimal literal there must be before it checks for an underscore.
        Parameters:
        length - minimum checking length of the literal
      • setMaxDecimalSymbolsUntilUnderscore

        public void setMaxDecimalSymbolsUntilUnderscore​(int amount)
        Sets how many characters there can be until there must be an underscore (for decimal literals).
        Parameters:
        amount - maximum number of characters between underscores
      • setMinHexSymbolLength

        public void setMinHexSymbolLength​(int length)
        Sets how many characters in a hex literal there must be before it checks for an underscore.
        Parameters:
        length - minimum checking length of the literal
      • setMaxHexSymbolsUntilUnderscore

        public void setMaxHexSymbolsUntilUnderscore​(int amount)
        Sets how many characters there can be until there must be an underscore (for hex literals).
        Parameters:
        amount - maximum number of characters between underscores
      • setMinBinarySymbolLength

        public void setMinBinarySymbolLength​(int length)
        Sets how many characters in a byte literal there must be before it checks for an underscore.
        Parameters:
        length - minimum checking length of the literal
      • setMaxBinarySymbolsUntilUnderscore

        public void setMaxBinarySymbolsUntilUnderscore​(int amount)
        Sets how many characters there can be until there must be an underscore (binary literals).
        Parameters:
        amount - maximum number of characters between underscores
      • setIgnoreFieldNamePattern

        public void setIgnoreFieldNamePattern​(String pattern)
        Sets the regexp pattern for field names to ignore.
        Parameters:
        pattern - the regexp pattern of fields to ignore
      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck