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.ErrorMessagesProvider
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. |
static interface |
InputReader.ValueChecker<T>
Functional interface for checking value constraints
|
Modifier and Type | Field and Description |
---|---|
protected T |
defaultValue
null, if there is no default value
|
protected BiFunction<T,T,Boolean> |
equalsFunc
The function used to check whether two values are equal.
|
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 InputReader.ErrorMessagesProvider |
parseErrorMessagesProvider
The provider of parse error messages.
|
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 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 String |
propertyName
The name of the property corresponding to the value to be read.
|
protected Supplier<TextTerminal> |
textTerminalSupplier
Supplier of s
|
protected List<InputReader.ValueChecker<T>> |
valueCheckers
The list of value checkers used to detect constraint violations
|
protected Function<T,String> |
valueFormatter
The formatter used when displaying values of type T.
|
protected List<InputReader.ValueChecker<List<T>>> |
valueListCheckers
The list of value checkers used to detect constraint violations for a list of values
|
Constructor and Description |
---|
InputReader(Supplier<TextTerminal> textTerminalSupplier) |
Modifier and Type | Method and Description |
---|---|
protected void |
checkConfiguration()
Checks if the reader is correctly configured.
|
protected String |
getDefaultErrorMessage(String sVal)
Returns a generic error message.
|
protected List<String> |
getDefaultErrorMessages(String s)
If no
parseErrorMessagesProvider exists, this method is used to provide the list of error messages for the input string s. |
protected List<String> |
getErrorMessages(String s)
Provides the list of error messages for the input string s.
|
protected boolean |
isPossibleValue(T val) |
boolean |
isValueListMode() |
static <T> InputReader.ValueChecker<List<T>> |
noDuplicatesChecker() |
static <T> InputReader.ValueChecker<List<T>> |
nonEmptyListChecker() |
protected abstract InputReader.ParseResult<T> |
parse(String s)
Parses the input string
|
protected InputReader.ParseResult<T> |
parseAndCheck(String s)
Parses the input string and runs all value checkers in order to find constraint violations.
|
protected void |
printPrompt(List<String> prompt,
TextTerminal textTerminal)
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.
|
List<T> |
readList(List<String> prompt) |
List<T> |
readList(String... prompt) |
B |
withDefaultValue(T defaultValue) |
B |
withEqualsFunc(BiFunction<T,T,Boolean> equalsFunc) |
B |
withInputMasking(boolean inputMasking) |
B |
withInputTrimming(boolean inputTrimming) |
B |
withNumberedPossibleValues(boolean numbered) |
B |
withParseErrorMessagesProvider(InputReader.ErrorMessagesProvider parseErrorMessagesProvider) |
B |
withPossibleValues(List<T> possibleValues) |
B |
withPossibleValues(T... possibleValues) |
B |
withPromptAdjustments(boolean promptAdjustment) |
B |
withPropertyName(String propertyName) |
B |
withValueChecker(InputReader.ValueChecker<T> valueChecker)
Adds the valueChecker passed as argument.
|
B |
withValueFormatter(Function<T,String> valueFormatter) |
B |
withValueListChecker(InputReader.ValueChecker<List<T>> valueListChecker)
Adds the valueListChecker passed as argument.
|
protected final Supplier<TextTerminal> textTerminalSupplier
protected T defaultValue
protected List<T> possibleValues
protected boolean numberedPossibleValues
protected InputReader.ErrorMessagesProvider parseErrorMessagesProvider
getDefaultErrorMessages(String)
will be used.protected String propertyName
protected boolean inputMasking
protected boolean inputTrimming
protected boolean promptAdjustments
protected final List<InputReader.ValueChecker<T>> valueCheckers
protected final List<InputReader.ValueChecker<List<T>>> valueListCheckers
protected Function<T,String> valueFormatter
String.valueOf(Object)
protected BiFunction<T,T,Boolean> equalsFunc
Objects.equals(Object, Object)
public InputReader(Supplier<TextTerminal> textTerminalSupplier)
protected 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 withPromptAdjustments(boolean promptAdjustment)
public B withEqualsFunc(BiFunction<T,T,Boolean> equalsFunc)
public B withParseErrorMessagesProvider(InputReader.ErrorMessagesProvider parseErrorMessagesProvider)
public B withValueChecker(InputReader.ValueChecker<T> valueChecker)
public B withValueListChecker(InputReader.ValueChecker<List<T>> valueListChecker)
public boolean isValueListMode()
readList(List)
protected String getDefaultErrorMessage(String sVal)
protected List<String> getDefaultErrorMessages(String s)
parseErrorMessagesProvider
exists, this method is used to provide the list of error messages for the input string s.
It should return a non-empty list of messages.protected final List<String> getErrorMessages(String s)
parseErrorMessagesProvider
exists, it will be used. Otherwise, getDefaultErrorMessages(String)
will be called.protected InputReader.ParseResult<T> parseAndCheck(String s)
s
- the input stringInputReader.ParseResult
that holds the parsed value and/or the error messages, if errors occurred.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 valueprotected boolean isPossibleValue(T val)
protected void checkConfiguration() throws IllegalArgumentException
IllegalArgumentException
protected void printPrompt(List<String> prompt, TextTerminal textTerminal)
prompt
- the list of prompt messages. May be null.textTerminal
- the text terminal to which the messages are sent.public static <T> InputReader.ValueChecker<List<T>> nonEmptyListChecker()
public static <T> InputReader.ValueChecker<List<T>> noDuplicatesChecker()