Class ReaderUtils
- java.lang.Object
-
- org.jline.reader.impl.ReaderUtils
-
public class ReaderUtils extends java.lang.Object
Utility methods for LineReader implementations.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.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
distance(java.lang.String word, java.lang.String cand)
Calculates the edit distance between a word and a candidate string.static boolean
getBoolean(LineReader reader, java.lang.String name, boolean def)
Gets a boolean variable from a LineReader with a default value.static int
getInt(LineReader reader, java.lang.String name, int def)
Gets an integer variable from a LineReader with a default value.static long
getLong(LineReader reader, java.lang.String name, long def)
Gets a long variable from a LineReader with a default value.static java.lang.String
getString(LineReader reader, java.lang.String name, java.lang.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 Detail
-
isSet
public static boolean isSet(LineReader reader, LineReader.Option option)
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
public static java.lang.String getString(LineReader reader, java.lang.String name, java.lang.String def)
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
public static boolean getBoolean(LineReader reader, java.lang.String name, boolean def)
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
public static int getInt(LineReader reader, java.lang.String name, int def)
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
public static long getLong(LineReader reader, java.lang.String name, long def)
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
public static int distance(java.lang.String word, java.lang.String cand)
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)
-
-