You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractValidator.java 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data.validator;
  17. import java.util.Objects;
  18. import com.vaadin.data.ValidationResult;
  19. import com.vaadin.data.Validator;
  20. import com.vaadin.server.SerializableFunction;
  21. /**
  22. * An abstract base class for typed validators.
  23. *
  24. * @param <T>
  25. * The value type
  26. * @author Vaadin Ltd.
  27. * @since 8.0
  28. */
  29. public abstract class AbstractValidator<T> implements Validator<T> {
  30. private final SerializableFunction<T, String> messageProvider;
  31. /**
  32. * Constructs a validator with the given error message. The substring "{0}"
  33. * is replaced by the value that failed validation.
  34. *
  35. * @param errorMessage
  36. * the message to be included in a failed result, not null
  37. */
  38. protected AbstractValidator(String errorMessage) {
  39. Objects.requireNonNull(errorMessage, "error message cannot be null");
  40. this.messageProvider = value -> errorMessage.replace("{0}",
  41. String.valueOf(value));
  42. }
  43. /**
  44. * Returns the error message for the given value.
  45. *
  46. * @param value
  47. * an invalid value
  48. * @return the formatted error message
  49. */
  50. protected String getMessage(T value) {
  51. return messageProvider.apply(value);
  52. }
  53. /**
  54. * A helper method for creating a {@code Result} from a value and a validity
  55. * flag. If the flag is true, returns {@code Result.ok}, otherwise yields
  56. * {@code Result.error} bearing the error message returned by
  57. * {@link #getMessage(T)}.
  58. * <p>
  59. * For instance, the following {@code apply} method only accepts even
  60. * numbers:
  61. *
  62. * <pre>
  63. * &#64;Override
  64. * public Result&lt;T&gt; apply(Integer value) {
  65. * return toResult(value, value % 2 == 0);
  66. * }
  67. * </pre>
  68. *
  69. * @param value
  70. * the validated value
  71. * @param isValid
  72. * whether the value is valid or not
  73. * @return the validation result
  74. */
  75. protected ValidationResult toResult(T value, boolean isValid) {
  76. return isValid ? ValidationResult.ok()
  77. : ValidationResult.error(getMessage(value));
  78. }
  79. }