View Javadoc

1   package com.atlassian.sal.api.validate;
2   
3   import com.atlassian.annotations.PublicApi;
4   import com.google.common.collect.ImmutableSet;
5   
6   import javax.annotation.Nonnull;
7   import java.util.Collections;
8   
9   import static com.google.common.base.Preconditions.checkNotNull;
10  
11  @SuppressWarnings("UnusedDeclaration")
12  @PublicApi
13  /**
14   * The outcome of a validation check.
15   * @since 3.0
16   */
17  public class ValidationResult {
18      private static final ValidationResult VALID = new ValidationResult(Collections.<String>emptySet());
19  
20      private final ImmutableSet<String> errorMessages;
21  
22      private final ImmutableSet<String> warningMessages;
23  
24      private ValidationResult(@Nonnull final Iterable<String> errorMessages) {
25          this(errorMessages, Collections.<String>emptySet());
26      }
27  
28      private ValidationResult(@Nonnull final Iterable<String> errorMessages, @Nonnull final Iterable<String> warningMessages) {
29          checkNotNull(errorMessages, "errorMessages");
30          checkNotNull(warningMessages, "warningMessages");
31          this.errorMessages = ImmutableSet.copyOf(errorMessages);
32          this.warningMessages = ImmutableSet.copyOf(warningMessages);
33      }
34  
35      public static ValidationResult valid() {
36          return VALID;
37      }
38  
39      public static ValidationResult withErrorMessages(@Nonnull final Iterable<String> errorMessages) {
40          return new ValidationResult(errorMessages);
41      }
42  
43      public static ValidationResult withWarningMessages(@Nonnull final Iterable<String> warningMessages) {
44          return new ValidationResult(Collections.<String>emptySet(), warningMessages);
45      }
46  
47      public static ValidationResult withErrorAndWarningMessages(@Nonnull final Iterable<String> errorMessages, @Nonnull final Iterable<String> warningMessages) {
48          return new ValidationResult(errorMessages, warningMessages);
49      }
50  
51      /**
52       * Return true if the validation passed, false if there were errors. (Note warnings do not cause this method to
53       * return false).
54       *
55       * @return true if the validation passed, false if there were errors.
56       * @see #hasWarnings()
57       */
58      public boolean isValid() {
59          return errorMessages.isEmpty();
60      }
61  
62      /**
63       * Return true if the validation added error messages.
64       *
65       * @return true if the validation failed with error messages.
66       */
67      public boolean hasErrors() {
68          return !errorMessages.isEmpty();
69      }
70  
71      /**
72       * Return true if the validation added warning messages.
73       *
74       * @return true if the validation failed with warning messages.
75       */
76      public boolean hasWarnings() {
77          return !warningMessages.isEmpty();
78      }
79  
80      /**
81       * Returns error messages.
82       * <p/>
83       * These should normally be localised to the end user's locale. This will never be null, but may be empty.
84       *
85       * @return validation error messages, could be empty but not null.
86       */
87      @Nonnull
88      public Iterable<String> getErrorMessages() {
89          return errorMessages;
90      }
91  
92      /**
93       * Returns warnings messages.
94       * <p/>
95       * These should normally be localised to the end user's locale. This will never be null, but may be empty.
96       *
97       * @return validation warning messages, could be empty but not null.
98       */
99      @Nonnull
100     public Iterable<String> getWarningMessages() {
101         return warningMessages;
102     }
103 }