@Override public String convertToPresentation(LocalDateTime value, ValueContext context) { return DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss", context.getLocale().orElse(Locale.ENGLISH)) .format(value); }
@Override public String convertToPresentation(LocalDate value, ValueContext context) { return DateTimeFormatter .ofPattern("yyyy-MM-dd", context.getLocale().orElse(Locale.ENGLISH)) .format(value); }
@Override public String convertToPresentation(Date value, ValueContext context) { if (value == null) { return null; } return getFormat(context.getLocale().orElse(null)).format(value); }
@Override public String convertToPresentation(T value, ValueContext context) { if (value == null) { return null; } return getFormat(context.getLocale().orElse(null)).format(value); }
@Override public String convertToPresentation(Boolean value, ValueContext context) { if (value == null) { return null; } Locale locale = context.getLocale().orElse(null); if (value) { return getTrueString(locale); } else { return getFalseString(locale); } }
@Override public Result<LocalDate> convertToModel(String value, ValueContext context) { for (String pattern : new String[] { "yyyy-MM-dd", "yyyy-MM", "yyyy" }) { try { Locale effectiveLocale = context.getLocale() .orElse(Locale.ENGLISH); LocalDate date = DateTimeFormatter .ofPattern(pattern, effectiveLocale) .parse(value, LocalDate::from); return Result.ok(date); } catch (DateTimeParseException e) { // not parseable, ignore and try another format } } return Result.error("Could not parse date value: " + value); }
@Override public Result<LocalDateTime> convertToModel(String value, ValueContext context) { for (String pattern : new String[] { "yyyy-MM-dd HH:mm:ssZ", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM-dd", "yyyy-MM", "yyyy" }) { try { Locale effectiveLocale = context.getLocale() .orElse(Locale.ENGLISH); LocalDateTime date = DateTimeFormatter .ofPattern(pattern, effectiveLocale) .parse(value, LocalDateTime::from); return Result.ok(date); } catch (DateTimeParseException ignored) { // not parseable, ignore and try another format } } return Result.error("Could not parse date value: " + value); }
@Override public Result<Date> convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } // Remove leading and trailing white space value = value.trim(); ParsePosition parsePosition = new ParsePosition(0); Date parsedValue = getFormat(context.getLocale().orElse(null)) .parse(value, parsePosition); if (parsePosition.getIndex() != value.length()) { return Result.error("Could not convert '" + value); } return Result.ok(parsedValue); }
/** * Validates the given value as if it were the value of the bean property * configured for this validator. Returns {@code Result.ok} if there are no * JSR-303 constraint violations, a {@code Result.error} of chained * constraint violation messages otherwise. * <p> * Null values are accepted unless the property has an {@code @NotNull} * annotation or equivalent. * * @param value * the input value to validate * @param context * the value context for validation * @return the validation result */ @Override public ValidationResult apply(final Object value, ValueContext context) { Set<? extends ConstraintViolation<?>> violations = getJavaxBeanValidator() .validateValue(beanType, propertyName, value); Locale locale = context.getLocale().orElse(Locale.getDefault()); Optional<ValidationResult> result = violations.stream() .map(violation -> ValidationResult .error(getMessage(violation, locale))) .findFirst(); return result.orElse(ValidationResult.ok()); }
Number parsedValue = getFormat(context.getLocale().orElse(null)) .parse(value, parsePosition); if (parsePosition.getIndex() != value.length()) {
@Override public Result<Boolean> convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } // Remove leading and trailing white space value = value.trim(); Locale locale = context.getLocale().orElse(null); if (getTrueString(locale).equals(value)) { return Result.ok(true); } else if (getFalseString(locale).equals(value)) { return Result.ok(false); } else if (value.isEmpty()) { return Result.ok(null); } else { return Result.error(errorMessageProvider.apply(context)); } }
@Override public String convertToPresentation(T value, ValueContext context) { if (value != null) { return getNumberFormat(context.getLocale().orElse(null)).format(value); } return null; }
@Override public Result<T> convertToModel(String value, ValueContext context) { if (value != null && !value.trim().equals("")) { try { return Result.ok(ConversionUtils.convertNumberToTargetClass( getNumberFormat(context.getLocale().orElse(null)).parse(value.trim()), numberType)); } catch (@SuppressWarnings("unused") Exception e) { return Result.error("Could not convert '" + value + "' to " + numberType.getName()); } } return Result.ok(null); }