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

The following examples show how to use com.vaadin.flow.data.binder.ValueContext. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
Source File: AbstractStringToNumberConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public String convertToPresentation(T value, ValueContext context) {
    if (value == null) {
        return null;
    }

    return getFormat(context.getLocale().orElse(null)).format(value);
}
 
Example #8
Source File: LocalDateTimeToDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDateTime convertToPresentation(Date date, ValueContext context) {
    if (date == null) {
        return null;
    }

    return Instant.ofEpochMilli(date.getTime()).atZone(zoneId).toLocalDateTime();
}
 
Example #9
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 #10
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 #11
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 #12
Source File: DateToSqlDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public Date convertToPresentation(java.sql.Date value,
        ValueContext context) {
    if (value == null) {
        return null;
    }

    return new Date(value.getTime());
}
 
Example #13
Source File: StringToDateConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public String convertToPresentation(Date value, ValueContext context) {
    if (value == null) {
        return null;
    }

    return getFormat(context.getLocale().orElse(null)).format(value);
}
 
Example #14
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 #15
Source File: StringToUuidConverter.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public String convertToPresentation(UUID value, ValueContext context) {
    if (value == null) {
        return null;
    }
    // `java.util.UUID::toString` generates a textual representation of a
    // UUID’s 128-bits as in a  hexadecimal `String` in 32-character canonical
    // format with four hyphens separating groups of digits.
    // https://docs.oracle.com/javase/10/docs/api/java/util/UUID.html#toString()
    return value.toString();
}
 
Example #16
Source File: StringLengthValidator.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult apply(String value, ValueContext context) {
    if (value == null) {
        return toResult(value, true);
    }
    ValidationResult lengthCheck = validator.apply(value.length(), context);
    return toResult(value, !lengthCheck.isError());
}
 
Example #17
Source File: NotEmptyValidatorTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void nullValueIsDisallowed() {
    NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo");
    ValidationResult result = validator.apply(null, new ValueContext());
    Assert.assertTrue(result.isError());
    Assert.assertEquals("foo", result.getErrorMessage());
}
 
Example #18
Source File: NotEmptyValidatorTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyValueIsDisallowed() {
    NotEmptyValidator<String> validator = new NotEmptyValidator<>("foo");
    ValidationResult result = validator.apply("", new ValueContext());
    Assert.assertTrue(result.isError());
    Assert.assertEquals("foo", result.getErrorMessage());
}
 
Example #19
Source File: NotEmptyValidatorTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void nonNullValueIsAllowed() {
    NotEmptyValidator<Object> validator = new NotEmptyValidator<>("foo");
    Object value = new Object();
    ValidationResult result = validator.apply(value, new ValueContext());
    Assert.assertFalse(result.isError());
    Assert.assertFalse(result.isError());
}
 
Example #20
Source File: ValidatorTestBase.java    From flow with Apache License 2.0 5 votes vote down vote up
protected <T> void assertPasses(T value, Validator<? super T> validator) {
    ValidationResult result = validator.apply(value, new ValueContext());
    if (result.isError()) {
        Assert.fail(value + " should pass " + validator + " but got "
                + result.getErrorMessage());
    }
}
 
Example #21
Source File: ValidatorTestBase.java    From flow with Apache License 2.0 5 votes vote down vote up
protected <T> void assertFails(T value, String errorMessage,
        Validator<? super T> validator) {
    ValidationResult result = validator.apply(value,
            new ValueContext(localeContext));
    Assert.assertTrue(result.isError());
    Assert.assertEquals(errorMessage, result.getErrorMessage());
}
 
Example #22
Source File: NotEmptyValidator.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult apply(T value, ValueContext context) {
    if (Objects.isNull(value) || Objects.equals(value, "")) {
        return ValidationResult.error(message);
    } else {
        return ValidationResult.ok();
    }
}
 
Example #23
Source File: StringToCharacterConverter.java    From crudui with Apache License 2.0 5 votes vote down vote up
@Override
public String convertToPresentation(Character value, ValueContext context) {
    if (value == null) {
        return null;
    }

    return value.toString();
}
 
Example #24
Source File: DoubleToIntegerConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public Double convertToPresentation(Integer value, ValueContext context) {
    if (value == null) {
        return null;
    }
    return Double.valueOf(value);
}
 
Example #25
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 #26
Source File: RadiusUserDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public RadiusUserDto convertToPresentation(String value, ValueContext context) {
    if (value != null) {
        radiusUserDto.setUsername(value);
    }
    return radiusUserDto;
}
 
Example #27
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 #28
Source File: AttributeDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public T convertToPresentation(String value, ValueContext context) {
    if (value != null) {
        attributeDto.setName(value);
    }
    return attributeDto;
}
 
Example #29
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 #30
Source File: RadiusGroupDtoToNameConverter.java    From radman with MIT License 5 votes vote down vote up
@Override
public RadiusGroupDto convertToPresentation(String value, ValueContext context) {
    if (value != null) {
        radiusGroupDto.setName(value);
    }
    return radiusGroupDto;
}