Skip to content

Commit d5a5103

Browse files
committed
Update usability of Problem class
1 parent 7e03faf commit d5a5103

File tree

1 file changed

+85
-19
lines changed

1 file changed

+85
-19
lines changed

src/main/java/io/github/malczuuu/problem4j/core/Problem.java

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package io.github.malczuuu.problem4j.core;
22

3+
import java.io.Serial;
34
import java.io.Serializable;
45
import java.net.URI;
56
import java.util.ArrayList;
7+
import java.util.Arrays;
68
import java.util.Collections;
79
import java.util.HashMap;
810
import java.util.List;
911
import java.util.Map;
12+
import java.util.Objects;
1013
import java.util.Set;
1114
import java.util.stream.Collectors;
1215

1316
public class Problem implements Serializable {
1417

18+
@Serial private static final long serialVersionUID = 1L;
19+
1520
public static final URI BLANK_TYPE = URI.create("about:blank");
1621
public static final String CONTENT_TYPE = "application/problem+json";
1722

@@ -20,13 +25,18 @@ public static ProblemBuilder builder() {
2025
}
2126

2227
public static ProblemBuilder builder(Problem problem) {
23-
ProblemBuilder builder = new ProblemBuilderImpl();
24-
builder.type(problem.getType());
25-
builder.title(problem.getTitle());
26-
builder.status(problem.getStatus());
27-
builder.detail(problem.getDetail());
28-
builder.instance(problem.getInstance());
29-
problem.getExtensions().forEach(e -> builder.extension(e, problem.getExtensionValue(e)));
28+
ProblemBuilder builder =
29+
builder()
30+
.type(problem.getType())
31+
.title(problem.getTitle())
32+
.status(problem.getStatus())
33+
.detail(problem.getDetail())
34+
.instance(problem.getInstance());
35+
36+
for (String extension : problem.getExtensions()) {
37+
builder = builder.extension(extension, problem.getExtensionValue(extension));
38+
}
39+
3040
return builder;
3141
}
3242

@@ -53,7 +63,7 @@ public Problem(
5363
this.status = status;
5464
this.detail = detail;
5565
this.instance = instance;
56-
this.extensions = Collections.unmodifiableMap(new HashMap<>(extensions));
66+
this.extensions = Map.copyOf(extensions);
5767
}
5868

5969
public Problem(
@@ -66,6 +76,11 @@ public Problem(
6676
this(type, title, status, detail, instance, buildMapFromExtensions(extensions));
6777
}
6878

79+
public Problem(
80+
URI type, String title, int status, String detail, URI instance, Object... extensions) {
81+
this(type, title, status, detail, instance, buildMapFromRawArgs(extensions));
82+
}
83+
6984
private static Map<String, Object> buildMapFromExtensions(Set<Extension> extensions) {
7085
Map<String, Object> map = new HashMap<>(extensions.size());
7186
extensions.forEach(e -> map.put(e.getKey(), e.getValue()));
@@ -80,6 +95,23 @@ private static Map<String, Object> buildMapFromExtensions(Extension[] extensions
8095
return map;
8196
}
8297

98+
private static Map<String, Object> buildMapFromRawArgs(Object[] arguments) {
99+
Map<String, Object> map = new HashMap<>(arguments.length / 2);
100+
101+
List<Object> valuesAsList = new ArrayList<>(Arrays.asList(arguments));
102+
if (valuesAsList.size() % 2 != 0) {
103+
valuesAsList.add(valuesAsList.size() - 1);
104+
}
105+
106+
for (int i = 0; i < arguments.length; i += 2) {
107+
String key = arguments[i].toString();
108+
Object value = arguments[i + 1];
109+
map.put(key, value);
110+
}
111+
112+
return map;
113+
}
114+
83115
public URI getType() {
84116
return this.type;
85117
}
@@ -112,18 +144,37 @@ public boolean hasExtension(String extension) {
112144
return extensions.containsKey(extension);
113145
}
114146

147+
@Override
148+
public boolean equals(Object o) {
149+
if (o == null || getClass() != o.getClass()) {
150+
return false;
151+
}
152+
Problem problem = (Problem) o;
153+
return Objects.equals(getType(), problem.getType())
154+
&& Objects.equals(getTitle(), problem.getTitle())
155+
&& getStatus() == problem.getStatus()
156+
&& Objects.equals(getDetail(), problem.getDetail())
157+
&& Objects.equals(getInstance(), problem.getInstance())
158+
&& Objects.equals(extensions, problem.extensions);
159+
}
160+
161+
@Override
162+
public int hashCode() {
163+
return Objects.hash(getType(), getTitle(), getStatus(), getDetail(), getInstance(), extensions);
164+
}
165+
115166
@Override
116167
public String toString() {
117168
List<String> lines = new ArrayList<>(4);
118-
if (type != null) {
119-
lines.add("\"type\": \"" + quote(type.toString()) + "\"");
169+
if (getType() != null) {
170+
lines.add("\"type\": \"" + quote(getType().toString()) + "\"");
120171
}
121-
if (title != null) {
122-
lines.add("\"title\": \"" + quote(title) + "\"");
172+
if (getTitle() != null) {
173+
lines.add("\"title\": \"" + quote(getTitle()) + "\"");
123174
}
124-
lines.add("\"status\": " + status);
125-
if (detail != null) {
126-
lines.add("\"detail\": \"" + quote(detail) + "\"");
175+
lines.add("\"status\": " + getStatus());
176+
if (getDetail() != null) {
177+
lines.add("\"detail\": \"" + quote(getDetail()) + "\"");
127178
}
128179
return lines.stream().collect(Collectors.joining(", ", "{ ", " }"));
129180
}
@@ -158,15 +209,30 @@ public Object setValue(Object value) {
158209
return value;
159210
}
160211

212+
@Override
213+
public boolean equals(Object obj) {
214+
if (obj == null || getClass() != obj.getClass()) {
215+
return false;
216+
}
217+
Extension extension = (Extension) obj;
218+
return Objects.equals(getKey(), extension.getKey())
219+
&& Objects.equals(getValue(), extension.getValue());
220+
}
221+
222+
@Override
223+
public int hashCode() {
224+
return Objects.hash(getKey(), getValue());
225+
}
226+
161227
@Override
162228
public String toString() {
163229
String valueLine;
164-
if (value instanceof Number || value instanceof Boolean) {
165-
valueLine = "\"value\": " + value;
230+
if (getValue() instanceof Number || getValue() instanceof Boolean) {
231+
valueLine = "\"value\": " + getValue();
166232
} else {
167-
valueLine = "\"value\": " + "\"" + quote(value.toString()) + "\"";
233+
valueLine = "\"value\": " + "\"" + quote(getValue().toString()) + "\"";
168234
}
169-
return "{ \"key\": \"" + quote(key) + "\", " + valueLine + " }";
235+
return "{ \"key\": \"" + quote(getKey()) + "\", " + valueLine + " }";
170236
}
171237
}
172238
}

0 commit comments

Comments
 (0)