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.ObjectA 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 interfaceInputReader.ErrorMessagesProviderFunctional interface for providing error messagesstatic interfaceInputReader.InvalidIndexErrorMessagesProviderFunctional interface for providing error messages for invalid indexstatic classInputReader.ParseResult<T>A holder object returned by theparse(String)method, containing the parsed value and/or the error messages.static interfaceInputReader.ValueChecker<T>Functional interface for checking value constraints
-
Field Summary
Fields Modifier and Type Field Description protected TdefaultValuenull, if there is no default valueprotected java.util.function.BiFunction<T,T,java.lang.Boolean>equalsFuncThe function used to check whether two values are equal.protected booleaninlinePossibleValuesIf true, the list of possible values will be displayed on a single line.protected booleaninputMaskingIf true, the input will be masked (useful for example when reading passwords)protected booleaninputTrimmingIf true, the input will be trimmed.protected InputReader.InvalidIndexErrorMessagesProviderinvalidIndexErrorMessagesProviderThe provider of invalid index error messages.protected java.lang.StringitemNameThe name of the item corresponding to the value to be read.protected booleannumberedPossibleValuesIf true, the list of possible values will be numbered and the desired value will be selected by choosing the corresponding number.protected InputReader.ErrorMessagesProviderparseErrorMessagesProviderThe provider of parse error messages.protected java.util.List<T>possibleValuesNon-null and non-empty, if the value to be read must be chosen from a list of allowed values.protected booleanpromptAdjustmentsIf 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<?>>propertiesConfiguratorstatic java.lang.StringPROPS_PREFIX_ERROR_MESSAGEprotected java.util.function.Supplier<TextTerminal<?>>textTerminalSupplierSupplier ofTextTerminalsprotected java.util.List<InputReader.ValueChecker<T>>valueCheckersThe list of value checkers used to detect constraint violationsprotected java.util.function.Function<T,java.lang.String>valueFormatterThe formatter used when displaying values of type T.protected java.util.List<InputReader.ValueChecker<java.util.List<T>>>valueListCheckersThe 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 voidcheckConfiguration()Checks if the reader is correctly configured.protected <V> VexecuteWithTerminal(java.util.function.Function<TextTerminal<?>,V> action)protected java.lang.StringgetDefaultErrorMessage(java.lang.String sVal)Returns a generic error message.protected java.util.List<java.lang.String>getDefaultErrorMessages(java.lang.String s)If noparseErrorMessagesProviderexists, 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 booleanisPossibleValue(T val)booleanisValueListMode()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 voidprintPrompt(java.util.List<java.lang.String> prompt, TextTerminal<?> textTerminal)Displays a prompt inviting the user to enter a value.Tread(java.lang.String... prompt)Reads a value of type T.Tread(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.StringreadWithPrompt(TextTerminal<?> textTerminal, java.util.List<java.lang.String> prompt)BwithDefaultValue(T defaultValue)BwithEqualsFunc(java.util.function.BiFunction<T,T,java.lang.Boolean> equalsFunc)BwithInlinePossibleValues(java.util.List<T> possibleValues)BwithInlinePossibleValues(T... possibleValues)BwithInputMasking(boolean inputMasking)BwithInputTrimming(boolean inputTrimming)BwithInvalidIndexErrorMessagesProvider(InputReader.InvalidIndexErrorMessagesProvider invalidIndexErrorMessagesProvider)BwithItemName(java.lang.String itemName)BwithNumberedPossibleValues(java.util.List<T> possibleValues)BwithNumberedPossibleValues(T... possibleValues)BwithParseErrorMessagesProvider(InputReader.ErrorMessagesProvider parseErrorMessagesProvider)BwithPossibleValues(java.util.List<T> possibleValues)BwithPossibleValues(T... possibleValues)BwithPromptAdjustments(boolean promptAdjustment)BwithPropertiesConfigurator(java.util.function.Consumer<TerminalProperties<?>> propertiesConfigurator)Sets a configurator for InputReader-specific properties.BwithPropertiesPrefix(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.BwithValueChecker(InputReader.ValueChecker<T> valueChecker)Adds the valueChecker passed as argument.BwithValueFormatter(java.util.function.Function<T,java.lang.String> valueFormatter)BwithValueListChecker(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 ofTextTerminals
-
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.ParseResultthat 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.propertiescontains: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 noparseErrorMessagesProviderexists, 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 aparseErrorMessagesProviderexists, 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.ParseResultthat 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.IllegalArgumentExceptionChecks 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()
-
-