@Override public Result<T> convertToModel(PRESENTATION value, ValueContext context) { Result<MODEL> model = Converter.this.convertToModel(value, context); return model.flatMap(v -> other.convertToModel(v, 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 <S> Result<S> flatMap(SerializableFunction<R, Result<S>> mapper) { Result<S> result = wrappedResult.flatMap(mapper); if (!(result instanceof ValidationResultWrap)) { return new ValidationResultWrap<S>(result, resultList); } List<ValidationResult> currentResults = new ArrayList<>(resultList); ValidationResultWrap<S> resultWrap = (ValidationResultWrap<S>) result; currentResults.addAll(resultWrap.getValidationResults()); return new ValidationResultWrap<>(resultWrap.getWrappedResult(), currentResults); }
@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)); }); }