Class AIResponseParser
java.lang.Object
org.tavall.gemini.utils.AIResponseParser
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanparseBoolean(String raw, boolean fallback) Parses the given string into a boolean value.static StringparseBoundedString(String raw, int maxLength, String fallback) Parses a raw string and ensures the result does not exceed a specified maximum length.static doubleparseConfidence(String raw, double fallback) Parses a confidence value from a string representation.static doubleparseDouble(String raw, double fallback) Parses a string and attempts to convert it to a double value.static doubleparseDouble(String raw, double min, double max, double fallback) Parses a string to a double value while ensuring it falls within a specified range.parseDoubleList(String raw) Parses a string containing a list of numbers into a list of Double values.static <T extends Enum<T>>
TParses the given raw string into an enum constant of the specified enum type.parseEnumList(String raw, Class<T> enumType) Parses a delimited string into a list of Enum constants of the specified type.static InstantparseInstant(String raw, Instant fallback) Parses a string into anInstant.static intParses the given raw string into an integer.static intParses a string into an integer, ensuring the parsed value falls within a specified range.parseIntList(String raw) Parses a string containing integer values separated by delimiters (commas, semicolons, or whitespace) into a list of integers.parseKeyValueMap(String raw) Parses a raw string into a map of key-value pairs.static longParses the given string into a long value.parseLongList(String raw) Parses a delimited string into a list of Long values.static StringparseString(String raw, String fallback) Parses a raw string and returns a trimmed, non-empty string.parseStringList(String raw) Parses a raw string into a list of trimmed strings, splitting the input by common delimiters such as commas, semicolons, or newline characters.static voidpraseGeminiResponse(String response)
-
Constructor Details
-
AIResponseParser
public AIResponseParser()
-
-
Method Details
-
praseGeminiResponse
-
parseString
Parses a raw string and returns a trimmed, non-empty string. If the input is null, empty, or consists of only whitespace, the fallback value is returned.- Parameters:
raw- the raw string to parse. May be null or contain leading/trailing whitespace.fallback- the fallback string to return if the raw string is null, empty, or only whitespace.- Returns:
- a trimmed, non-empty string if the raw string is valid; otherwise, the fallback value.
-
parseBoundedString
Parses a raw string and ensures the result does not exceed a specified maximum length. If the raw string is null or empty, the fallback value is returned. If the parsed string's length exceeds the specified maximum length, the fallback value is returned.- Parameters:
raw- the raw input string to be parsedmaxLength- the maximum allowed length of the parsed stringfallback- the fallback value to return if the raw string is invalid or exceeds the maximum length- Returns:
- the parsed string if valid and within the maximum length, otherwise the fallback value
-
parseInt
Parses the given raw string into an integer. If the parsing fails (e.g., due to the input not being a valid number), the specified fallback value is returned instead.- Parameters:
raw- the input string to be parsed as an integer. It may include leading or trailing whitespace.fallback- the value to return in case the parsing fails or the input is invalid.- Returns:
- the parsed integer if successful, or the fallback value if parsing fails.
-
parseInt
Parses a string into an integer, ensuring the parsed value falls within a specified range. If the parsed value is outside the range or the input is invalid, returns a fallback value.- Parameters:
raw- the input string to parsemin- the minimum allowed value (inclusive)max- the maximum allowed value (inclusive)fallback- the fallback value to use if parsing fails or the value is out of range- Returns:
- the parsed integer if valid and within the range; otherwise, the fallback value
-
parseLong
Parses the given string into a long value. If the string cannot be parsed, the specified fallback value is returned.- Parameters:
raw- the string to be parsed. It is expected to contain a valid long value or be trim-able to one.fallback- the value to return if parsing fails.- Returns:
- the parsed long value if successful; otherwise, the fallback value.
-
parseDouble
Parses a string and attempts to convert it to a double value. If the conversion fails due to an exception (e.g., NumberFormatException), the provided fallback value is returned.- Parameters:
raw- the input string to parse, which may contain a numeric value in string formfallback- the value to return if parsing fails or the input string is invalid- Returns:
- the parsed double value if the conversion is successful; otherwise, the fallback value
-
parseDouble
Parses a string to a double value while ensuring it falls within a specified range. If the parsed value is outside the range or the string cannot be parsed, a fallback value is returned.- Parameters:
raw- the string to parse into a doublemin- the minimum allowable value (inclusive)max- the maximum allowable value (inclusive)fallback- the value to return if parsing fails or the parsed value is out of range- Returns:
- the parsed double value if it is within the specified range, otherwise the fallback value
-
parseBoolean
Parses the given string into a boolean value. The method recognizes certain strings such as "true", "1", "yes" (case-insensitive) as true values, and "false", "0", "no" (case-insensitive) as false values. If the string does not match any of the recognized values or an error occurs during parsing, the fallback value is returned.- Parameters:
raw- the input string to be parsedfallback- the fallback boolean value to return if parsing fails or the input is not recognized- Returns:
- the parsed boolean value if successful, or the fallback value otherwise
-
parseEnum
Parses the given raw string into an enum constant of the specified enum type. If the parsing fails or if the raw string is invalid, the provided fallback value is returned.- Type Parameters:
T- the type of the enum- Parameters:
raw- the raw string to be parsedenumType- the class of the enum to which the string should be parsedfallback- the fallback value to return in case of parsing failure- Returns:
- the parsed enum constant if successful; the fallback value otherwise
-
parseEnumList
Parses a delimited string into a list of Enum constants of the specified type. The method splits the input string using delimiters such as commas, semicolons, or whitespace and attempts to convert each part into an Enum constant of the specified type. Values that cannot be converted or are null are ignored.- Type Parameters:
T- the type of Enum being parsed- Parameters:
raw- the raw, delimited string to parseenumType- the Enum class type to which the parsed values belong- Returns:
- a list of Enum constants parsed from the input string, or an empty list if parsing fails
-
parseIntList
Parses a string containing integer values separated by delimiters (commas, semicolons, or whitespace) into a list of integers. Non-numeric or invalid parts are ignored.- Parameters:
raw- the input string containing integer values delimited by commas, semicolons, or whitespace- Returns:
- a list of parsed integers, or an empty list if the parsing fails
-
parseLongList
Parses a delimited string into a list of Long values. The input string is split based on delimiters such as commas, semicolons, or whitespace. Each resulting part is parsed into a Long. Invalid parts are ignored.- Parameters:
raw- the delimited input string to parse, may contain numbers separated by commas, semicolons, or whitespace- Returns:
- a list of parsed Long values, or an empty list if no valid numbers are found
-
parseDoubleList
Parses a string containing a list of numbers into a list of Double values. The input string is split using delimiters such as commas, semicolons, or whitespace. Each extracted substring is then converted into a Double. If conversion fails for any substring, it is ignored.- Parameters:
raw- the raw string to parse, containing numeric values separated by delimiters- Returns:
- a list of Double values parsed from the input string, or an empty list if the input is null, empty, or contains no valid numbers
-
parseStringList
Parses a raw string into a list of trimmed strings, splitting the input by common delimiters such as commas, semicolons, or newline characters. Empty or whitespace-only entries are excluded from the result.- Parameters:
raw- the raw input string to parse, which may contain multiple tokens separated by delimiters such as commas, semicolons, or newlines- Returns:
- a list of non-empty, trimmed strings parsed from the input
-
parseKeyValueMap
Parses a raw string into a map of key-value pairs. The input string is split into lines using commas or newline characters. Each line is further split into key and value using either a colon (:) or an equals sign (=). Keys are trimmed, converted to lowercase, and stored with their corresponding trimmed values in the resulting map.- Parameters:
raw- the raw input string containing key-value pairs, separated by commas, newline characters, colons, or equals signs- Returns:
- a map containing the parsed and normalized key-value pairs, or an empty map if parsing fails
-
parseInstant
Parses a string into anInstant. If the parsing fails, the provided fallback value is returned. -
parseConfidence
Parses a confidence value from a string representation. The input string may represent a percentage (e.g., "75%") or a decimal value (e.g., "0.75"). If the parsed value is greater than 1.0, it is treated as a percentage (i.e., divided by 100). Values below 0.0 are clamped to 0.0, and values above 1.0 are clamped to 1.0. If parsing fails, the provided fallback value is returned.- Parameters:
raw- the raw string to parse as a confidence valuefallback- the fallback value to return if parsing fails- Returns:
- the parsed confidence value as a double, adjusted to be within the valid range [0.0, 1.0], or the fallback value if parsing fails
-