Skip to content

Commit 1e9c2c2

Browse files
committed
Play with lower java version compatibility
1 parent 713bd9b commit 1e9c2c2

File tree

4 files changed

+62
-54
lines changed

4 files changed

+62
-54
lines changed

settings.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ pluginManagement {
44
}
55
}
66

7+
plugins {
8+
id("org.gradle.toolchains.foojay-resolver-convention").version("1.0.0")
9+
}
10+
711
rootProject.name = "problem4j-core"

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

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

3+
import java.util.HashMap;
34
import java.util.Map;
45

56
final class JsonEscape {
67

7-
private static final Map<Character, String> REPLACEMENTS =
8-
Map.of(
9-
'"', "\\\"",
10-
'\\', "\\\\",
11-
'\b', "\\b",
12-
'\f', "\\f",
13-
'\n', "\\n",
14-
'\r', "\\r",
15-
'\t', "\\t",
16-
'/', "\\/");
8+
private static final Map<Character, String> REPLACEMENTS = new HashMap<>();
9+
10+
static {
11+
REPLACEMENTS.put('"', "\\\"");
12+
REPLACEMENTS.put('\\', "\\\\");
13+
REPLACEMENTS.put('\b', "\\b");
14+
REPLACEMENTS.put('\f', "\\f");
15+
REPLACEMENTS.put('\n', "\\n");
16+
REPLACEMENTS.put('\r', "\\r");
17+
REPLACEMENTS.put('\t', "\\t");
18+
REPLACEMENTS.put('/', "\\/");
19+
}
1720

1821
static String escape(String value) {
1922
StringBuilder result = new StringBuilder();
@@ -54,9 +57,11 @@ private static boolean shouldBeHexed(char character) {
5457
* @param character the character to be escaped
5558
*/
5659
private static void hex(StringBuilder result, char character) {
57-
String hexedCharacter = Integer.toHexString(character);
60+
String hexedCharacter = Integer.toHexString(character).toUpperCase();
5861
result.append("\\u");
59-
result.append("0".repeat(4 - hexedCharacter.length()));
60-
result.append(hexedCharacter.toUpperCase());
62+
for (int i = 0; i < 4 - hexedCharacter.length(); i++) {
63+
result.append('0');
64+
}
65+
result.append(hexedCharacter);
6166
}
6267
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Problem(
4545
this.status = status;
4646
this.detail = detail;
4747
this.instance = instance;
48-
this.extensions = Map.copyOf(extensions);
48+
this.extensions = new HashMap<>(extensions);
4949
}
5050

5151
public Problem(

src/test/java/io/github/malczuuu/problem4j/core/ProblemTests.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import java.net.URI;
8+
import java.util.Arrays;
9+
import java.util.Collections;
10+
import java.util.HashMap;
11+
import java.util.HashSet;
812
import java.util.Map;
13+
import java.util.Set;
914
import org.junit.jupiter.api.Test;
1015

1116
class ProblemTests {
@@ -81,20 +86,18 @@ void givenNullKeyInArguments_whenCreatingProblem_thenNullPointerExceptionThrown(
8186
}
8287

8388
@Test
84-
void givenNullValueInArguments_whenCreatingProblem_thenNullPointerExceptionThrown() {
85-
assertThrows(
86-
NullPointerException.class,
87-
() ->
88-
new Problem(
89-
URI.create("https://example.com/error"),
90-
"Test Error",
91-
400,
92-
"Test detail",
93-
URI.create("https://example.com/instance"),
94-
"key1",
95-
null,
96-
"key2",
97-
"value2"));
89+
void givenNullValueInArguments_whenCreatingProblem_thenNullValuesAccepted() {
90+
91+
new Problem(
92+
URI.create("https://example.com/error"),
93+
"Test Error",
94+
400,
95+
"Test detail",
96+
URI.create("https://example.com/instance"),
97+
"key1",
98+
null,
99+
"key2",
100+
"value2");
98101
}
99102

100103
@Test
@@ -223,8 +226,10 @@ void givenExtensionArrayConstructor_whenCreatingProblem_thenExtensionsBuiltFromA
223226

224227
@Test
225228
void givenSetExtensionConstructor_whenCreatingProblem_thenExtensionsBuiltFromSet() {
226-
java.util.Set<Problem.Extension> extensions =
227-
java.util.Set.of(Problem.extension("key1", "value1"), Problem.extension("key2", "value2"));
229+
Set<Problem.Extension> extensions =
230+
new HashSet<>(
231+
Arrays.asList(
232+
Problem.extension("key1", "value1"), Problem.extension("key2", "value2")));
228233

229234
Problem problem =
230235
new Problem(
@@ -260,7 +265,7 @@ void givenEmptyExtensionArray_whenCreatingProblem_thenNoExtensions() {
260265

261266
@Test
262267
void givenEmptyExtensionSet_whenCreatingProblem_thenNoExtensions() {
263-
java.util.Set<Problem.Extension> emptyExtensions = java.util.Set.of();
268+
Set<Problem.Extension> emptyExtensions = Collections.emptySet();
264269

265270
Problem problem =
266271
new Problem(
@@ -275,36 +280,30 @@ void givenEmptyExtensionSet_whenCreatingProblem_thenNoExtensions() {
275280
}
276281

277282
@Test
278-
void givenExtensionsWithNullValues_whenCreatingProblem_thenNullPointerExceptionThrown() {
283+
void givenExtensionsWithNullValues_whenCreatingProblem_thenNullValueAccepted() {
279284
Problem.Extension extensionWithNullValue = Problem.extension("key1", null);
280285

281-
assertThrows(
282-
NullPointerException.class,
283-
() ->
284-
new Problem(
285-
URI.create("https://example.com/error"),
286-
"Test Error",
287-
400,
288-
"Test detail",
289-
URI.create("https://example.com/instance"),
290-
extensionWithNullValue));
286+
new Problem(
287+
URI.create("https://example.com/error"),
288+
"Test Error",
289+
400,
290+
"Test detail",
291+
URI.create("https://example.com/instance"),
292+
extensionWithNullValue);
291293
}
292294

293295
@Test
294-
void givenMapWithNullValues_whenCreatingProblem_thenNullPointerExceptionThrown() {
295-
Map<String, Object> mapWithNullValue = new java.util.HashMap<>();
296+
void givenMapWithNullValues_whenCreatingProblem_thenNullValuesAccepted() {
297+
Map<String, Object> mapWithNullValue = new HashMap<>();
296298
mapWithNullValue.put("key1", "value1");
297299
mapWithNullValue.put("key2", null);
298300

299-
assertThrows(
300-
NullPointerException.class,
301-
() ->
302-
new Problem(
303-
URI.create("https://example.com/error"),
304-
"Test Error",
305-
400,
306-
"Test detail",
307-
URI.create("https://example.com/instance"),
308-
mapWithNullValue));
301+
new Problem(
302+
URI.create("https://example.com/error"),
303+
"Test Error",
304+
400,
305+
"Test detail",
306+
URI.create("https://example.com/instance"),
307+
mapWithNullValue);
309308
}
310309
}

0 commit comments

Comments
 (0)