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 java.util.Collections;
7   import javax.annotation.Nonnull;
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  {
19      private static final ValidationResult VALID = new ValidationResult(Collections.<String>emptySet());
20  
21      private final ImmutableSet<String> errorMessages;
22  
23      private final ImmutableSet<String> warningMessages;
24  
25      private ValidationResult(@Nonnull final Iterable<String> errorMessages)
26      {
27          this(errorMessages, Collections.<String>emptySet());
28      }
29  
30      private ValidationResult(@Nonnull final Iterable<String> errorMessages, @Nonnull final Iterable<String> warningMessages)
31      {
32          checkNotNull(errorMessages, "errorMessages");
33          checkNotNull(warningMessages, "warningMessages");
34          this.errorMessages = ImmutableSet.copyOf(errorMessages);
35          this.warningMessages = ImmutableSet.copyOf(warningMessages);
36      }
37  
38      public static ValidationResult valid()
39      {
40          return VALID;
41      }
42  
43      public static ValidationResult withErrorMessages(@Nonnull final Iterable<String> errorMessages)
44      {
45          return new ValidationResult(errorMessages);
46      }
47  
48      public static ValidationResult withWarningMessages(@Nonnull final Iterable<String> warningMessages)
49      {
50          return new ValidationResult(Collections.<String>emptySet(), warningMessages);
51      }
52  
53      public static ValidationResult withErrorAndWarningMessages(@Nonnull final Iterable<String> errorMessages, @Nonnull final Iterable<String> warningMessages)
54      {
55          return new ValidationResult(errorMessages, warningMessages);
56      }
57  
58      /**
59       * Return true if the validation passed, false if there were errors. (Note warnings do not cause this method to
60       * return false).
61       *
62       * @return true if the validation passed, false if there were errors.
63       * @see #hasWarnings()
64       */
65      public boolean isValid()
66      {
67          return errorMessages.isEmpty();
68      }
69  
70      /**
71       * Return true if the validation added error messages.
72       *
73       * @return true if the validation failed with error messages.
74       */
75      public boolean hasErrors()
76      {
77          return !errorMessages.isEmpty();
78      }
79  
80      /**
81       * Return true if the validation added warning messages.
82       *
83       * @return true if the validation failed with warning messages.
84       */
85      public boolean hasWarnings()
86      {
87          return !warningMessages.isEmpty();
88      }
89  
90      /**
91       * Returns error messages.
92       * <p/>
93       * These should normally be localised to the end user's locale. This will never be null, but may be empty.
94       *
95       * @return validation error messages, could be empty but not null.
96       */
97      @Nonnull
98      public Iterable<String> getErrorMessages()
99      {
100         return errorMessages;
101     }
102 
103     /**
104      * Returns warnings messages.
105      * <p/>
106      * These should normally be localised to the end user's locale. This will never be null, but may be empty.
107      *
108      * @return validation warning messages, could be empty but not null.
109      */
110     @Nonnull
111     public Iterable<String> getWarningMessages()
112     {
113         return warningMessages;
114     }
115 }