com.vaadin.flow.data.binder.Result Java Examples

The following examples show how to use com.vaadin.flow.data.binder.Result. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StringToUuidConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
public Result<UUID> convertToModel(String value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    // Remove leading and trailing white space
    value = value.trim();

    // Parse string as UUID.
    UUID uuid = null;
    try {
        uuid = UUID.fromString(value);
    } catch (java.lang.IllegalArgumentException e) {
        LoggerFactory.getLogger(StringToUuidConverter.class.getName()).warn(
                "Unable to convert String to UUID: " + value, e);
        return Result.error(this.errorMessageProvider.apply(context));
    }
    return Result.ok(uuid); // Return the UUID object, converted from String.
}
 
Example #2
Source File: AbstractStringToNumberConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the value to a Number using the given locale and
 * {@link #getFormat(Locale)}.
 *
 * @param value
 *            The value to convert
 * @param context
 *            The value context for conversion
 * @return The converted value
 */
protected Result<Number> convertToNumber(String value,
        ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    // Remove leading and trailing white space
    value = value.trim();

    // Parse and detect errors. If the full string was not used, it is
    // an error.
    ParsePosition parsePosition = new ParsePosition(0);
    Number parsedValue = getFormat(context.getLocale().orElse(null))
            .parse(value, parsePosition);
    if (parsePosition.getIndex() != value.length()) {
        return Result.error(getErrorMessage(context));
    }

    if (parsedValue == null) {
        // Convert "" to the empty value
        return Result.ok(emptyValue);
    }

    return Result.ok(parsedValue);
}
 
Example #3
Source File: StringToDateConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: StringToByteConverter.java    From crudui with Apache License 2.0 6 votes vote down vote up
@Override
public Result<Byte> convertToModel(String value, ValueContext context) {
    Result<Number> n = convertToNumber(value, context);
    return n.flatMap(number -> {
        if (number == null) {
            return Result.ok(null);
        } else {
            byte intValue = number.byteValue();
            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);
            } else {
                return Result.error(getErrorMessage(context));
            }
        }
    });
}
 
Example #5
Source File: StringToIntegerConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
@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);
        } else {
            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);
            } else {
                return Result.error(getErrorMessage(context));
            }
        }
    });
}
 
Example #6
Source File: StringToBooleanConverter.java    From flow with Apache License 2.0 6 votes vote down vote up
@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));
    }
}
 
Example #7
Source File: StringToBigIntegerConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<BigInteger> convertToModel(String value,
        ValueContext context) {
    return convertToNumber(value, context).map(number -> {
        if (number == null) {
            return null;
        }
        // Empty value will be a BigInteger
        if (number instanceof BigInteger) {
            return (BigInteger) number;
        }
        return ((BigDecimal) number).toBigInteger();
    });
}
 
Example #8
Source File: DateToSqlDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<java.sql.Date> convertToModel(Date value,
        ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    return Result.ok(new java.sql.Date(value.getTime()));
}
 
Example #9
Source File: StringToDoubleConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Double> convertToModel(String value, ValueContext context) {
    Result<Number> n = convertToNumber(value, context);

    return n.map(number -> {
        if (number == null) {
            return null;
        } else {
            return number.doubleValue();
        }
    });
}
 
Example #10
Source File: StringToFloatConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Float> convertToModel(String value, ValueContext context) {
    Result<Number> n = convertToNumber(value, context);

    return n.map(number -> {
        if (number == null) {
            return null;
        } else {
            return number.floatValue();
        }
    });
}
 
Example #11
Source File: LocalDateTimeToDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Date> convertToModel(LocalDateTime localDate, ValueContext context) {
    if (localDate == null) {
        return Result.ok(null);
    }

    return Result.ok(Date.from(localDate.atZone(zoneId).toInstant()));
}
 
Example #12
Source File: StringToLongConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Long> convertToModel(String value, ValueContext context) {
    Result<Number> n = convertToNumber(value, context);
    return n.map(number -> {
        if (number == null) {
            return null;
        } else {
            return number.longValue();
        }
    });
}
 
Example #13
Source File: LocalDateToDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Date> convertToModel(LocalDate localDate,
        ValueContext context) {
    if (localDate == null) {
        return Result.ok(null);
    }

    return Result.ok(Date.from(localDate.atStartOfDay(zoneId).toInstant()));
}
 
Example #14
Source File: DoubleToIntegerConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<Integer> convertToModel(Double value, ValueContext context) {
    try {
        if (value == null) {
            return Result.ok(null);
        }
        return Result.ok(value.intValue());
    } catch (Exception ex) {
        return Result.error(errorMessage);
    }
}
 
Example #15
Source File: DateToLongConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Long> convertToModel(Date value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    return Result.ok(value.getTime());
}
 
Example #16
Source File: StringToCharacterConverter.java    From crudui with Apache License 2.0 5 votes vote down vote up
@Override
public Result<Character> convertToModel(String value, ValueContext context) {
    if (value == null) {
        return Result.ok(null);
    }

    if (value.length() > 1) {
        return Result.error("Could not convert '" + value);
    }

    return Result.ok(value.charAt(0));
}
 
Example #17
Source File: RadiusGroupDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<String> convertToModel(RadiusGroupDto value, ValueContext context) {
    if (value == null) {
        return Result.ok("");
    }
    radiusGroupDto = value;
    return Result.ok(value.getName());
}
 
Example #18
Source File: AttributeDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<String> convertToModel(T value, ValueContext context) {
    if (value == null) {
        return Result.ok("");
    }
    attributeDto = value;
    return Result.ok(value.getName());
}
 
Example #19
Source File: RadiusUserDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Result<String> convertToModel(RadiusUserDto value, ValueContext context) {
    if (value == null) {
        return Result.ok("");
    }
    radiusUserDto = value;
    return Result.ok(value.getUsername());
}
 
Example #20
Source File: StringToBigDecimalConverter.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public Result<BigDecimal> convertToModel(String value,
        ValueContext context) {
    return convertToNumber(value, context)
            .map(number -> (BigDecimal) number);
}
 
Example #21
Source File: Converter.java    From flow with Apache License 2.0 3 votes vote down vote up
/**
 * Constructs a converter from two functions. Any {@code Exception}
 * instances thrown from the {@code toModel} function are converted into
 * error-bearing {@code Result} objects using the given {@code onError}
 * function.
 *
 * @param <P>
 *            the presentation type
 * @param <M>
 *            the model type
 * @param toModel
 *            the function to convert to model
 * @param toPresentation
 *            the function to convert to presentation
 * @param onError
 *            the function to provide error messages
 * @return the new converter
 *
 * @see Result
 * @see Function
 */
static <P, M> Converter<P, M> from(SerializableFunction<P, M> toModel,
        SerializableFunction<M, P> toPresentation,
        SerializableFunction<Exception, String> onError) {

    return from(val -> Result.of(() -> toModel.apply(val), onError),
            toPresentation);
}
 
Example #22
Source File: Converter.java    From flow with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a converter that returns its input as-is in both directions.
 *
 * @param <T>
 *            the input and output type
 * @return an identity converter
 */
static <T> Converter<T, T> identity() {
    return from(t -> Result.ok(t), t -> t);
}
 
Example #23
Source File: Converter.java    From flow with Apache License 2.0 2 votes vote down vote up
/**
 * Converts the given value from presentation type to model type.
 * <p>
 * A converter can optionally use locale to do the conversion.
 *
 * @param value
 *            The value to convert. Can be null
 * @param context
 *            The value context for the conversion.
 * @return The converted value compatible with the source type
 */
Result<MODEL> convertToModel(PRESENTATION value, ValueContext context);