Class AIResponseParser

java.lang.Object
org.tavall.gemini.utils.AIResponseParser

public class AIResponseParser extends Object
  • Constructor Details

    • AIResponseParser

      public AIResponseParser()
  • Method Details

    • praseGeminiResponse

      public static void praseGeminiResponse(String response)
    • parseString

      public static String parseString(String raw, String fallback)
      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

      public static String parseBoundedString(String raw, int maxLength, String fallback)
      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 parsed
      maxLength - the maximum allowed length of the parsed string
      fallback - 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

      public static int parseInt(String raw, int fallback)
      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

      public static int parseInt(String raw, int min, int max, int fallback)
      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 parse
      min - 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

      public static long parseLong(String raw, long fallback)
      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

      public static double parseDouble(String raw, double fallback)
      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 form
      fallback - 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

      public static double parseDouble(String raw, double min, double max, double fallback)
      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 double
      min - 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

      public static boolean parseBoolean(String raw, boolean fallback)
      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 parsed
      fallback - 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

      public static <T extends Enum<T>> T parseEnum(String raw, Class<T> enumType, T fallback)
      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 parsed
      enumType - the class of the enum to which the string should be parsed
      fallback - the fallback value to return in case of parsing failure
      Returns:
      the parsed enum constant if successful; the fallback value otherwise
    • parseEnumList

      public static <T extends Enum<T>> List<T> parseEnumList(String raw, Class<T> enumType)
      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 parse
      enumType - 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

      public static List<Integer> parseIntList(String raw)
      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

      public static List<Long> parseLongList(String raw)
      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

      public static List<Double> parseDoubleList(String raw)
      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

      public static List<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. 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

      public static Map<String,String> parseKeyValueMap(String raw)
      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

      public static Instant parseInstant(String raw, Instant fallback)
      Parses a string into an Instant. If the parsing fails, the provided fallback value is returned.
      Parameters:
      raw - the input string to parse into an Instant; may be null or contain whitespace.
      fallback - the fallback Instant value to return if parsing fails.
      Returns:
      the parsed Instant if successful, or the fallback Instant if an error occurs.
    • parseConfidence

      public static double parseConfidence(String raw, double fallback)
      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 value
      fallback - 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