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.

ValidationStatus.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2000-2016 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;
  17. import java.io.Serializable;
  18. import java.util.Objects;
  19. import java.util.Optional;
  20. import com.vaadin.data.Binder.Binding;
  21. import com.vaadin.data.Binder.BindingBuilder;
  22. /**
  23. * Represents the status of field validation. Status can be {@code Status.OK},
  24. * {@code Status.ERROR} or {@code Status.RESET}. Status OK and ERROR are always
  25. * associated with a ValidationResult {@link #getResult}.
  26. * <p>
  27. * Use
  28. * {@link BindingBuilder#withValidationStatusHandler(ValidationStatusHandler)}
  29. * to register a handler for field level validation status changes.
  30. *
  31. * @author Vaadin Ltd
  32. *
  33. * @param <TARGET>
  34. * the target data type of the binding for which the validation
  35. * status changed, matches the field type unless a converter has been
  36. * set
  37. *
  38. * @see BindingBuilder#withValidationStatusHandler(ValidationStatusHandler)
  39. * @see Binding#validate()
  40. * @see ValidationStatusHandler
  41. * @see BinderValidationStatus
  42. *
  43. * @since 8.0
  44. */
  45. public class ValidationStatus<TARGET> implements Serializable {
  46. /**
  47. * Status of the validation.
  48. * <p>
  49. * The status is the part of {@link ValidationStatus} which indicates
  50. * whether the validation failed or not, or whether it is in unresolved
  51. * state (e.g. after clear or reset).
  52. */
  53. public enum Status {
  54. /** Validation passed. */
  55. OK,
  56. /** Validation failed. */
  57. ERROR,
  58. /**
  59. * Unresolved status, e.g field has not yet been validated because value
  60. * was cleared.
  61. * <p>
  62. * In practice this status means that the value might be invalid, but
  63. * validation errors should be hidden.
  64. */
  65. UNRESOLVED;
  66. }
  67. private final Status status;
  68. private final ValidationResult result;
  69. private final Binding<?, TARGET> binding;
  70. /**
  71. * Convenience method for creating a {@link Status#UNRESOLVED} validation
  72. * status for the given binding.
  73. *
  74. * @param source
  75. * the source binding
  76. * @return unresolved validation status
  77. * @param <TARGET>
  78. * the target data type of the binding for which the validation
  79. * status was reset
  80. */
  81. public static <TARGET> ValidationStatus<TARGET> createUnresolvedStatus(
  82. Binding<?, TARGET> source) {
  83. return new ValidationStatus<>(source, Status.UNRESOLVED, null);
  84. }
  85. /**
  86. * Creates a new validation status for the given binding and validation
  87. * result.
  88. *
  89. * @param source
  90. * the source binding
  91. * @param result
  92. * the result of the validation
  93. */
  94. public ValidationStatus(Binding<?, TARGET> source,
  95. ValidationResult result) {
  96. this(source, result.isError() ? Status.ERROR : Status.OK, result);
  97. }
  98. /**
  99. * Creates a new status change event.
  100. * <p>
  101. * The {@code message} must be {@code null} if the {@code status} is
  102. * {@link Status#OK}.
  103. *
  104. * @param source
  105. * field whose status has changed, not {@code null}
  106. * @param status
  107. * updated status value, not {@code null}
  108. * @param result
  109. * the related result, may be {@code null}
  110. */
  111. public ValidationStatus(Binding<?, TARGET> source, Status status,
  112. ValidationResult result) {
  113. Objects.requireNonNull(source, "Event source may not be null");
  114. Objects.requireNonNull(status, "Status may not be null");
  115. if (Objects.equals(status, Status.OK) && result.isError()
  116. || Objects.equals(status, Status.ERROR) && !result.isError()
  117. || Objects.equals(status, Status.UNRESOLVED)
  118. && result != null) {
  119. throw new IllegalStateException(
  120. "Invalid validation status " + status + " for given result "
  121. + (result == null ? "null" : result.toString()));
  122. }
  123. binding = source;
  124. this.status = status;
  125. this.result = result;
  126. }
  127. /**
  128. * Gets status of the validation.
  129. *
  130. * @return status
  131. */
  132. public Status getStatus() {
  133. return status;
  134. }
  135. /**
  136. * Gets whether the validation failed or not.
  137. *
  138. * @return {@code true} if validation failed, {@code false} if validation
  139. * passed
  140. */
  141. public boolean isError() {
  142. return status == Status.ERROR;
  143. }
  144. /**
  145. * Gets error validation message if status is {@link Status#ERROR}.
  146. *
  147. * @return an optional validation error status or an empty optional if
  148. * status is not an error
  149. */
  150. public Optional<String> getMessage() {
  151. return Optional.ofNullable(result).filter(ValidationResult::isError)
  152. .map(ValidationResult::getErrorMessage);
  153. }
  154. /**
  155. * Gets the validation result if status is either {@link Status#OK} or
  156. * {@link Status#ERROR} or an empty optional if status is
  157. * {@link Status#UNRESOLVED}.
  158. *
  159. * @return the validation result
  160. */
  161. public Optional<ValidationResult> getResult() {
  162. return Optional.ofNullable(result);
  163. }
  164. /**
  165. * Gets the source binding of the validation status.
  166. *
  167. * @return the source binding
  168. */
  169. public Binding<?, TARGET> getBinding() {
  170. return binding;
  171. }
  172. /**
  173. * Gets the bound field for this status.
  174. *
  175. * @return the field
  176. */
  177. public HasValue<?> getField() {
  178. return getBinding().getField();
  179. }
  180. }