@Override public Result<Date> 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 { return Result.ok(new SimpleDateFormat(pattern).parse(value)); } catch (ParseException e) { // not parseable, ignore and try another format } } return Result.error("Could not parse date value: " + value); }
@Override public <X extends Throwable> R getOrThrow( SerializableFunction<String, ? extends X> exceptionProvider) throws X { return wrappedResult.getOrThrow(exceptionProvider); }
/** * Gets the validation result if status is either {@link Status#OK} or * {@link Status#ERROR} or an empty optional if status is * {@link Status#UNRESOLVED}. * * @return the validation result */ public Optional<ValidationResult> getResult() { if (result == null) { return Optional.empty(); } return Optional.of(result.isError() ? ValidationResult.error(result.getMessage().orElse("")) : ValidationResult.ok()); }
/** * If this Result has a value, returns a Result of applying the given * function to the value. Otherwise, returns a Result bearing the same error * as this one. Note that any exceptions thrown by the mapping function are * not wrapped but allowed to propagate. * * @param <S> * the type of the mapped value * @param mapper * the mapping function * @return the mapped result */ public default <S> Result<S> map(SerializableFunction<R, S> mapper) { return flatMap(value -> ok(mapper.apply(value))); }
@Override public Result<Integer> convertToModel(String value, ValueContext context) { Result<Number> n = convertToNumber(value, context); return n.flatMap(number -> { if (number == null) { return Result.ok(null); } int intValue = number.intValue(); if (intValue == number.longValue()) { // If the value of n is outside the range of long, the // return value of longValue() is either Long.MIN_VALUE or // Long.MAX_VALUE. The/ above comparison promotes int to // long and thus does not need to consider wrap-around. return Result.ok(intValue); } return Result.error(getErrorMessage(context)); }); }
@Override public Result<String> convertToModel(String value, ValueContext context) { return Result.ok(value); }
parsedDate.ifOk(v -> { if (!setValue(v, true) && !isDifferentValue(v)) { if (parsedDate.isError()) { dateString = null; currentErrorMessage = parsedDate.getMessage() .orElse("Parsing error");
/** * This method is called to handle a non-empty date string from the client * if the client could not parse it as a Date. * * By default, an error result is returned whose error message is * {@link #getParseErrorMessage()}. * * This can be overridden to handle conversions, to return a result with * {@code null} value (equivalent to empty input) or to return a custom * error. * * @param dateString * date string to handle * @return result that contains parsed Date as a value or an error */ protected Result<T> handleUnparsableDateString(String dateString) { return Result.error(getParseErrorMessage()); }
@Override public boolean isError() { return wrappedResult.isError(); }
@Override public Optional<String> getMessage() { return wrappedResult.getMessage(); }
/** * Write the field value by invoking the setter function on the given * bean, if the value passes all registered validators. * * @param bean * the bean to set the property value to */ private BindingValidationStatus<TARGET> writeFieldValue(BEAN bean) { assert bean != null; Result<TARGET> result = doConversion(); if (!isReadOnly()) { result.ifOk(value -> setter.accept(bean, value)); } return toValidationStatus(result); }
@Override public Result<Object> convertToModel(String value, ValueContext context) { return Result.ok(value); }
Result<LocalDate> parsedDate = handleUnparsableDateString( dateString); parsedDate.ifOk(v -> setValue(v, true)); if (parsedDate.isError()) { dateString = null; currentErrorMessage = parsedDate .getMessage().orElse("Parsing error");
@Override public Result<T> convertToModel(String value, ValueContext context) { return Result.error("Conversion to model is not supported"); }
this.status = result.isError() ? Status.ERROR : Status.OK; if (result instanceof ValidationResultWrap) { results = ((ValidationResultWrap<TARGET>) result)
/** * Gets error validation message if status is {@link Status#ERROR}. * * @return an optional validation error status or an empty optional if * status is not an error */ public Optional<String> getMessage() { if (getStatus() == Status.OK || result == null) { return Optional.empty(); } return result.getMessage(); }
@Override public Result<TYPE> convertToModel(String value, ValueContext context) { try { return Result.ok(type .cast(type.getMethod(this.staticMethodName, String.class) .invoke(null, value))); } catch (InvocationTargetException e) { return Result.error(e.getCause().getMessage()); } catch (Exception e) { return Result.error(e.getMessage()); } }
/** * Parses a given string as a value of given type. * * @param value * String value to convert. * @param type * Expected result type. * @return String converted to the expected result type using a registered * converter for that type. */ public <T> T parse(String value, Class<? extends T> type) { Converter<String, T> converter = findConverterFor(type); if (converter != null) { Result<T> result = converter.convertToModel(value, new ValueContext()); return result.getOrThrow(msg -> new IllegalArgumentException(msg)); } else { return null; } }
@SuppressWarnings("unchecked") @Override public Result<T> convertToModel(String value, ValueContext context) { if (value == null || value.trim().isEmpty()) { return Result.ok(null); } try { T result = (T) Enum.valueOf(type, value.toUpperCase(Locale.ROOT)); return Result.ok(result); } catch (Exception e) { return Result.error(e.getMessage()); } }