Procházet zdrojové kódy

Remove exceptions logging from Validator

Change-Id: I2ea2ce10e53ccd228ba2c8c5d1e9cd1589c5e692
feature/vaadin8
elmot před 7 roky
rodič
revize
d601f8b158
1 změnil soubory, kde provedl 22 přidání a 29 odebrání
  1. 22
    29
      server/src/main/java/com/vaadin/tokka/data/Validator.java

+ 22
- 29
server/src/main/java/com/vaadin/tokka/data/Validator.java Zobrazit soubor

import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;


import com.vaadin.tokka.data.util.Result; import com.vaadin.tokka.data.util.Result;


* failed the validation. * failed the validation.
* <p> * <p>
* For instance, the following validator checks if a number is positive: * For instance, the following validator checks if a number is positive:
*
* </p>
*
* <pre> * <pre>
* Validator&lt;Integer&gt; v = num -> { * Validator&lt;Integer&gt; v = num -> {
* if (num >= 0) * if (num >= 0)
* return Result.error("number must be positive"); * return Result.error("number must be positive");
* }; * };
* </pre> * </pre>
*
*
* @author Vaadin Ltd. * @author Vaadin Ltd.
* *
* @param <T> * @param <T>
* the type of the value to validate
*
* the type of the value to validate
*
* @see Result * @see Result
*
*
* @since * @since
*/ */
@FunctionalInterface @FunctionalInterface


/** /**
* Returns a validator that passes any value. * Returns a validator that passes any value.
*
*
* @param <T> * @param <T>
* the value type
* the value type
* @return an always-passing validator * @return an always-passing validator
*/ */
public static <T> Validator<T> alwaysPass() { public static <T> Validator<T> alwaysPass() {
* <p> * <p>
* For instance, the following chained validator checks if a number is * For instance, the following chained validator checks if a number is
* between 0 and 10, inclusive: * between 0 and 10, inclusive:
*
* </p>
* <pre> * <pre>
* Validator&lt;Integer&gt; v = Validator * Validator&lt;Integer&gt; v = Validator
* .from(num -> num >= 0, "number must be >= 0") * .from(num -> num >= 0, "number must be >= 0")
* .chain(Validator.from(num -> num <= 10, "number must be <= 10")); * .chain(Validator.from(num -> num <= 10, "number must be <= 10"));
* </pre> * </pre>
*
*
* @param next * @param next
* the validator to apply next, not null
* the validator to apply next, not null
* @return a chained validator * @return a chained validator
*
*
* @see #from(Predicate, String) * @see #from(Predicate, String)
*/ */
public default Validator<T> chain(Function<T, Result<T>> next) { public default Validator<T> chain(Function<T, Result<T>> next) {
* the outcome of the validation: either {@link Result#ok(Object) Result.ok} * the outcome of the validation: either {@link Result#ok(Object) Result.ok}
* if the value passed validation or {@link Result#error(String) * if the value passed validation or {@link Result#error(String)
* Result.error} otherwise. * Result.error} otherwise.
*
*
* @param value * @param value
* the input value to validate
* the input value to validate
* @return the validation result * @return the validation result
*/ */
@Override @Override
* <p> * <p>
* For instance, the following validator checks if a number is between 0 and * For instance, the following validator checks if a number is between 0 and
* 10, inclusive: * 10, inclusive:
*
* </p>
* <pre> * <pre>
* Validator&lt;Integer&gt; v = Validator.from( * Validator&lt;Integer&gt; v = Validator.from(
* num -> num >= 0 && num <= 10, * num -> num >= 0 && num <= 10,
* "number must be between 0 and 10"); * "number must be between 0 and 10");
* </pre> * </pre>
*
*
* @param <T> * @param <T>
* the value type
* the value type
* @param guard * @param guard
* the function used to validate, not null
* the function used to validate, not null
* @param errorMessage * @param errorMessage
* the message returned if validation fails, not null
* the message returned if validation fails, not null
* @return the new validator using the function * @return the new validator using the function
*/ */
public static <T> Validator<T> from(Predicate<T> guard, public static <T> Validator<T> from(Predicate<T> guard,
Objects.requireNonNull(guard, "guard cannot be null"); Objects.requireNonNull(guard, "guard cannot be null");
Objects.requireNonNull(errorMessage, "errorMessage cannot be null"); Objects.requireNonNull(errorMessage, "errorMessage cannot be null");
return value -> { return value -> {
try {
if (guard.test(value)) {
return Result.ok(value);
} else {
return Result.error(errorMessage);
}
} catch (Exception e) {
Logger.getLogger(Validator.class.getName()).log(Level.FINE,
errorMessage, e);
if (guard.test(value)) {
return Result.ok(value);
} else {
return Result.error(errorMessage); return Result.error(errorMessage);
} }
}; };

Načítá se…
Zrušit
Uložit