Class ReaderUtils
This class provides helper methods for working with LineReader variables and options. It includes methods for retrieving variables of different types (string, boolean, integer, long) with default values, checking if options are set, and calculating string distances for completion matching.
These utilities are primarily used by the LineReader implementation classes to access configuration values in a consistent way, with proper type conversion and default handling.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
Calculates the edit distance between a word and a candidate string.static boolean
getBoolean
(LineReader reader, String name, boolean def) Gets a boolean variable from a LineReader with a default value.static int
getInt
(LineReader reader, String name, int def) Gets an integer variable from a LineReader with a default value.static long
getLong
(LineReader reader, String name, long def) Gets a long variable from a LineReader with a default value.static String
getString
(LineReader reader, String name, String def) Gets a string variable from a LineReader with a default value.static boolean
isSet
(LineReader reader, LineReader.Option option) Checks if a LineReader option is set.
-
Method Details
-
isSet
Checks if a LineReader option is set.This method safely handles null readers by returning false.
- Parameters:
reader
- the LineReader to check, may be nulloption
- the option to check- Returns:
- true if the reader is not null and the option is set, false otherwise
-
getString
Gets a string variable from a LineReader with a default value.This method safely handles null readers by returning the default value.
- Parameters:
reader
- the LineReader to get the variable from, may be nullname
- the name of the variable to getdef
- the default value to return if the variable is not set or the reader is null- Returns:
- the variable value as a string, or the default value
-
getBoolean
Gets a boolean variable from a LineReader with a default value.This method safely handles null readers by returning the default value. String values are converted to boolean according to these rules:
- Empty string, "on", "1", and "true" (case-insensitive) are considered true
- All other strings are considered false
- Parameters:
reader
- the LineReader to get the variable from, may be nullname
- the name of the variable to getdef
- the default value to return if the variable is not set or the reader is null- Returns:
- the variable value as a boolean, or the default value
-
getInt
Gets an integer variable from a LineReader with a default value.This method safely handles null readers by returning the default value. String values are parsed as integers, with a fallback to 0 if parsing fails.
- Parameters:
reader
- the LineReader to get the variable from, may be nullname
- the name of the variable to getdef
- the default value to return if the variable is not set or the reader is null- Returns:
- the variable value as an integer, or the default value
-
getLong
Gets a long variable from a LineReader with a default value.This method safely handles null readers by returning the default value. String values are parsed as longs, with a fallback to 0 if parsing fails.
- Parameters:
reader
- the LineReader to get the variable from, may be nullname
- the name of the variable to getdef
- the default value to return if the variable is not set or the reader is null- Returns:
- the variable value as a long, or the default value
-
distance
Calculates the edit distance between a word and a candidate string.This method is used for fuzzy matching in completion. It uses the Levenshtein distance algorithm to determine how similar two strings are, with special handling for candidates that are longer than the word being matched.
- Parameters:
word
- the word to match againstcand
- the candidate string to check- Returns:
- the edit distance between the strings (lower values indicate closer matches)
-