Class SafeIdUtil
java.lang.Object
com.jagrosh.jdautilities.commons.utils.SafeIdUtil
A Utilities class for safely checking and converting String IDs to longs usable with
MiscUtil.parseSnowflake(String)
, a utility used in
several Object#getXById(String)
methods.
This class contains two static methods:
SafeIdUtil.safeConvert(String)
- Safely converts a String to a format usable withMiscUtil.parseSnowflake(String)
.SafeIdUtil.checkId(String)
- Checks if a String is safe to use withMiscUtil.parseSnowflake(String)
as it is.
- Since:
- 1.2
- Author:
- Kaidan Gustave
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Checks if the provided String ID is usable withMiscUtil.parseSnowflake(String)
.static long
safeConvert
(String id) Safely convert the provided String ID to along
usable withMiscUtil.parseSnowflake(String)
.
-
Method Details
-
safeConvert
Safely convert the provided String ID to along
usable withMiscUtil.parseSnowflake(String)
.- Parameters:
id
- The String ID to be converted- Returns:
- If the String can be converted into a non-negative
long
, then it will return the conversion.
However, if one of the following criteria is met, then this method will return0L
:- If the provided String throws a
NumberFormatException
when used withLong.parseLong(String)
. - If the provided String is converted, but the returned
long
is negative.
- If the provided String throws a
-
checkId
Checks if the provided String ID is usable withMiscUtil.parseSnowflake(String)
.- Parameters:
id
- The String ID to be converted- Returns:
true
if both of the following criteria are not met:- If the provided String throws a
NumberFormatException
when used withLong.parseLong(String)
. - If the provided String is converted, but the returned
long
is negative.
- If the provided String throws a
-