/** * If no {@link #parseErrorMessagesProvider} exists, this method is used to provide the list of error messages for the input string <tt>s</tt>. * It should return a non-empty list of messages. */ protected List<String> getDefaultErrorMessages(String s) { return new ArrayList<>(Collections.singleton(getDefaultErrorMessage(s))); }
/** * Parses the input string and runs all value checkers in order to find constraint violations. * @param s the input string * @return a {@link ParseResult} that holds the parsed value and/or the error messages, if errors occurred. */ protected ParseResult<T> parseAndCheck(String s) { ParseResult<T> res = parse(s); if(res.errorMessages == null) { List<String> allErrors = new ArrayList<>(); for(ValueChecker<T> checker : valueCheckers) { List<String> errors = checker.getErrorMessages(res.value, itemName); if(errors != null) allErrors.addAll(errors); } if(!allErrors.isEmpty()) { allErrors.add(0, getDefaultErrorMessage(s)); res = new ParseResult<>(res.value, allErrors); } } return res; }
private T getValueFromIndex(String sVal, TextTerminal<?> textTerminal) { try { int optIndex = Integer.parseInt(sVal); if(optIndex > 0 && optIndex <= possibleValues.size()) { return possibleValues.get(optIndex - 1); } } catch (NumberFormatException e) { // Continue the execution. The next statement will print the error message. } textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> { textTerminal.print(getDefaultErrorMessage(sVal)); textTerminal.println(" Enter a value between 1 and " + possibleValues.size() + "."); }); textTerminal.println(); return null; }
allErrors.add(0, getDefaultErrorMessage(null)); textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t ->t.println(allErrors)); textTerminal.println();
private T getValueFromString(String sVal, TextTerminal<?> textTerminal) { ParseResult<T> result = parseAndCheck(sVal); List<String> errMessages = result.getErrorMessages(); if(errMessages == null) { Optional<T> value = getPossibleValue(result.getValue()); if(value.isPresent()) return value.get(); textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> { t.print(getDefaultErrorMessage(sVal)); if(inlinePossibleValues) { String options = possibleValues.stream() .map(val -> "'" + valueFormatter.apply(val) + "'") .collect(Collectors.joining(", ")); t.println(" Please enter one of: " + options + "."); } else { t.println(" Please enter one of the displayed values."); } }); textTerminal.println(); } else { textTerminal.executeWithPropertiesPrefix(PROPS_PREFIX_ERROR_MESSAGE, t -> t.println(errMessages)); textTerminal.println(); } return null; }