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 FilecreateSecureTempFile(String prefix, String suffix) Creates a temporary file with owner-only read/write permissions.static intCalculates the edit distance between a word and a candidate string.static booleangetBoolean(LineReader reader, String name, boolean def) Gets a boolean variable from a LineReader with a default value.static intgetInt(LineReader reader, String name, int def) Gets an integer variable from a LineReader with a default value.static longgetLong(LineReader reader, String name, long def) Gets a long variable from a LineReader with a default value.static StringgetString(LineReader reader, String name, String def) Gets a string variable from a LineReader with a default value.static booleanisSet(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
-
createSecureTempFile
Creates a temporary file with owner-only read/write permissions.On POSIX file systems the file is created with
rw-------permissions (0600). The effective permissions are verified after creation; if they do not match, the file is deleted and anIOExceptionis thrown.On non-POSIX file systems (e.g. Windows NTFS), the file is created with an ACL that grants read, write, and delete access to the current user only. The ACL is applied atomically at creation time, eliminating any TOCTOU race between creation and restriction.
- Parameters:
prefix- the prefix for the temporary file namesuffix- the suffix for the temporary file name (may be null)- Returns:
- the created temporary
File - Throws:
IOException- if the file cannot be created or if the permissions cannot be restricted
-
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)
-