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.

ValidationResult.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  20. * Represents the result of a validation. A result may be either successful or
  21. * contain an error message in case of a failure.
  22. * <p>
  23. * ValidationResult instances are created using the factory methods
  24. * {@link #ok()} and {@link #error(String)}, denoting success and failure
  25. * respectively.
  26. *
  27. * @author Vaadin Ltd
  28. * @since 8.0
  29. *
  30. */
  31. public interface ValidationResult extends Serializable {
  32. class SimpleValidationResult implements ValidationResult {
  33. private final String error;
  34. SimpleValidationResult(String error) {
  35. this.error = error;
  36. }
  37. @Override
  38. public String getErrorMessage() {
  39. if (error == null) {
  40. throw new IllegalStateException("The result is not an error. "
  41. + "It cannot contain error message");
  42. } else {
  43. return error;
  44. }
  45. }
  46. @Override
  47. public boolean isError() {
  48. return error != null;
  49. }
  50. }
  51. /**
  52. * Returns the result message.
  53. * <p>
  54. * Throws an {@link IllegalStateException} if the result represents success.
  55. *
  56. * @return the error message
  57. * @throws IllegalStateException
  58. * if the result represents success
  59. */
  60. String getErrorMessage();
  61. /**
  62. * Checks if the result denotes an error.
  63. *
  64. * @return <code>true</code> if the result denotes an error,
  65. * <code>false</code> otherwise
  66. */
  67. boolean isError();
  68. /**
  69. * Returns a successful result.
  70. *
  71. * @return the successful result
  72. */
  73. public static ValidationResult ok() {
  74. return new SimpleValidationResult(null);
  75. }
  76. /**
  77. * Creates the validation result which represent an error with the given
  78. * {@code errorMessage}.
  79. *
  80. * @param errorMessage
  81. * error message, not {@code null}
  82. * @return validation result which represent an error with the given
  83. * {@code errorMessage}
  84. * @throws NullPointerException
  85. * if {@code errorMessage} is null
  86. */
  87. public static ValidationResult error(String errorMessage) {
  88. Objects.requireNonNull(errorMessage);
  89. return new SimpleValidationResult(errorMessage);
  90. }
  91. }