@Override protected ParseResult<Character> parse(String s) { if(s == null || s.length() != 1) return new ParseResult<>(null, getErrorMessages(s)); return new ParseResult<>(s.charAt(0)); } }
/** * 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; }
@Override protected ParseResult<Boolean> parse(String s) { if(trueInput.equalsIgnoreCase(s)) return new ParseResult<>(true); if(falseInput.equalsIgnoreCase(s)) return new ParseResult<>(false); return new ParseResult<>(null, getErrorMessages(s)); }
@Override protected ParseResult<String> parse(String s) { return new ParseResult<>(s); }
@Override protected ParseResult<Double> parse(String s) { try { return new ParseResult<>(Double.parseDouble(s)); } catch (NumberFormatException e) { return new ParseResult<>(null, getErrorMessages(s)); } } }
@Override protected ParseResult<Long> parse(String s) { try { return new ParseResult<>(Long.parseLong(s)); } catch (NumberFormatException e) { return new ParseResult<>(null, getErrorMessages(s)); } } }
@Override protected ParseResult<Byte> parse(String s) { try { return new ParseResult<>(Byte.parseByte(s)); } catch (NumberFormatException e) { return new ParseResult<>(null, getErrorMessages(s)); } } }
@Override protected ParseResult<T> parse(String s) { T value = enumValues.get(s); if(value != null) return new ParseResult<>(value); return new ParseResult<T>(null, getErrorMessages(s)); } }
@Override protected ParseResult<Short> parse(String s) { try { return new ParseResult<>(Short.parseShort(s)); } catch (NumberFormatException e) { return new ParseResult<>(null, getErrorMessages(s)); } } }
@Override protected ParseResult<Float> parse(String s) { try { return new ParseResult<>(Float.parseFloat(s)); } catch (NumberFormatException e) { return new ParseResult<>(null, getErrorMessages(s)); } } }
@Override protected ParseResult<Integer> parse(String s) { try { return new ParseResult<>(Integer.parseInt(s)); } catch (NumberFormatException e) { return new ParseResult<>(null, getErrorMessages(s)); } } }