Skip to content

Commit da69542

Browse files
Update Java version references from 21 to 17 in documentation and code comments; refactor JsonNormalizer to replace switch expressions with if-else statements for better readability.
1 parent b4467b5 commit da69542

File tree

7 files changed

+100
-61
lines changed

7 files changed

+100
-61
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apply from: 'gradle/publishing.gradle'
1313
apply from: 'gradle/verification.gradle'
1414

1515
java {
16-
// Only enforce toolchain in CI to ensure consistent Java 21 builds
16+
// Only enforce toolchain in CI to ensure consistent Java 17 builds
1717
// Locally, use whatever JDK is installed
1818
if (System.getenv('CI') == 'true') {
1919
toolchain {

src/main/java/com/felipestanzani/jtoon/encoder/HeaderFormatter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
/**
88
* Formats headers for arrays and tables in TOON format.
9-
* Uses Java 21 Records to reduce parameter count and improve code clarity.
109
*/
1110
public final class HeaderFormatter {
1211

@@ -16,7 +15,6 @@ private HeaderFormatter() {
1615

1716
/**
1817
* Configuration for header formatting.
19-
* Using Java 21 Record to group related parameters.
2018
*
2119
* @param length Array or table length
2220
* @param key Optional key prefix

src/main/java/com/felipestanzani/jtoon/encoder/ListItemEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void encodeObjectAsListItem(ObjectNode obj, LineWriter writer, int
4040
}
4141

4242
// First key-value on the same line as "- "
43-
String firstKey = keys.getFirst();
43+
String firstKey = keys.get(0);
4444
JsonNode firstValue = obj.get(firstKey);
4545
encodeFirstKeyValue(firstKey, firstValue, writer, depth, options);
4646

src/main/java/com/felipestanzani/jtoon/encoder/PrimitiveEncoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* Encodes primitive values and object keys for TOON format.
1313
* Delegates validation to StringValidator, escaping to StringEscaper,
1414
* and header formatting to HeaderFormatter.
15-
* Refactored to use Java 21 features and follow SOLID principles.
1615
*/
1716
public final class PrimitiveEncoder {
1817

@@ -21,7 +20,7 @@ private PrimitiveEncoder() {
2120
}
2221

2322
/**
24-
* Encodes a primitive JsonNode value using Java 21 switch expressions.
23+
* Encodes a primitive JsonNode value.
2524
*/
2625
public static String encodePrimitive(JsonNode value, String delimiter) {
2726
return switch (value.getNodeType()) {

src/main/java/com/felipestanzani/jtoon/normalizer/JsonNormalizer.java

Lines changed: 94 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.*;
1212
import java.util.function.Function;
1313
import java.util.function.IntFunction;
14+
import java.util.stream.Collectors;
1415
import java.util.stream.Stream;
1516

1617
/**
@@ -60,15 +61,19 @@ public static JsonNode parse(String json) {
6061
* @return The normalized JsonNode
6162
*/
6263
public static JsonNode normalize(Object value) {
63-
return switch (value) {
64-
case null -> NullNode.getInstance();
65-
case JsonNode jsonNode -> jsonNode;
66-
case Optional<?> optional -> normalize(optional.orElse(null));
67-
case Stream<?> stream -> normalize(stream.toList());
68-
default -> value.getClass().isArray()
69-
? normalizeArray(value)
70-
: normalizeWithStrategy(value);
71-
};
64+
if (value == null) {
65+
return NullNode.getInstance();
66+
} else if (value instanceof JsonNode) {
67+
return (JsonNode) value;
68+
} else if (value instanceof Optional<?>) {
69+
return normalize(((Optional<?>) value).orElse(null));
70+
} else if (value instanceof Stream<?>) {
71+
return normalize(((Stream<?>) value).collect(Collectors.toList()));
72+
} else if (value.getClass().isArray()) {
73+
return normalizeArray(value);
74+
} else {
75+
return normalizeWithStrategy(value);
76+
}
7277
}
7378

7479
/**
@@ -87,17 +92,25 @@ private static JsonNode normalizeWithStrategy(Object value) {
8792
* Returns null if the value is not a primitive type.
8893
*/
8994
private static JsonNode tryNormalizePrimitive(Object value) {
90-
return switch (value) {
91-
case String string -> TextNode.valueOf(string);
92-
case Boolean bool -> BooleanNode.valueOf(bool);
93-
case Integer integer -> IntNode.valueOf(integer);
94-
case Long longVal -> LongNode.valueOf(longVal);
95-
case Double doubleVal -> normalizeDouble(doubleVal);
96-
case Float floatVal -> normalizeFloat(floatVal);
97-
case Short shortVal -> ShortNode.valueOf(shortVal);
98-
case Byte byteVal -> IntNode.valueOf(byteVal);
99-
case null, default -> null;
100-
};
95+
if (value instanceof String) {
96+
return TextNode.valueOf((String) value);
97+
} else if (value instanceof Boolean) {
98+
return BooleanNode.valueOf((Boolean) value);
99+
} else if (value instanceof Integer) {
100+
return IntNode.valueOf((Integer) value);
101+
} else if (value instanceof Long) {
102+
return LongNode.valueOf((Long) value);
103+
} else if (value instanceof Double) {
104+
return normalizeDouble((Double) value);
105+
} else if (value instanceof Float) {
106+
return normalizeFloat((Float) value);
107+
} else if (value instanceof Short) {
108+
return ShortNode.valueOf((Short) value);
109+
} else if (value instanceof Byte) {
110+
return IntNode.valueOf((Byte) value);
111+
} else {
112+
return null;
113+
}
101114
}
102115

103116
/**
@@ -144,11 +157,13 @@ private static Optional<JsonNode> tryConvertToLong(Double value) {
144157
* Returns null if the value is not a big number type.
145158
*/
146159
private static JsonNode tryNormalizeBigNumber(Object value) {
147-
return switch (value) {
148-
case BigInteger bigInt -> normalizeBigInteger(bigInt);
149-
case BigDecimal bigDec -> DecimalNode.valueOf(bigDec);
150-
case null, default -> null;
151-
};
160+
if (value instanceof BigInteger) {
161+
return normalizeBigInteger((BigInteger) value);
162+
} else if (value instanceof BigDecimal) {
163+
return DecimalNode.valueOf((BigDecimal) value);
164+
} else {
165+
return null;
166+
}
152167
}
153168

154169
/**
@@ -167,16 +182,23 @@ private static JsonNode normalizeBigInteger(BigInteger value) {
167182
* Returns null if the value is not a temporal type.
168183
*/
169184
private static JsonNode tryNormalizeTemporal(Object value) {
170-
return switch (value) {
171-
case LocalDateTime v -> formatTemporal(v, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
172-
case LocalDate v -> formatTemporal(v, DateTimeFormatter.ISO_LOCAL_DATE);
173-
case LocalTime v -> formatTemporal(v, DateTimeFormatter.ISO_LOCAL_TIME);
174-
case ZonedDateTime v -> formatTemporal(v, DateTimeFormatter.ISO_ZONED_DATE_TIME);
175-
case OffsetDateTime v -> formatTemporal(v, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
176-
case Instant instant -> TextNode.valueOf(instant.toString());
177-
case java.util.Date date -> TextNode.valueOf(date.toInstant().toString());
178-
case null, default -> null;
179-
};
185+
if (value instanceof LocalDateTime) {
186+
return formatTemporal((LocalDateTime) value, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
187+
} else if (value instanceof LocalDate) {
188+
return formatTemporal((LocalDate) value, DateTimeFormatter.ISO_LOCAL_DATE);
189+
} else if (value instanceof LocalTime) {
190+
return formatTemporal((LocalTime) value, DateTimeFormatter.ISO_LOCAL_TIME);
191+
} else if (value instanceof ZonedDateTime) {
192+
return formatTemporal((ZonedDateTime) value, DateTimeFormatter.ISO_ZONED_DATE_TIME);
193+
} else if (value instanceof OffsetDateTime) {
194+
return formatTemporal((OffsetDateTime) value, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
195+
} else if (value instanceof Instant) {
196+
return TextNode.valueOf(((Instant) value).toString());
197+
} else if (value instanceof java.util.Date) {
198+
return TextNode.valueOf(((java.util.Date) value).toInstant().toString());
199+
} else {
200+
return null;
201+
}
180202
}
181203

182204
/**
@@ -191,11 +213,13 @@ private static <T> JsonNode formatTemporal(T temporal, DateTimeFormatter formatt
191213
* Returns null if the value is not a collection type.
192214
*/
193215
private static JsonNode tryNormalizeCollection(Object value) {
194-
return switch (value) {
195-
case Collection<?> collection -> normalizeCollection(collection);
196-
case Map<?, ?> map -> normalizeMap(map);
197-
case null, default -> null;
198-
};
216+
if (value instanceof Collection<?>) {
217+
return normalizeCollection((Collection<?>) value);
218+
} else if (value instanceof Map<?, ?>) {
219+
return normalizeMap((Map<?, ?>) value);
220+
} else {
221+
return null;
222+
}
199223
}
200224

201225
/**
@@ -232,18 +256,36 @@ private static JsonNode tryNormalizePojo(Object value) {
232256
* Normalizes arrays to ArrayNode.
233257
*/
234258
private static JsonNode normalizeArray(Object array) {
235-
return switch (array) {
236-
case int[] arr -> buildArrayNode(arr.length, i -> IntNode.valueOf(arr[i]));
237-
case long[] arr -> buildArrayNode(arr.length, i -> LongNode.valueOf(arr[i]));
238-
case double[] arr -> buildArrayNode(arr.length, i -> normalizeDoubleElement(arr[i]));
239-
case float[] arr -> buildArrayNode(arr.length, i -> normalizeFloatElement(arr[i]));
240-
case boolean[] arr -> buildArrayNode(arr.length, i -> BooleanNode.valueOf(arr[i]));
241-
case byte[] arr -> buildArrayNode(arr.length, i -> IntNode.valueOf(arr[i]));
242-
case short[] arr -> buildArrayNode(arr.length, i -> ShortNode.valueOf(arr[i]));
243-
case char[] arr -> buildArrayNode(arr.length, i -> TextNode.valueOf(String.valueOf(arr[i])));
244-
case Object[] arr -> buildArrayNode(arr.length, i -> normalize(arr[i]));
245-
case null, default -> MAPPER.createArrayNode();
246-
};
259+
if (array instanceof int[]) {
260+
int[] arr = (int[]) array;
261+
return buildArrayNode(arr.length, i -> IntNode.valueOf(arr[i]));
262+
} else if (array instanceof long[]) {
263+
long[] arr = (long[]) array;
264+
return buildArrayNode(arr.length, i -> LongNode.valueOf(arr[i]));
265+
} else if (array instanceof double[]) {
266+
double[] arr = (double[]) array;
267+
return buildArrayNode(arr.length, i -> normalizeDoubleElement(arr[i]));
268+
} else if (array instanceof float[]) {
269+
float[] arr = (float[]) array;
270+
return buildArrayNode(arr.length, i -> normalizeFloatElement(arr[i]));
271+
} else if (array instanceof boolean[]) {
272+
boolean[] arr = (boolean[]) array;
273+
return buildArrayNode(arr.length, i -> BooleanNode.valueOf(arr[i]));
274+
} else if (array instanceof byte[]) {
275+
byte[] arr = (byte[]) array;
276+
return buildArrayNode(arr.length, i -> IntNode.valueOf(arr[i]));
277+
} else if (array instanceof short[]) {
278+
short[] arr = (short[]) array;
279+
return buildArrayNode(arr.length, i -> ShortNode.valueOf(arr[i]));
280+
} else if (array instanceof char[]) {
281+
char[] arr = (char[]) array;
282+
return buildArrayNode(arr.length, i -> TextNode.valueOf(String.valueOf(arr[i])));
283+
} else if (array instanceof Object[]) {
284+
Object[] arr = (Object[]) array;
285+
return buildArrayNode(arr.length, i -> normalize(arr[i]));
286+
} else {
287+
return MAPPER.createArrayNode();
288+
}
247289
}
248290

249291
/**

src/main/java/com/felipestanzani/jtoon/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@
173173
* records</li>
174174
* <li><strong>Utility Classes</strong>: Static utility classes with private
175175
* constructors</li>
176-
* <li><strong>Modern Java</strong>: Leverages Java 21 features (records, switch
177-
* expressions, pattern matching)</li>
176+
* <li><strong>Modern Java</strong>: Leverages Java 17 features (records, switch
177+
* expressions)</li>
178178
* </ul>
179179
*
180180
* <h2>Performance Considerations</h2>

0 commit comments

Comments
 (0)