Class NumberUtil
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault radix of a number.static final intNumber of bits in anintexcept for the sign bit.static final intNumber of bits in alongexcept for the sign bit. -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull OptionalIntparseInt(@NonNull CharSequence possibleInteger) Parses a decimalintnumber.static @NotNull OptionalIntparseInt(@NonNull CharSequence possibleInteger, int radix) Parses anintnumber.static @NotNull Result<@NotNull Integer,@NotNull IntegerParseError> parseIntResult(@NonNull CharSequence possibleInteger) Parses a decimalintnumber.static @NotNull Result<@NotNull Integer,@NotNull IntegerParseError> parseIntResult(@NonNull CharSequence possibleInteger, int radix) Parses anintnumber.static @NotNull OptionalLongparseLong(@NonNull CharSequence possibleLong) Parses a decimallongnumber.static @NotNull OptionalLongparseLong(@NonNull CharSequence possibleLong, int radix) Parses alongnumber.static @NotNull Result<@NotNull Long,@NotNull IntegerParseError> parseLongResult(@NonNull CharSequence possibleLong) Parses a decimallongnumber.static @NotNull Result<@NotNull Long,@NotNull IntegerParseError> parseLongResult(@NonNull CharSequence possibleLong, int radix) Parses alongnumber.static intsaturatingSum(int left, int right) Sums theintvalues using their bounds instead of wrapping on overflow.static longsaturatingSum(long left, long right) Sums thelongvalues using their bounds instead of wrapping on overflow.
-
Field Details
-
INT_NON_SIGN_BITS
public static final int INT_NON_SIGN_BITSNumber of bits in anintexcept for the sign bit.- See Also:
-
LONG_NON_SIGN_BITS
public static final int LONG_NON_SIGN_BITSNumber of bits in alongexcept for the sign bit.- See Also:
-
DEFAULT_RADIX
public static final int DEFAULT_RADIXDefault radix of a number.- See Also:
-
-
Method Details
-
parseInt
@NotNull public static @NotNull OptionalInt parseInt(@NonNull @NonNull CharSequence possibleInteger, int radix) Parses an
intnumber.This method is based on OpenJDK
Integer.parseInt(String, int)but it returnsOptionalIntto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleInteger- string which is expected to contain anintnumber but may notradix- radix of the possible number- Returns:
- optional containing the parsed number or an empty one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
parseInt
@NotNull public static @NotNull OptionalInt parseInt(@NonNull @NonNull CharSequence possibleInteger) Parses a decimal
intnumber.This method is based on OpenJDK
Integer.parseInt(String, int)but it returnsOptionalIntto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleInteger- string which is expected to contain anintnumber but may not- Returns:
- optional containing the parsed number or an empty one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
parseIntResult
@NotNull public static @NotNull Result<@NotNull Integer,@NotNull IntegerParseError> parseIntResult(@NonNull @NonNull CharSequence possibleInteger, int radix) Parses an
intnumber.This method is based on OpenJDK
Integer.parseInt(String, int)but it returnsResultto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleInteger- string which is expected to contain anintnumber but may notradix- radix of the possible number- Returns:
- result containing the parsed number or an error one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
parseIntResult
@NotNull public static @NotNull Result<@NotNull Integer,@NotNull IntegerParseError> parseIntResult(@NonNull @NonNull CharSequence possibleInteger) Parses a decimal
intnumber.This method is based on OpenJDK
Integer.parseInt(String, int)but it returnsResultto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleInteger- string which is expected to contain anintnumber but may not- Returns:
- result containing the parsed number or an error one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
parseLong
@NotNull public static @NotNull OptionalLong parseLong(@NonNull @NonNull CharSequence possibleLong, int radix) Parses a
longnumber.This method is based on OpenJDK
Long.parseLong(String, int)but it returnsOptionalLongto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleLong- string which is expected to contain alongnumber but may notradix- radix of the possible number- Returns:
- optional containing the parsed number or an empty one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
parseLong
Parses a decimal
longnumber.This method is based on OpenJDK
Long.parseLong(String, int)but it returnsOptionalLongto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleLong- string which is expected to contain alongnumber but may not- Returns:
- optional containing the parsed number or an empty one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
parseLongResult
@NotNull public static @NotNull Result<@NotNull Long,@NotNull IntegerParseError> parseLongResult(@NonNull @NonNull CharSequence possibleLong, int radix) Parses a
longnumber.This method is based on OpenJDK
Long.parseLong(String, int)but it returnsResultto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleLong- string which is expected to contain alongnumber but may notradix- radix of the possible number- Returns:
- result containing the parsed number or an error one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
parseLongResult
@NotNull public static @NotNull Result<@NotNull Long,@NotNull IntegerParseError> parseLongResult(@NonNull @NonNull CharSequence possibleLong) Parses a decimal
longnumber.This method is based on OpenJDK
Long.parseLong(String, int)but it returnsResultto handle invalid input instead of throwingNumberFormatException.- Parameters:
possibleLong- string which is expected to contain alongnumber but may not- Returns:
- result containing the parsed number or an error one if it could not be parsed
- Throws:
IllegalArgumentException- if the radix is out of bound [Character.MIN_RADIX;Character.MAX_RADIX].
-
saturatingSum
public static int saturatingSum(int left, int right) Sums theintvalues using their bounds instead of wrapping on overflow.- Parameters:
left- left addendright- right addend- Returns:
- sum of the addends
- Implementation note
- implementation is based on Stackoverflow answer
-
saturatingSum
public static long saturatingSum(long left, long right) Sums thelongvalues using their bounds instead of wrapping on overflow.- Parameters:
left- left addendright- right addend- Returns:
- sum of the addends
- Implementation note
- implementation is based on Stackoverflow answer
-