Class InputReader<T,​B extends InputReader<T,​B>>

  • Type Parameters:
    T - the type of the values that can be read by this InputReader
    B - 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.
    • 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 of TextTerminals
      • 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.
      • 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: use String.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.
      • withDefaultValue

        public B withDefaultValue​(T defaultValue)
      • withPossibleValues

        public B withPossibleValues​(T... possibleValues)
      • withPossibleValues

        public B withPossibleValues​(java.util.List<T> possibleValues)
      • withNumberedPossibleValues

        public B withNumberedPossibleValues​(T... possibleValues)
      • withNumberedPossibleValues

        public B withNumberedPossibleValues​(java.util.List<T> possibleValues)
      • withInlinePossibleValues

        public B withInlinePossibleValues​(T... possibleValues)
      • withInlinePossibleValues

        public B withInlinePossibleValues​(java.util.List<T> possibleValues)
      • 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)
      • 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 calls withPropertiesConfigurator(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, if textio.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 no 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.
      • getErrorMessages

        protected final java.util.List<java.lang.String> getErrorMessages​(java.lang.String s)
        Provides the list of error messages for the input string s. If a parseErrorMessagesProvider 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)
      • getPossibleValue

        protected java.util.Optional<T> getPossibleValue​(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.