View Javadoc

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  
17  package com.atlassian.jira.rest.client.api.domain.util;
18  
19  import com.google.common.base.Objects;
20  import com.google.common.collect.ImmutableList;
21  import com.google.common.collect.ImmutableMap;
22  
23  import javax.annotation.Nullable;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Map;
27  
28  /**
29   * Error container returned in bulk operations
30   *
31   * @since v2.0
32   */
33  public class ErrorCollection {
34  
35  	private final Integer status;
36  	private final Collection<String> errorMessages;
37  	private final Map<String, String> errors;
38  
39  	public ErrorCollection(@Nullable final Integer status, final Collection<String> errorMessages, final Map<String, String> errors) {
40  		this.status = status;
41  		this.errors = ImmutableMap.copyOf(errors);
42  		this.errorMessages = ImmutableList.copyOf(errorMessages);
43  	}
44  
45  	public ErrorCollection(final String errorMessage) {
46  		this(null, ImmutableList.of(errorMessage), Collections.<String, String>emptyMap());
47  	}
48  
49  	@SuppressWarnings("unused")
50  	@Nullable
51  	public Integer getStatus() {
52  		return status;
53  	}
54  
55  	public Collection<String> getErrorMessages() {
56  		return errorMessages;
57  	}
58  
59  	public Map<String, String> getErrors() {
60  		return errors;
61  	}
62  
63  	public static Builder builder() {
64  		return new Builder();
65  	}
66  
67  	@Override
68  	public String toString() {
69  		return Objects.toStringHelper(this)
70  				.add("status", status)
71  				.add("errors", errors)
72  				.add("errorMessages", errorMessages)
73  				.toString();
74  	}
75  
76  	@Override
77  	public boolean equals(final Object obj) {
78  		if (obj instanceof ErrorCollection) {
79  			final ErrorCollection that = (ErrorCollection) obj;
80  			return Objects.equal(this.status, that.status)
81  					&& Objects.equal(this.errors, that.errors)
82  					&& Objects.equal(this.errorMessages, that.errorMessages);
83  		}
84  		return false;
85  	}
86  
87  	@Override
88  	public int hashCode() {
89  		return Objects.hashCode(status, errors, errorMessages);
90  	}
91  
92  	public static class Builder {
93  
94  		private int status;
95  		private final ImmutableMap.Builder<String, String> errors;
96  		private final ImmutableList.Builder<String> errorMessages;
97  
98  		public Builder() {
99  			errors = ImmutableMap.builder();
100 			errorMessages = ImmutableList.builder();
101 		}
102 
103 		public Builder status(final int status) {
104 			this.status = status;
105 			return this;
106 		}
107 
108 		public Builder error(final String key, final String message) {
109 			errors.put(key, message);
110 			return this;
111 
112 		}
113 
114 		public Builder errorMessage(final String message) {
115 			errorMessages.add(message);
116 			return this;
117 		}
118 
119 		public ErrorCollection build() {
120 			return new ErrorCollection(status, errorMessages.build(), errors.build());
121 		}
122 	}
123 }