Skip to content

Commit 10445d1

Browse files
authored
Using constants to avoid the use of hardcoded values (#95)
* refactor: using constants in decode classes * refactor: using constants in encode classes * adjust imports * using constants instead of hardcoded values * enhance unit tests * feat: add more tests to ConstantsTest * adjust imports * remove random '//' I inserted
1 parent 482570e commit 10445d1

19 files changed

Lines changed: 298 additions & 155 deletions

src/main/java/dev/toonformat/jtoon/decoder/ArrayDecoder.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import java.util.List;
88
import java.util.regex.Matcher;
99

10+
import static dev.toonformat.jtoon.util.Constants.BACKSLASH;
11+
import static dev.toonformat.jtoon.util.Constants.COLON;
12+
import static dev.toonformat.jtoon.util.Constants.DOUBLE_QUOTE;
13+
import static dev.toonformat.jtoon.util.Constants.LIST_ITEM_PREFIX;
1014
import static dev.toonformat.jtoon.util.Headers.ARRAY_HEADER_PATTERN;
1115
import static dev.toonformat.jtoon.util.Headers.TABULAR_HEADER_PATTERN;
1216

@@ -47,10 +51,10 @@ static Delimiter extractDelimiterFromHeader(String header, DecodeContext context
4751
if (matcher.find()) {
4852
String delimiter = matcher.group(3);
4953
if (delimiter != null) {
50-
if ("\t".equals(delimiter)) {
54+
if (Delimiter.TAB.toString().equals(delimiter)) {
5155
return Delimiter.TAB;
5256
}
53-
if ("|".equals(delimiter)) {
57+
if (Delimiter.PIPE.toString().equals(delimiter)) {
5458
return Delimiter.PIPE;
5559
}
5660
}
@@ -82,7 +86,7 @@ static List<Object> parseArrayWithDelimiter(String header, int depth, Delimiter
8286
int headerEndIdx = arrayMatcher.end();
8387
String afterHeader = header.substring(headerEndIdx).trim();
8488

85-
if (afterHeader.startsWith(":")) {
89+
if (afterHeader.startsWith(COLON)) {
8690
String inlineContent = afterHeader.substring(1).trim();
8791

8892
if (!inlineContent.isEmpty()) {
@@ -106,7 +110,7 @@ static List<Object> parseArrayWithDelimiter(String header, int depth, Delimiter
106110
return Collections.emptyList();
107111
}
108112

109-
if (nextContent.startsWith("- ")) {
113+
if (nextContent.startsWith(LIST_ITEM_PREFIX)) {
110114
context.currentLine--;
111115
return parseListArray(depth, header, context);
112116
} else {
@@ -195,11 +199,11 @@ static List<String> parseDelimitedValues(String input, Delimiter arrayDelimiter)
195199
stringBuilder.append(currentChar);
196200
escaped = false;
197201
i++;
198-
} else if (currentChar == '\\') {
202+
} else if (currentChar == BACKSLASH) {
199203
stringBuilder.append(currentChar);
200204
escaped = true;
201205
i++;
202-
} else if (currentChar == '"') {
206+
} else if (currentChar == DOUBLE_QUOTE) {
203207
stringBuilder.append(currentChar);
204208
inQuotes = !inQuotes;
205209
i++;

src/main/java/dev/toonformat/jtoon/decoder/DecodeHelper.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import java.util.List;
66
import java.util.Map;
77

8+
import static dev.toonformat.jtoon.util.Constants.BACKSLASH;
9+
import static dev.toonformat.jtoon.util.Constants.DOUBLE_QUOTE;
10+
import static dev.toonformat.jtoon.util.Constants.SPACE;
11+
import static dev.toonformat.jtoon.util.Constants.COLON;
12+
813
/**
914
* Handles indentation, depth, conflicts, and validation for other decode classes.
1015
*/
@@ -47,7 +52,7 @@ private static int computeLeadingSpaces(String line, DecodeContext context) {
4752
int lengthOfLine = line.length();
4853
while (i < lengthOfLine) {
4954
char c = line.charAt(i);
50-
if (c == ' ') {
55+
if (c == SPACE.charAt(0)) {
5156
leadingSpaces++;
5257
} else if (c == Delimiter.TAB.getValue()) {
5358
if (context.options.strict()) {
@@ -98,11 +103,11 @@ static int findUnquotedColon(String content) {
98103

99104
if (escaped) {
100105
escaped = false;
101-
} else if (c == '\\') {
106+
} else if (c == BACKSLASH) {
102107
escaped = true;
103-
} else if (c == '"') {
108+
} else if (c == DOUBLE_QUOTE) {
104109
inQuotes = !inQuotes;
105-
} else if (c == ':' && !inQuotes) {
110+
} else if (c == COLON.charAt(0) && !inQuotes) {
106111
return i;
107112
}
108113
}

src/main/java/dev/toonformat/jtoon/decoder/KeyDecoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Map;
1010
import java.util.regex.Matcher;
1111

12+
import static dev.toonformat.jtoon.util.Constants.DOT;
1213
import static dev.toonformat.jtoon.util.Headers.KEYED_ARRAY_PATTERN;
1314

1415
/**
@@ -156,7 +157,7 @@ static boolean shouldExpandKey(String key, DecodeContext context) {
156157
return false;
157158
}
158159
// Check if a key contains dots and is a valid identifier pattern
159-
if (!key.contains(".")) {
160+
if (!key.contains(DOT)) {
160161
return false;
161162
}
162163
// Valid identifier: starts with a letter or underscore, followed by letters,

src/main/java/dev/toonformat/jtoon/decoder/ListItemDecoder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.Map;
99
import java.util.regex.Matcher;
1010

11+
import static dev.toonformat.jtoon.util.Constants.LIST_ITEM_MARKER;
12+
import static dev.toonformat.jtoon.util.Constants.OPEN_BRACKET;
1113
import static dev.toonformat.jtoon.util.Headers.KEYED_ARRAY_PATTERN;
1214

1315
/**
@@ -31,7 +33,7 @@ public static void processListArrayItem(String line, int lineDepth, int depth,
3133
if (lineDepth == depth + 1) {
3234
String content = line.substring((depth + 1) * context.options.indent());
3335

34-
if (content.startsWith("-")) {
36+
if (content.startsWith(LIST_ITEM_MARKER)) {
3537
result.add(parseListItem(content, depth, context));
3638
} else {
3739
context.currentLine++;
@@ -66,7 +68,7 @@ public static Object parseListItem(String content, int depth, DecodeContext cont
6668
}
6769

6870
// Check for standalone array (e.g., "[2]: 1,2")
69-
if (itemContent.startsWith("[")) {
71+
if (itemContent.startsWith(OPEN_BRACKET)) {
7072
// For nested arrays in list items, default to comma delimiter if not specified
7173
Delimiter nestedArrayDelimiter = ArrayDecoder.extractDelimiterFromHeader(itemContent, context);
7274
// parseArrayWithDelimiter handles currentLine increment internally

src/main/java/dev/toonformat/jtoon/decoder/PrimitiveDecoder.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import dev.toonformat.jtoon.util.StringEscaper;
44

5+
import static dev.toonformat.jtoon.util.Constants.DOT;
6+
import static dev.toonformat.jtoon.util.Constants.NULL_LITERAL;
7+
import static dev.toonformat.jtoon.util.Constants.TRUE_LITERAL;
8+
import static dev.toonformat.jtoon.util.Constants.FALSE_LITERAL;
9+
510
/**
611
* Handles parsing of primitive TOON values with type inference.
712
*
@@ -48,13 +53,13 @@ static Object parse(String value) {
4853

4954
// Check for null literal
5055
switch (value) {
51-
case "null" -> {
56+
case NULL_LITERAL -> {
5257
return null;
5358
}
54-
case "true" -> {
59+
case TRUE_LITERAL -> {
5560
return true;
5661
}
57-
case "false" -> {
62+
case FALSE_LITERAL -> {
5863
return false;
5964
}
6065
default -> {
@@ -78,7 +83,7 @@ static Object parse(String value) {
7883
// Try parsing as number
7984
try {
8085
// Check if it contains exponent notation or decimal point
81-
if (value.contains(".") || value.contains("e") || value.contains("E")) {
86+
if (value.contains(DOT) || value.contains("e") || value.contains("E")) {
8287
double parsed = Double.parseDouble(value);
8388
// Handle negative zero - Java doesn't distinguish, but spec says it should be 0
8489
if (parsed == 0.0) {

src/main/java/dev/toonformat/jtoon/decoder/TabularArrayDecoder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.Map;
1111
import java.util.regex.Matcher;
1212

13+
import static dev.toonformat.jtoon.util.Constants.BACKSLASH;
14+
import static dev.toonformat.jtoon.util.Constants.DOUBLE_QUOTE;
1315
import static dev.toonformat.jtoon.util.Headers.TABULAR_HEADER_PATTERN;
1416

1517
/**
@@ -101,9 +103,9 @@ private static void validateKeysDelimiter(String keysStr, Delimiter expectedDeli
101103
char c = keysStr.charAt(i);
102104
if (escaped) {
103105
escaped = false;
104-
} else if (c == '\\') {
106+
} else if (c == BACKSLASH) {
105107
escaped = true;
106-
} else if (c == '"') {
108+
} else if (c == DOUBLE_QUOTE) {
107109
inQuotes = !inQuotes;
108110
} else if (!inQuotes) {
109111
checkDelimiterMismatch(expectedChar, c);

src/main/java/dev/toonformat/jtoon/decoder/ValueDecoder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.LinkedHashMap;
88
import java.util.regex.Matcher;
99

10+
import static dev.toonformat.jtoon.util.Constants.NULL_LITERAL;
11+
import static dev.toonformat.jtoon.util.Constants.OPEN_BRACKET;
1012
import static dev.toonformat.jtoon.util.Headers.KEYED_ARRAY_PATTERN;
1113

1214
/**
@@ -52,7 +54,7 @@ public static Object decode(String toon, DecodeOptions options) {
5254

5355
// Special case: if input is exactly "null", return null
5456
String trimmed = toon.trim();
55-
if ("null".equals(trimmed)) {
57+
if (NULL_LITERAL.equals(trimmed)) {
5658
return null;
5759
}
5860

@@ -78,7 +80,7 @@ public static Object decode(String toon, DecodeOptions options) {
7880
}
7981

8082
// Handle standalone arrays: [2]:
81-
if (!line.isEmpty() && line.charAt(0) == '[') {
83+
if (!line.isEmpty() && line.charAt(0) == OPEN_BRACKET.charAt(0)) {
8284
return ArrayDecoder.parseArray(line, depth, context);
8385
}
8486

src/main/java/dev/toonformat/jtoon/encoder/ArrayEncoder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.toonformat.jtoon.encoder;
22

3-
43
import dev.toonformat.jtoon.EncodeOptions;
54
import tools.jackson.databind.JsonNode;
65
import tools.jackson.databind.node.ArrayNode;

src/main/java/dev/toonformat/jtoon/encoder/Flatten.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.Set;
1111
import java.util.regex.Pattern;
1212

13+
import static dev.toonformat.jtoon.util.Constants.DOT;
14+
1315
/**
1416
* Recursively flattens a JSON object or array into a single-level object.
1517
*/
@@ -75,7 +77,7 @@ public static FoldResult tryFoldKeyChain(String key,
7577
}
7678

7779
// start chain from absolute key
78-
String absKey = (pathPrefix == null) ? key : String.join(".", pathPrefix, key);
80+
String absKey = (pathPrefix == null) ? key : String.join(DOT, pathPrefix, key);
7981

8082
// Collect segments of the single-key chain
8183
final ChainResult chain = collectSingleKeyChain(absKey, value, remainingDepth);
@@ -97,7 +99,7 @@ public static FoldResult tryFoldKeyChain(String key,
9799
}
98100

99101
// Build folded key
100-
String foldedKey = String.join(".", chain.segments);
102+
String foldedKey = String.join(DOT, chain.segments);
101103

102104
// Detect collisions with sibling keys
103105
if (siblings.contains(foldedKey)) {
@@ -107,7 +109,7 @@ public static FoldResult tryFoldKeyChain(String key,
107109
// Compute absolute dotted path
108110
String absolutePath =
109111
(pathPrefix != null && !pathPrefix.isEmpty())
110-
? String.join(".", pathPrefix, foldedKey)
112+
? String.join(DOT, pathPrefix, foldedKey)
111113
: foldedKey;
112114

113115

@@ -138,8 +140,8 @@ public static FoldResult tryFoldKeyChain(String key,
138140
*/
139141
private static ChainResult collectSingleKeyChain(String startKey, JsonNode startValue, int maxDepth) {
140142
// normalize absolute key to its local segment
141-
String localStartKey = startKey.contains(".")
142-
? startKey.substring(startKey.lastIndexOf('.') + 1)
143+
String localStartKey = startKey.contains(DOT)
144+
? startKey.substring(startKey.lastIndexOf(DOT.charAt(0)) + 1)
143145
: startKey;
144146

145147
final List<String> segments = new ArrayList<>();

src/main/java/dev/toonformat/jtoon/encoder/HeaderFormatter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
import java.util.List;
44

5-
import static dev.toonformat.jtoon.util.Constants.*;
5+
import static dev.toonformat.jtoon.util.Constants.COLON;
6+
import static dev.toonformat.jtoon.util.Constants.OPEN_BRACKET;
7+
import static dev.toonformat.jtoon.util.Constants.COMMA;
8+
import static dev.toonformat.jtoon.util.Constants.OPEN_BRACE;
9+
import static dev.toonformat.jtoon.util.Constants.CLOSE_BRACE;
10+
import static dev.toonformat.jtoon.util.Constants.CLOSE_BRACKET;
11+
import static dev.toonformat.jtoon.util.Constants.HASHTAG;
612

713
/**
814
* Formats headers for arrays and tables in TOON format.
@@ -81,7 +87,7 @@ private static void appendArrayLength(
8187
header.append(OPEN_BRACKET);
8288

8389
if (lengthMarker) {
84-
header.append("#");
90+
header.append(HASHTAG);
8591
}
8692

8793
header.append(length);

0 commit comments

Comments
 (0)