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 java.lang.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 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 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.Supplier<TextTerminal<?>> |
textTerminalSupplier
Supplier of
TextTerminal s |
protected java.util.List<InputReader.ValueChecker<T>> |
valueCheckers
The list of value checkers used to detect constraint violations
|
protected 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 and Description |
---|
InputReader(java.util.function.Supplier<TextTerminal<?>> textTerminalSupplier) |
Modifier and Type | Method and Description |
---|---|
protected void |
checkConfiguration()
Checks if the reader is correctly configured.
|
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 no
parseErrorMessagesProvider exists, this method is used to provide the list of error messages for the input string s. |
protected java.util.List<java.lang.String> |
getErrorMessages(java.lang.String s)
Provides the list of error messages for the input string s.
|
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 string
|
protected 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.util.List<java.lang.String> prompt)
Reads a value of type T.
|
T |
read(java.lang.String... prompt)
Reads a value of type T.
|
java.util.List<T> |
readList(java.util.List<java.lang.String> prompt) |
java.util.List<T> |
readList(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 |
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 |
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.
|
protected final java.util.function.Supplier<TextTerminal<?>> textTerminalSupplier
TextTerminal
sprotected T defaultValue
protected java.util.List<T> possibleValues
protected boolean numberedPossibleValues
protected boolean inlinePossibleValues
protected InputReader.ErrorMessagesProvider parseErrorMessagesProvider
getDefaultErrorMessages(String)
will be used.protected java.lang.String itemName
protected boolean inputMasking
protected boolean inputTrimming
protected boolean promptAdjustments
protected final java.util.List<InputReader.ValueChecker<T>> valueCheckers
protected final java.util.List<InputReader.ValueChecker<java.util.List<T>>> valueListCheckers
protected java.util.function.Function<T,java.lang.String> valueFormatter
String.valueOf(Object)
public InputReader(java.util.function.Supplier<TextTerminal<?>> textTerminalSupplier)
protected abstract InputReader.ParseResult<T> parse(java.lang.String s)
s
- the input stringInputReader.ParseResult
that holds the parsed value and/or the error messages, if errors occurred.public B withInputMasking(boolean inputMasking)
public B withInputTrimming(boolean inputTrimming)
public B withPromptAdjustments(boolean promptAdjustment)
public B withItemName(java.lang.String itemName)
public B withValueFormatter(java.util.function.Function<T,java.lang.String> valueFormatter)
public B withEqualsFunc(java.util.function.BiFunction<T,T,java.lang.Boolean> equalsFunc)
public B withParseErrorMessagesProvider(InputReader.ErrorMessagesProvider parseErrorMessagesProvider)
public B withValueChecker(InputReader.ValueChecker<T> valueChecker)
public B withValueListChecker(InputReader.ValueChecker<java.util.List<T>> valueListChecker)
public boolean isValueListMode()
readList(List)
protected java.lang.String getDefaultErrorMessage(java.lang.String sVal)
protected java.util.List<java.lang.String> getDefaultErrorMessages(java.lang.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 java.util.List<java.lang.String> getErrorMessages(java.lang.String s)
parseErrorMessagesProvider
exists, it will be used. Otherwise, getDefaultErrorMessages(String)
will be called.protected InputReader.ParseResult<T> parseAndCheck(java.lang.String s)
s
- the input stringInputReader.ParseResult
that holds the parsed value and/or the error messages, if errors occurred.public T read(java.lang.String... prompt)
prompt
- the messages to be displayed for prompting the user to enter the valuepublic T read(java.util.List<java.lang.String> prompt)
prompt
- the list of messages to be displayed for prompting the user to enter the valuepublic java.util.List<T> readList(java.lang.String... prompt)
public java.util.List<T> readList(java.util.List<java.lang.String> prompt)
protected boolean isPossibleValue(T val)
protected void checkConfiguration() throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
protected void printPrompt(java.util.List<java.lang.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<java.util.List<T>> nonEmptyListChecker()
public static <T> InputReader.ValueChecker<java.util.List<T>> noDuplicatesChecker()