-
Notifications
You must be signed in to change notification settings - Fork 1
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
Labels
new feature
A new feature such as a new class, method, package, group of classes, etc.
Milestone
Comments
sleberknight
added
the
new feature
A new feature such as a new class, method, package, group of classes, etc.
label
Mar 14, 2023
sleberknight
added a commit
that referenced
this issue
Mar 15, 2023
sleberknight
added a commit
that referenced
this issue
Mar 15, 2023
sleberknight
added a commit
that referenced
this issue
Mar 15, 2023
chrisrohr
pushed a commit
that referenced
this issue
Mar 15, 2023
This is not done. GitHub, why did you auto-close this when I didn't put "closing keywords" in the commit??? |
sleberknight
added a commit
that referenced
this issue
Mar 15, 2023
chrisrohr
pushed a commit
that referenced
this issue
Mar 15, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andFloats
classes to the above. These are generally OK, but force you to handle the possiblenull
return value.CodeQL really doesn't like it when you do not handle
NumberFormatException
, and flags usages of JDK methods likeInteger#parseInt
with "Missing catch of NumberFormatException" errors like this one.This issue proposes to add
tryParse
methods toKiwiPrimitives
and provide several options to parse strings (asCharSequence
for more flexibility). The proposed methods will handleint
,long
, anddouble
to start with. They will provide callers several options:null
Optional
for the primitive type, e.g.OptionalInt
IllegalStateException
(notIllegalArgumentException
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>
orSupplier<RuntimeException>
which would allow for throwing custom exceptions. But for now this is probably not necessary.The
tryParse
methods should accept the more generalCharSequence
such that other common types such asStringBuilder
can also be parsed easily.Initially there will only be variants that assume a
radix
of10
, e.g. internally will call the JDK method without theradix
.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
anddouble
.The text was updated successfully, but these errors were encountered: