PageRenderTime 36ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-kinect-wallboard-extensions/src/main/java/com/atlassian/jirawallboard/resttransitionproxy/ErrorCollection.java

https://bitbucket.org/shamid/kinect-wallboards/
Java | 273 lines | 187 code | 47 blank | 39 comment | 2 complexity | 06f743fb3cf1dafeb224bfce200ce14e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, BSD-3-Clause
  1. package com.atlassian.jirawallboard.resttransitionproxy;
  2. import com.atlassian.jira.util.dbc.Assertions;
  3. import org.apache.commons.lang.builder.EqualsBuilder;
  4. import org.apache.commons.lang.builder.HashCodeBuilder;
  5. import org.apache.commons.lang.builder.ToStringBuilder;
  6. import org.apache.commons.lang.builder.ToStringStyle;
  7. import javax.xml.bind.annotation.XmlElement;
  8. import javax.xml.bind.annotation.XmlRootElement;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.Collection;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Set;
  16. import static com.atlassian.jira.util.dbc.Assertions.notNull;
  17. /**
  18. * A JAXB representation of an {@link com.atlassian.jira.util.ErrorCollection} useful for returning via JSON or XML.
  19. *
  20. * @since v4.2
  21. */
  22. @XmlRootElement
  23. public class ErrorCollection
  24. {
  25. /**
  26. * @return a new Builder
  27. */
  28. public static Builder builder()
  29. {
  30. return Builder.newBuilder();
  31. }
  32. /**
  33. * Returns a new ErrorCollection containing a list of error messages.
  34. *
  35. * @param messages an array of Strings containing error messages
  36. * @return a new ErrorCollection
  37. */
  38. public static ErrorCollection of(String... messages)
  39. {
  40. return of(Arrays.asList(messages));
  41. }
  42. /**
  43. * Returns a new ErrorCollection containing a list of error messages.
  44. *
  45. * @param messages an Iterable of Strings containing error messages
  46. * @return a new ErrorCollection
  47. */
  48. public static ErrorCollection of(Iterable<String> messages)
  49. {
  50. Builder b = builder();
  51. for (String message : messages)
  52. {
  53. b.addErrorMessage(message);
  54. }
  55. return b.build();
  56. }
  57. /**
  58. * Returns a new ErrorCollection containing all the errors contained in the input error collection.
  59. *
  60. * @param errorCollection a com.atlassian.jira.util.ErrorCollection
  61. * @return a new ErrorCollection
  62. */
  63. public static ErrorCollection of(com.atlassian.jira.util.ErrorCollection errorCollection)
  64. {
  65. return builder().addErrorCollection(errorCollection).build();
  66. }
  67. /**
  68. * Generic error messages
  69. */
  70. @XmlElement
  71. private Collection<String> errorMessages = new ArrayList<String>();
  72. @XmlElement
  73. private HashMap<String, String> errors = new HashMap<String, String>();
  74. /**
  75. * Builder used to create a new immutable error collection.
  76. */
  77. public static class Builder
  78. {
  79. private ErrorCollection errorCollection;
  80. public static Builder newBuilder()
  81. {
  82. return new Builder(Collections.<String>emptyList());
  83. }
  84. public static Builder newBuilder(ValidationError... errors)
  85. {
  86. Assertions.notNull("errors", errors);
  87. return new Builder(Collections.<String>emptyList());
  88. }
  89. public static Builder newBuilder(Set<String> errorMessages)
  90. {
  91. Assertions.notNull("errorMessages", errorMessages);
  92. return new Builder(errorMessages);
  93. }
  94. public static Builder newBuilder(Collection<ValidationError> errors)
  95. {
  96. Assertions.notNull("errors", errors);
  97. return new Builder(Collections.<String>emptyList());
  98. }
  99. public static Builder newBuilder(ErrorCollection errorCollection)
  100. {
  101. Assertions.notNull("errorCollection", errorCollection);
  102. return new Builder(errorCollection.getErrorMessages());
  103. }
  104. Builder(Collection<String> errorMessages)
  105. {
  106. this.errorCollection = new ErrorCollection(errorMessages);
  107. }
  108. public Builder addErrorCollection(com.atlassian.jira.util.ErrorCollection errorCollection)
  109. {
  110. Assertions.notNull("errorCollection", errorCollection);
  111. this.errorCollection.addErrorCollection(errorCollection);
  112. return this;
  113. }
  114. public Builder addErrorMessage(String errorMessage)
  115. {
  116. Assertions.notNull("errorMessage", errorMessage);
  117. this.errorCollection.addErrorMessage(errorMessage);
  118. return this;
  119. }
  120. public ErrorCollection build()
  121. {
  122. return this.errorCollection;
  123. }
  124. }
  125. @SuppressWarnings ( { "UnusedDeclaration", "unused" })
  126. private ErrorCollection()
  127. {}
  128. private ErrorCollection(Collection<String> errorMessages)
  129. {
  130. this.errorMessages.addAll(notNull("errorMessages", errorMessages));
  131. }
  132. @SuppressWarnings ("unchecked")
  133. private void addErrorCollection(com.atlassian.jira.util.ErrorCollection errorCollection)
  134. {
  135. errorMessages.addAll(notNull("errorCollection", errorCollection).getErrorMessages());
  136. errors.putAll(errorCollection.getErrors());
  137. }
  138. private void addErrorMessage(String errorMessage)
  139. {
  140. errorMessages.add(errorMessage);
  141. }
  142. public boolean hasAnyErrors()
  143. {
  144. return !errorMessages.isEmpty() && !errors.isEmpty();
  145. }
  146. public Collection<String> getErrorMessages()
  147. {
  148. return errorMessages;
  149. }
  150. @Override
  151. public int hashCode()
  152. {
  153. return HashCodeBuilder.reflectionHashCode(this);
  154. }
  155. @Override
  156. public boolean equals(final Object o)
  157. {
  158. return EqualsBuilder.reflectionEquals(this, o);
  159. }
  160. @Override
  161. public String toString()
  162. {
  163. return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  164. }
  165. /**
  166. * Simple bean for holding a field reference and an error key as well as some optional parameters.
  167. *
  168. * @since v4.2
  169. */
  170. @XmlRootElement
  171. class ValidationError
  172. {
  173. // The field the error relates to
  174. @XmlElement
  175. private String field;
  176. // The Error key...
  177. @XmlElement
  178. private String error;
  179. @XmlElement
  180. private List<String> params;
  181. @SuppressWarnings ( { "UnusedDeclaration", "unused" })
  182. private ValidationError() {}
  183. public ValidationError(String field, String error)
  184. {
  185. this.field = field;
  186. this.error = error;
  187. }
  188. public ValidationError(String field, String error, List<String> params)
  189. {
  190. this.field = field;
  191. this.error = error;
  192. this.params = params;
  193. }
  194. public ValidationError(String field, String error, String param)
  195. {
  196. this(field, error, Arrays.asList(param));
  197. }
  198. public String getField()
  199. {
  200. return field;
  201. }
  202. public String getError()
  203. {
  204. return error;
  205. }
  206. public List<String> getParams()
  207. {
  208. return params;
  209. }
  210. @Override
  211. public int hashCode()
  212. {
  213. return HashCodeBuilder.reflectionHashCode(this);
  214. }
  215. @Override
  216. public boolean equals(final Object o)
  217. {
  218. return EqualsBuilder.reflectionEquals(this, o);
  219. }
  220. @Override
  221. public String toString()
  222. {
  223. return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  224. }
  225. }
  226. }