T
- the type of the values that can be read by this InputReaderB
- the type of this InputReaderpublic abstract class InputReader<T,B extends InputReader<T,B>> extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
InputReader.ErrorMessageProvider
Functional interface for providing error messages
|
static class |
InputReader.ParseResult<T>
A holder object returned by the
parse(String) method, containing the parsed value and/or the error messages. |
Modifier and Type | Field and Description |
---|---|
protected T |
defaultValue
null, if there is no default value
|
protected InputReader.ErrorMessageProvider |
errorMessageProvider
The provider of error messages.
|
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 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 List<T> |
possibleValues
Non-null and non-empty, if the value to be read must be chosen from a list of allowed values.
|
protected String |
propertyName
The name of the property corresponding to the value to be read.
|
protected Supplier<TextTerminal> |
textTerminalSupplier
Supplier of s
|
protected Function<T,String> |
valueFormatter
The formatter used when displaying values of type T.
|
Constructor and Description |
---|
InputReader(Supplier<TextTerminal> textTerminalSupplier) |
Modifier and Type | Method and Description |
---|---|
void |
checkConfiguration()
Checks if the reader is correctly configured.
|
protected String |
getDefaultErrorMessage()
Returns a generic error message.
|
protected List<String> |
getDefaultErrorMessage(String s)
If no errorMessageProvider exists, this method is used to provide the list of error messages for the input string s.
|
List<String> |
getErrorMessage(String s)
Provides the list of error messages for the input string s.
|
abstract InputReader.ParseResult<T> |
parse(String s)
Parses the input string
|
protected void |
printPrompt(List<String> prompt,
TextTerminal textTerminal,
List<T> options)
Displays a prompt inviting the user to enter a value.
|
T |
read(List<String> prompt)
Reads a value of type T.
|
T |
read(String... prompt)
Reads a value of type T.
|
B |
withDefaultValue(T defaultValue) |
B |
withErrorMessageProvider(InputReader.ErrorMessageProvider errorMessageProvider) |
B |
withInputMasking(boolean inputMasking) |
B |
withInputTrimming(boolean inputTrimming) |
B |
withNumberedPossibleValues(boolean numbered) |
B |
withPossibleValues(List<T> possibleValues) |
B |
withPossibleValues(T... possibleValues) |
B |
withPropertyName(String propertyName) |
B |
withValueFormatter(Function<T,String> valueFormatter) |
protected final Supplier<TextTerminal> textTerminalSupplier
protected T defaultValue
protected List<T> possibleValues
protected boolean numberedPossibleValues
protected InputReader.ErrorMessageProvider errorMessageProvider
getDefaultErrorMessage(String)
will be used.protected String propertyName
protected boolean inputMasking
protected boolean inputTrimming
protected Function<T,String> valueFormatter
String.valueOf(Object)
public InputReader(Supplier<TextTerminal> textTerminalSupplier)
public abstract InputReader.ParseResult<T> parse(String s)
s
- the input stringInputReader.ParseResult
that holds the parsed value and/or the error messages, if errors occurred.public B withNumberedPossibleValues(boolean numbered)
public B withInputMasking(boolean inputMasking)
public B withInputTrimming(boolean inputTrimming)
public B withErrorMessageProvider(InputReader.ErrorMessageProvider errorMessageProvider)
protected String getDefaultErrorMessage()
protected List<String> getDefaultErrorMessage(String s)
public final List<String> getErrorMessage(String s)
getDefaultErrorMessage(String)
will be called.public T read(String... prompt)
prompt
- the messages to be displayed for prompting the user to enter the valuepublic T read(List<String> prompt)
prompt
- the list of messages to be displayed for prompting the user to enter the valuepublic void checkConfiguration() throws IllegalArgumentException
IllegalArgumentException
protected void printPrompt(List<String> prompt, TextTerminal textTerminal, List<T> options)
prompt
- the list of prompt messages. May be null.textTerminal
- the text terminal to which the messages are sent.options
- the list of options from which the user can choose a value. May be null.