Package org.beryx.textio
Class InputReader<T,B extends InputReader<T,B>>
- java.lang.Object
-
- org.beryx.textio.InputReader<T,B>
-
- Type Parameters:
T
- the type of the values that can be read by this InputReaderB
- the type of this InputReader
- Direct Known Subclasses:
BooleanInputReader
,ComparableInputReader
,EnumInputReader
,GenericInputReader
,StringInputReader
public abstract class InputReader<T,B extends InputReader<T,B>> extends java.lang.Object
A reader for values of type T. It offers a fluent interface for configuring various settings such as input masking, possible values, default value, error messages etc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
InputReader.ErrorMessagesProvider
Functional interface for providing error messagesstatic interface
InputReader.InvalidIndexErrorMessagesProvider
Functional interface for providing error messages for invalid indexstatic class
InputReader.ParseResult<T>
A holder object returned by theparse(String)
method, containing the parsed value and/or the error messages.static interface
InputReader.ValueChecker<T>
Functional interface for checking value constraints
-
Field Summary
Fields Modifier and Type Field Description protected T
defaultValue
null, if there is no default valueprotected java.util.function.BiFunction<T,T,java.lang.Boolean>
equalsFunc
The function used to check whether two values are equal.protected boolean
inlinePossibleValues
If true, the list of possible values will be displayed on a single line.protected boolean
inputMasking
If true, the input will be masked (useful for example when reading passwords)protected boolean
inputTrimming
If true, the input will be trimmed.protected InputReader.InvalidIndexErrorMessagesProvider
invalidIndexErrorMessagesProvider
The provider of invalid index error messages.protected java.lang.String
itemName
The name of the item corresponding to the value to be read.protected boolean
numberedPossibleValues
If true, the list of possible values will be numbered and the desired value will be selected by choosing the corresponding number.protected InputReader.ErrorMessagesProvider
parseErrorMessagesProvider
The provider of parse error messages.protected java.util.List<T>
possibleValues
Non-null and non-empty, if the value to be read must be chosen from a list of allowed values.protected boolean
promptAdjustments
If true, the input reader is allowed to tweak the prompter, for example by including the default value or appending a colon at the end.protected java.util.function.Consumer<TerminalProperties<?>>
propertiesConfigurator
static java.lang.String
PROPS_PREFIX_ERROR_MESSAGE
protected java.util.function.Supplier<TextTerminal<?>>
textTerminalSupplier
Supplier ofTextTerminal
sprotected java.util.List<InputReader.ValueChecker<T>>
valueCheckers
The list of value checkers used to detect constraint violationsprotected java.util.function.Function<T,java.lang.String>
valueFormatter
The formatter used when displaying values of type T.protected java.util.List<InputReader.ValueChecker<java.util.List<T>>>
valueListCheckers
The list of value checkers used to detect constraint violations for a list of values
-
Constructor Summary
Constructors Constructor Description InputReader(java.util.function.Supplier<TextTerminal<?>> textTerminalSupplier)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
checkConfiguration()
Checks if the reader is correctly configured.protected <V> V
executeWithTerminal(java.util.function.Function<TextTerminal<?>,V> action)
protected java.lang.String
getDefaultErrorMessage(java.lang.String sVal)
Returns a generic error message.protected java.util.List<java.lang.String>
getDefaultErrorMessages(java.lang.String s)
If noparseErrorMessagesProvider
exists, this method is used to provide the list of error messages for the input strings
.protected java.util.List<java.lang.String>
getErrorMessages(java.lang.String s)
Provides the list of error messages for the input strings
.protected java.util.Optional<T>
getPossibleValue(T val)
protected boolean
isPossibleValue(T val)
boolean
isValueListMode()
static <T> InputReader.ValueChecker<java.util.List<T>>
noDuplicatesChecker()
static <T> InputReader.ValueChecker<java.util.List<T>>
nonEmptyListChecker()
protected abstract InputReader.ParseResult<T>
parse(java.lang.String s)
Parses the input stringprotected InputReader.ParseResult<T>
parseAndCheck(java.lang.String s)
Parses the input string and runs all value checkers in order to find constraint violations.protected void
printPrompt(java.util.List<java.lang.String> prompt, TextTerminal<?> textTerminal)
Displays a prompt inviting the user to enter a value.T
read(java.lang.String... prompt)
Reads a value of type T.T
read(java.util.List<java.lang.String> prompt)
Reads a value of type T.java.util.List<T>
readList(java.lang.String... prompt)
java.util.List<T>
readList(java.util.List<java.lang.String> prompt)
protected java.lang.String
readWithPrompt(TextTerminal<?> textTerminal, java.util.List<java.lang.String> prompt)
B
withDefaultValue(T defaultValue)
B
withEqualsFunc(java.util.function.BiFunction<T,T,java.lang.Boolean> equalsFunc)
B
withInlinePossibleValues(java.util.List<T> possibleValues)
B
withInlinePossibleValues(T... possibleValues)
B
withInputMasking(boolean inputMasking)
B
withInputTrimming(boolean inputTrimming)
B
withInvalidIndexErrorMessagesProvider(InputReader.InvalidIndexErrorMessagesProvider invalidIndexErrorMessagesProvider)
B
withItemName(java.lang.String itemName)
B
withNumberedPossibleValues(java.util.List<T> possibleValues)
B
withNumberedPossibleValues(T... possibleValues)
B
withParseErrorMessagesProvider(InputReader.ErrorMessagesProvider parseErrorMessagesProvider)
B
withPossibleValues(java.util.List<T> possibleValues)
B
withPossibleValues(T... possibleValues)
B
withPromptAdjustments(boolean promptAdjustment)
B
withPropertiesConfigurator(java.util.function.Consumer<TerminalProperties<?>> propertiesConfigurator)
Sets a configurator for InputReader-specific properties.B
withPropertiesPrefix(java.lang.String prefix)
Convenience method that callswithPropertiesConfigurator(Consumer)
with a configurator that takes all terminal properties with the given prefix and applies them after stripping the prefix from their keys.B
withValueChecker(InputReader.ValueChecker<T> valueChecker)
Adds the valueChecker passed as argument.B
withValueFormatter(java.util.function.Function<T,java.lang.String> valueFormatter)
B
withValueListChecker(InputReader.ValueChecker<java.util.List<T>> valueListChecker)
Adds the valueListChecker passed as argument.
-
-
-
Field Detail
-
PROPS_PREFIX_ERROR_MESSAGE
public static final java.lang.String PROPS_PREFIX_ERROR_MESSAGE
- See Also:
- Constant Field Values
-
textTerminalSupplier
protected final java.util.function.Supplier<TextTerminal<?>> textTerminalSupplier
Supplier ofTextTerminal
s
-
defaultValue
protected T defaultValue
null, if there is no default value
-
possibleValues
protected java.util.List<T> possibleValues
Non-null and non-empty, if the value to be read must be chosen from a list of allowed values.
-
numberedPossibleValues
protected boolean numberedPossibleValues
If true, the list of possible values will be numbered and the desired value will be selected by choosing the corresponding number.
-
inlinePossibleValues
protected boolean inlinePossibleValues
If true, the list of possible values will be displayed on a single line.
-
parseErrorMessagesProvider
protected InputReader.ErrorMessagesProvider parseErrorMessagesProvider
The provider of parse error messages. If null, thegetDefaultErrorMessages(String)
will be used.
-
invalidIndexErrorMessagesProvider
protected InputReader.InvalidIndexErrorMessagesProvider invalidIndexErrorMessagesProvider
The provider of invalid index error messages. If null, a default message will be used.
-
itemName
protected java.lang.String itemName
The name of the item corresponding to the value to be read. May be null.
-
inputMasking
protected boolean inputMasking
If true, the input will be masked (useful for example when reading passwords)
-
inputTrimming
protected boolean inputTrimming
If true, the input will be trimmed. Default: true
-
promptAdjustments
protected boolean promptAdjustments
If true, the input reader is allowed to tweak the prompter, for example by including the default value or appending a colon at the end. Default: true
-
valueCheckers
protected final java.util.List<InputReader.ValueChecker<T>> valueCheckers
The list of value checkers used to detect constraint violations
-
valueListCheckers
protected final java.util.List<InputReader.ValueChecker<java.util.List<T>>> valueListCheckers
The list of value checkers used to detect constraint violations for a list of values
-
valueFormatter
protected java.util.function.Function<T,java.lang.String> valueFormatter
The formatter used when displaying values of type T. Default: useString.valueOf(Object)
-
equalsFunc
protected java.util.function.BiFunction<T,T,java.lang.Boolean> equalsFunc
The function used to check whether two values are equal. Default:Objects.equals(Object, Object)
-
propertiesConfigurator
protected java.util.function.Consumer<TerminalProperties<?>> propertiesConfigurator
-
-
Constructor Detail
-
InputReader
public InputReader(java.util.function.Supplier<TextTerminal<?>> textTerminalSupplier)
-
-
Method Detail
-
parse
protected abstract InputReader.ParseResult<T> parse(java.lang.String s)
Parses the input string- Parameters:
s
- the input string- Returns:
- a
InputReader.ParseResult
that holds the parsed value and/or the error messages, if errors occurred.
-
withInputMasking
public B withInputMasking(boolean inputMasking)
-
withInputTrimming
public B withInputTrimming(boolean inputTrimming)
-
withPromptAdjustments
public B withPromptAdjustments(boolean promptAdjustment)
-
withItemName
public B withItemName(java.lang.String itemName)
-
withValueFormatter
public B withValueFormatter(java.util.function.Function<T,java.lang.String> valueFormatter)
-
withEqualsFunc
public B withEqualsFunc(java.util.function.BiFunction<T,T,java.lang.Boolean> equalsFunc)
-
withParseErrorMessagesProvider
public B withParseErrorMessagesProvider(InputReader.ErrorMessagesProvider parseErrorMessagesProvider)
-
withInvalidIndexErrorMessagesProvider
public B withInvalidIndexErrorMessagesProvider(InputReader.InvalidIndexErrorMessagesProvider invalidIndexErrorMessagesProvider)
-
withValueChecker
public B withValueChecker(InputReader.ValueChecker<T> valueChecker)
Adds the valueChecker passed as argument. May be called multiple times.
-
withValueListChecker
public B withValueListChecker(InputReader.ValueChecker<java.util.List<T>> valueListChecker)
Adds the valueListChecker passed as argument. May be called multiple times.
-
withPropertiesConfigurator
public B withPropertiesConfigurator(java.util.function.Consumer<TerminalProperties<?>> propertiesConfigurator)
Sets a configurator for InputReader-specific properties. The configurator will be applied to the terminal properties at the start of a read operation and the properties will be reverted to their previous values at the end of the read operation.
-
withPropertiesPrefix
public B withPropertiesPrefix(java.lang.String prefix)
Convenience method that callswithPropertiesConfigurator(Consumer)
with a configurator that takes all terminal properties with the given prefix and applies them after stripping the prefix from their keys.
For example, iftextio.properties
contains:textio.prompt.color = green textio.input.color = yellow textio.warn.prompt.color = red textio.warn.input.color = orange
then the following statement:textIO.newBooleanInputReader() .withPropertiesPrefix("warn") .read("Erase all data?");
will display the question in red and the user input in orange.
-
isValueListMode
public boolean isValueListMode()
- Returns:
- true, if currently reading a list of values via
readList(List)
-
getDefaultErrorMessage
protected java.lang.String getDefaultErrorMessage(java.lang.String sVal)
Returns a generic error message.
-
getDefaultErrorMessages
protected java.util.List<java.lang.String> getDefaultErrorMessages(java.lang.String s)
If noparseErrorMessagesProvider
exists, this method is used to provide the list of error messages for the input strings
. It should return a non-empty list of messages.
-
getErrorMessages
protected final java.util.List<java.lang.String> getErrorMessages(java.lang.String s)
Provides the list of error messages for the input strings
. If aparseErrorMessagesProvider
exists, it will be used. Otherwise,getDefaultErrorMessages(String)
will be called.
-
parseAndCheck
protected InputReader.ParseResult<T> parseAndCheck(java.lang.String s)
Parses the input string and runs all value checkers in order to find constraint violations.- Parameters:
s
- the input string- Returns:
- a
InputReader.ParseResult
that holds the parsed value and/or the error messages, if errors occurred.
-
read
public T read(java.lang.String... prompt)
Reads a value of type T. It repeatedly prompts the users to enter the value, until they provide a valid input string.- Parameters:
prompt
- the messages to be displayed for prompting the user to enter the value- Returns:
- the value of type T parsed from the input string
-
read
public T read(java.util.List<java.lang.String> prompt)
Reads a value of type T. It repeatedly prompts the user to enter a value, until a valid input string is provided.- Parameters:
prompt
- the list of messages to be displayed for prompting the user to enter the value- Returns:
- the value of type T parsed from the input string
-
readList
public java.util.List<T> readList(java.lang.String... prompt)
-
readList
public java.util.List<T> readList(java.util.List<java.lang.String> prompt)
-
readWithPrompt
protected java.lang.String readWithPrompt(TextTerminal<?> textTerminal, java.util.List<java.lang.String> prompt)
-
executeWithTerminal
protected <V> V executeWithTerminal(java.util.function.Function<TextTerminal<?>,V> action)
-
isPossibleValue
protected boolean isPossibleValue(T val)
-
checkConfiguration
protected void checkConfiguration() throws java.lang.IllegalArgumentException
Checks if the reader is correctly configured. This default implementation checks if the defaultValue is among the possibleValues.- Throws:
java.lang.IllegalArgumentException
-
printPrompt
protected void printPrompt(java.util.List<java.lang.String> prompt, TextTerminal<?> textTerminal)
Displays a prompt inviting the user to enter a value.- Parameters:
prompt
- the list of prompt messages. May be null.textTerminal
- the text terminal to which the messages are sent.
-
nonEmptyListChecker
public static <T> InputReader.ValueChecker<java.util.List<T>> nonEmptyListChecker()
-
noDuplicatesChecker
public static <T> InputReader.ValueChecker<java.util.List<T>> noDuplicatesChecker()
-
-