Package org.codehaus.groovy.syntax
Class Numbers
java.lang.Object
org.codehaus.groovy.syntax.Numbers
Utility class for processing and parsing Groovy numeric literals.
Provides methods for character classification (digits, hex digits, etc.)
and conversion of numeric literal strings to appropriate
Number objects,
handling various bases (decimal, octal, hexadecimal, binary) and type suffixes.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanisDigit(char c) Returnstrueif the specified character is a decimal digit (0-9).static booleanisHexDigit(char c) Returnstrueif the specified character is a hexadecimal digit (0-9, A-F, a-f).static booleanisNumericTypeSpecifier(char c, boolean isDecimal) Returnstrueif the specified character is a valid type suffix for a numeric literal.static booleanisOctalDigit(char c) Returnstrueif the specified character is an octal digit (0-7).static NumberparseDecimal(String text) Builds a Number from the given decimal descriptor.static NumberparseInteger(String text) Builds a Number from the given integer descriptor.
-
Constructor Details
-
Numbers
public Numbers()
-
-
Method Details
-
isDigit
public static boolean isDigit(char c) Returnstrueif the specified character is a decimal digit (0-9).- Parameters:
c- the character to check- Returns:
trueif the character is a decimal digit
-
isOctalDigit
public static boolean isOctalDigit(char c) Returnstrueif the specified character is an octal digit (0-7).- Parameters:
c- the character to check- Returns:
trueif the character is an octal digit
-
isHexDigit
public static boolean isHexDigit(char c) Returnstrueif the specified character is a hexadecimal digit (0-9, A-F, a-f).- Parameters:
c- the character to check- Returns:
trueif the character is a hexadecimal digit
-
isNumericTypeSpecifier
public static boolean isNumericTypeSpecifier(char c, boolean isDecimal) Returnstrueif the specified character is a valid type suffix for a numeric literal. For decimal literals: G/g (BigDecimal), D/d (double), F/f (float). For integer literals: G/g (BigInteger), I/i (int), L/l (long).- Parameters:
c- the character to checkisDecimal-trueif checking a decimal literal suffix,falsefor integer- Returns:
trueif the character is a valid numeric type specifier
-
parseInteger
Builds a Number from the given integer descriptor. Creates the narrowest type possible, or a specific type, if specified.- Parameters:
text- literal text to parse- Returns:
- instantiated Number object
- Throws:
NumberFormatException- if the number does not fit within the type requested by the type specifier suffix (invalid numbers don't make it here)
-
parseDecimal
Builds a Number from the given decimal descriptor. Uses BigDecimal, unless, Double or Float is requested.- Parameters:
text- literal text to parse- Returns:
- instantiated Number object
- Throws:
NumberFormatException- if the number does not fit within the type requested by the type specifier suffix (invalid numbers don't make it here)
-