Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ValidationResult.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.shared.ui.ErrorLevel;
  21. /**
  22. * Represents the result of a validation. A result may be either successful or
  23. * contain an error message in case of a failure.
  24. * <p>
  25. * ValidationResult instances are created using the factory methods
  26. * {@link #ok()} and {@link #error(String)}, denoting success and failure
  27. * respectively.
  28. *
  29. * @author Vaadin Ltd
  30. * @since 8.0
  31. *
  32. */
  33. public interface ValidationResult extends Serializable {
  34. class SimpleValidationResult implements ValidationResult {
  35. private final String error;
  36. private final ErrorLevel errorLevel;
  37. SimpleValidationResult(String error, ErrorLevel errorLevel) {
  38. if (error != null && errorLevel == null) {
  39. throw new IllegalStateException("ValidationResult has an "
  40. + "error message, but no ErrorLevel is provided.");
  41. }
  42. this.error = error;
  43. this.errorLevel = errorLevel;
  44. }
  45. @Override
  46. public String getErrorMessage() {
  47. if (!getErrorLevel().isPresent()) {
  48. throw new IllegalStateException("The result is not an error. "
  49. + "It cannot contain error message");
  50. } else {
  51. return error != null ? error : "";
  52. }
  53. }
  54. public Optional<ErrorLevel> getErrorLevel() {
  55. return Optional.ofNullable(errorLevel);
  56. }
  57. }
  58. /**
  59. * Returns the result message.
  60. * <p>
  61. * Throws an {@link IllegalStateException} if the result represents success.
  62. *
  63. * @return the error message
  64. * @throws IllegalStateException
  65. * if the result represents success
  66. */
  67. String getErrorMessage();
  68. /**
  69. * Returns optional error level for this validation result. Error level is
  70. * not present for successful validation results.
  71. * <p>
  72. * <strong>Note:</strong> By default {@link ErrorLevel#INFO} and
  73. * {@link ErrorLevel#WARNING} are not considered to be blocking the
  74. * validation and conversion chain.
  75. *
  76. * @see #isError()
  77. *
  78. * @return optional error level; error level is present for validation
  79. * results that have not passed validation
  80. *
  81. * @since 8.2
  82. */
  83. Optional<ErrorLevel> getErrorLevel();
  84. /**
  85. * Checks if the result denotes an error.
  86. * <p>
  87. * <strong>Note:</strong> By default {@link ErrorLevel#INFO} and
  88. * {@link ErrorLevel#WARNING} are not considered to be errors.
  89. *
  90. * @return <code>true</code> if the result denotes an error,
  91. * <code>false</code> otherwise
  92. */
  93. default boolean isError() {
  94. ErrorLevel errorLevel = getErrorLevel().orElse(null);
  95. return errorLevel != null && errorLevel != ErrorLevel.INFO
  96. && errorLevel != ErrorLevel.WARNING;
  97. }
  98. /**
  99. * Returns a successful result.
  100. *
  101. * @return the successful result
  102. */
  103. public static ValidationResult ok() {
  104. return new SimpleValidationResult(null, null);
  105. }
  106. /**
  107. * Creates the validation result which represent an error with the given
  108. * {@code errorMessage}.
  109. *
  110. * @param errorMessage
  111. * error message, not {@code null}
  112. * @return validation result which represent an error with the given
  113. * {@code errorMessage}
  114. * @throws NullPointerException
  115. * if {@code errorMessage} is null
  116. */
  117. public static ValidationResult error(String errorMessage) {
  118. Objects.requireNonNull(errorMessage);
  119. return create(errorMessage, ErrorLevel.ERROR);
  120. }
  121. /**
  122. * Creates the validation result with the given {@code errorMessage} and
  123. * {@code errorLevel}. Results with {@link ErrorLevel} of {@code INFO} or
  124. * {@code WARNING} are not errors by default.
  125. *
  126. * @see #ok()
  127. * @see #error(String)
  128. *
  129. * @param errorMessage
  130. * error message, not {@code null}
  131. * @param errorLevel
  132. * error level, not {@code null}
  133. * @return validation result with the given {@code errorMessage} and
  134. * {@code errorLevel}
  135. * @throws NullPointerException
  136. * if {@code errorMessage} or {@code errorLevel} is {@code null}
  137. *
  138. * @since 8.2
  139. */
  140. public static ValidationResult create(String errorMessage,
  141. ErrorLevel errorLevel) {
  142. Objects.requireNonNull(errorMessage);
  143. Objects.requireNonNull(errorLevel);
  144. return new SimpleValidationResult(errorMessage, errorLevel);
  145. }
  146. }