/api/src/main/java/com/atlassian/jira/rest/client/api/domain/util/ErrorCollection.java

https://bitbucket.org/paulrenenichols/jira-rest-java-client · Java · 123 lines · 81 code · 22 blank · 20 comment · 3 complexity · 83842ff09bc58a9d40b7b8af501c2014 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010-2012 Atlassian
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.atlassian.jira.rest.client.api.domain.util;
  17. import com.google.common.base.Objects;
  18. import com.google.common.collect.ImmutableList;
  19. import com.google.common.collect.ImmutableMap;
  20. import javax.annotation.Nullable;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.Map;
  24. /**
  25. * Error container returned in bulk operations
  26. *
  27. * @since v2.0
  28. */
  29. public class ErrorCollection {
  30. private final Integer status;
  31. private final Collection<String> errorMessages;
  32. private final Map<String, String> errors;
  33. public ErrorCollection(@Nullable final Integer status, final Collection<String> errorMessages, final Map<String, String> errors) {
  34. this.status = status;
  35. this.errors = ImmutableMap.copyOf(errors);
  36. this.errorMessages = ImmutableList.copyOf(errorMessages);
  37. }
  38. public ErrorCollection(final String errorMessage) {
  39. this(null, ImmutableList.of(errorMessage), Collections.<String, String>emptyMap());
  40. }
  41. @SuppressWarnings("unused")
  42. @Nullable
  43. public Integer getStatus() {
  44. return status;
  45. }
  46. public Collection<String> getErrorMessages() {
  47. return errorMessages;
  48. }
  49. public Map<String, String> getErrors() {
  50. return errors;
  51. }
  52. public static Builder builder() {
  53. return new Builder();
  54. }
  55. @Override
  56. public String toString() {
  57. return Objects.toStringHelper(this)
  58. .add("status", status)
  59. .add("errors", errors)
  60. .add("errorMessages", errorMessages)
  61. .toString();
  62. }
  63. @Override
  64. public boolean equals(final Object obj) {
  65. if (obj instanceof ErrorCollection) {
  66. final ErrorCollection that = (ErrorCollection) obj;
  67. return Objects.equal(this.status, that.status)
  68. && Objects.equal(this.errors, that.errors)
  69. && Objects.equal(this.errorMessages, that.errorMessages);
  70. }
  71. return false;
  72. }
  73. @Override
  74. public int hashCode() {
  75. return Objects.hashCode(status, errors, errorMessages);
  76. }
  77. public static class Builder {
  78. private int status;
  79. private final ImmutableMap.Builder<String, String> errors;
  80. private final ImmutableList.Builder<String> errorMessages;
  81. public Builder() {
  82. errors = ImmutableMap.builder();
  83. errorMessages = ImmutableList.builder();
  84. }
  85. public Builder status(final int status) {
  86. this.status = status;
  87. return this;
  88. }
  89. public Builder error(final String key, final String message) {
  90. errors.put(key, message);
  91. return this;
  92. }
  93. public Builder errorMessage(final String message) {
  94. errorMessages.add(message);
  95. return this;
  96. }
  97. public ErrorCollection build() {
  98. return new ErrorCollection(status, errorMessages.build(), errors.build());
  99. }
  100. }
  101. }