Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods in KiwiPrimitives to try parsing CharSequence to primitives #914

Closed
sleberknight opened this issue Mar 14, 2023 · 1 comment · Fixed by #915, #916 or #917
Closed

Add methods in KiwiPrimitives to try parsing CharSequence to primitives #914

sleberknight opened this issue Mar 14, 2023 · 1 comment · Fixed by #915, #916 or #917
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

sleberknight commented Mar 14, 2023

Guava has several methods across several utility classes to attempt parsing String values into int, long, double, and float:

  • Integer com.google.common.primitives.Ints#tryParse(String string)
  • Integer com.google.common.primitives.Ints#tryParse(String string, int radix)
  • Long com.google.common.primitives.Longs#tryParse(String string)
  • Long com.google.common.primitives.Longs#tryParse(String string, int radix)

There are similar methods in the Guava Doubles and Floats classes to the above. These are generally OK, but force you to handle the possible null return value.

CodeQL really doesn't like it when you do not handle NumberFormatException, and flags usages of JDK methods like Integer#parseInt with "Missing catch of NumberFormatException" errors like this one.

This issue proposes to add tryParse methods to KiwiPrimitives and provide several options to parse strings (as CharSequence for more flexibility). The proposed methods will handle int, long, and double to start with. They will provide callers several options:

  • return null
  • return Optional for the primitive type, e.g. OptionalInt
  • return the primitive type, or throw IllegalStateException (not IllegalArgumentException because these are intended to be used in the course of business logic, not for argument validation)

One other possible variant is to allow the caller to provide an exception Supplier, e.g. Supplier<Exception> or Supplier<RuntimeException> which would allow for throwing custom exceptions. But for now this is probably not necessary.

The tryParse methods should accept the more general CharSequence such that other common types such as StringBuilder can also be parsed easily.

Initially there will only be variants that assume a radix of 10, e.g. internally will call the JDK method without the radix.

For int, the following methods are proposed:

  • Integer tryParseIntOrNull(CharSequence cs)
  • OptionalInt tryParseInt(CharSequence cs)
  • int tryParseIntOrThrow(CharSequence cs)

There will be the same three methods for long and double.

@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Mar 14, 2023
@sleberknight sleberknight added this to the 2.6.0 milestone Mar 14, 2023
@sleberknight sleberknight self-assigned this Mar 14, 2023
@sleberknight sleberknight added the in progress A task that is actively being worked on label Mar 14, 2023
@sleberknight
Copy link
Member Author

This is not done. GitHub, why did you auto-close this when I didn't put "closing keywords" in the commit???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
1 participant