1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.forgerock.api.models;
19
20 import static org.forgerock.api.util.ValidationUtil.*;
21 import static org.forgerock.util.Reject.*;
22
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.Set;
28
29 import com.fasterxml.jackson.annotation.JsonAnySetter;
30 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
31
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonValue;
34
35
36
37
38 @JsonDeserialize(builder = Errors.Builder.class)
39 public final class Errors {
40
41
42
43
44 public static final ErrorEntryComparator ERROR_ENTRY_COMPARATOR = new ErrorEntryComparator();
45
46 private final Map<String, ApiError> errors;
47
48 private Errors(Builder builder) {
49 this.errors = builder.errors;
50 }
51
52
53
54
55
56
57 @JsonValue
58 public Map<String, ApiError> getErrors() {
59 return errors;
60 }
61
62
63
64
65
66
67
68 @JsonIgnore
69 public ApiError get(String name) {
70 return errors.get(name);
71 }
72
73
74
75
76
77
78 @JsonIgnore
79 public Set<String> getNames() {
80 return errors.keySet();
81 }
82
83 @Override
84 public boolean equals(Object o) {
85 if (this == o) {
86 return true;
87 }
88 if (o == null || getClass() != o.getClass()) {
89 return false;
90 }
91 Errors errors1 = (Errors) o;
92 return Objects.equals(errors, errors1.errors);
93 }
94
95 @Override
96 public int hashCode() {
97 return Objects.hash(errors);
98 }
99
100
101
102
103
104
105 public static Builder errors() {
106 return new Builder();
107 }
108
109
110
111
112 public static final class Builder {
113
114 private final Map<String, ApiError> errors = new HashMap<>();
115
116
117
118
119 private Builder() {
120 }
121
122
123
124
125
126
127
128
129 @JsonAnySetter
130 public Builder put(String name, ApiError apiError) {
131 if (isEmpty(name) || containsWhitespace(name)) {
132 throw new IllegalArgumentException(
133 "Error name is required, must not be blank, and must not contain " +
134 "whitespace; given: '" + name + "'");
135 }
136 if (errors.containsKey(name) && !errors.get(name).equals(apiError)) {
137 throw new IllegalStateException(
138 "Error name already exists but Error objects are not equal; " +
139 "given: '" + name + "'");
140 }
141 errors.put(name, checkNotNull(apiError));
142 return this;
143 }
144
145
146
147
148
149
150 public Errors build() {
151 return new Errors(this);
152 }
153 }
154
155
156
157
158
159
160
161
162
163 private static class ErrorEntryComparator implements Comparator<Map.Entry<String, ApiError>> {
164 @Override
165 public int compare(final Map.Entry<String, ApiError> o1, final Map.Entry<String, ApiError> o2) {
166 final int codeCompare = Integer.compare(o1.getValue().getCode(), o2.getValue().getCode());
167 if (codeCompare == 0) {
168 return o1.getValue().getDescription().toString()
169 .compareTo(o2.getValue().getDescription().toString());
170 }
171 return codeCompare;
172 }
173 }
174
175 }