Skip to content

Commit

Permalink
issue #24: allow customizing the error messages associated with numbe…
Browse files Browse the repository at this point in the history
…red options
  • Loading branch information
siordache committed Dec 3, 2019
1 parent 0ef2ca4 commit 787a2df
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions text-io/src/main/java/org/beryx/textio/InputReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public interface ErrorMessagesProvider {
List<String> getErrorMessages(String sVal, String itemName);
}

/** Functional interface for providing error messages for invalid index */
@FunctionalInterface
public interface InvalidIndexErrorMessagesProvider {
/**
* Returns the list of error messages for the given string representation of the value
* @param sVal the string representation of the index entered by the user
* @param itemName the name of the item corresponding to this value. May be null.
* @param minIndex the minimum value allowed for the index
* @param maxIndex the maximum value allowed for the index
* @return - the list of error messages or null if no error has been detected.
*/
List<String> getErrorMessages(String sVal, String itemName, int minIndex, int maxIndex);
}

/** Functional interface for checking value constraints */
@FunctionalInterface
public interface ValueChecker<T> {
Expand Down Expand Up @@ -110,6 +124,9 @@ public List<String> getErrorMessages() {
/** The provider of parse error messages. If null, the {@link #getDefaultErrorMessages(String)} will be used. */
protected ErrorMessagesProvider parseErrorMessagesProvider;

/** The provider of invalid index error messages. If null, a default message will be used. */
protected InvalidIndexErrorMessagesProvider invalidIndexErrorMessagesProvider;

/** The name of the item corresponding to the value to be read. May be null. */
protected String itemName;

Expand Down Expand Up @@ -245,6 +262,12 @@ public B withParseErrorMessagesProvider(ErrorMessagesProvider parseErrorMessages
return (B)this;
}

@SuppressWarnings("unchecked")
public B withInvalidIndexErrorMessagesProvider(InvalidIndexErrorMessagesProvider invalidIndexErrorMessagesProvider) {
this.invalidIndexErrorMessagesProvider = invalidIndexErrorMessagesProvider;
return (B)this;
}

/** Adds the valueChecker passed as argument. May be called multiple times. */
@SuppressWarnings("unchecked")
public B withValueChecker(ValueChecker<T> valueChecker) {
Expand Down Expand Up @@ -501,8 +524,12 @@ private T getValueFromIndex(String sVal, TextTerminal<?> textTerminal) {
// 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() + ".");
if(invalidIndexErrorMessagesProvider != null) {
textTerminal.println(invalidIndexErrorMessagesProvider.getErrorMessages(sVal, itemName, 1, possibleValues.size()));
} else {
textTerminal.print(getDefaultErrorMessage(sVal));
textTerminal.println(" Enter a value between 1 and " + possibleValues.size() + ".");
}
});
textTerminal.println();
return null;
Expand Down

0 comments on commit 787a2df

Please sign in to comment.