@Override public Result<String> convertToModel(String value, ValueContext context) { return Result.ok(value); }
@Override public Result<Object> convertToModel(String value, ValueContext context) { return Result.ok(value); }
@Override public Result<Character> convertToModel(String value, ValueContext context) { return Result.ok(value.charAt(0)); }
@Override public Result<Boolean> convertToModel(String value, ValueContext context) { return Result.ok(!value.equalsIgnoreCase("false")); }
@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()); } }
/** * Returns a converter that returns its input as-is in both directions. * * @param <T> * the input and output type * @return an identity converter */ public static <T> Converter<T, T> identity() { return from(t -> Result.ok(t), t -> t); }
@Override public Result<FIELDVALUE> convertToModel(FIELDVALUE value, ValueContext context) { if (delegate == null) { return Result.ok(value); } else { return delegate.convertToModel(value, context); } }
@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 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()); } }
@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); }
@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 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)); }); }
/** * 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<Resource> convertToModel(String value, ValueContext context) { if (!value.contains("://")) { // assume it'is "file://" protocol, one that is used to access a // file on a given path on the server, this will later be striped // out anyway value = "file://" + value; } String protocol = value.split("://")[0]; try { ResourceConverterByProtocol converter = ResourceConverterByProtocol .valueOf(protocol.toUpperCase(Locale.ROOT)); return Result.ok(converter.parse(value)); } catch (IllegalArgumentException iae) { return Result.error("Unrecognized protocol: " + protocol); } }
/** * Creates a new status change event. * <p> * The {@code message} must be {@code null} if the {@code status} is * {@link Status#OK}. * * @param source * field whose status has changed, not {@code null} * @param status * updated status value, not {@code null} * @param result * the related result, may be {@code null} */ @Deprecated public BindingValidationStatus(Binding<?, TARGET> source, Status status, ValidationResult result) { this(result.isError() ? Result.error(result.getErrorMessage()) : Result.ok(null), source); }