Skip to content

Commit e6b7a14

Browse files
committed
refactor: replace Map.of with HashMap for mutable collections
The change replaces immutable Map.of() with mutable HashMap instances where modifications are needed later. This affects test setup, workflow event handling, and HTTP server responses where collections need to be mutable after creation. Also adds @beforeeach setup in ExpressionParserTest to ensure clean state for each test and improves code consistency across the codebase.
1 parent 131c75d commit e6b7a14

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

src/main/java/mindustrytool/handler/HttpServer.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ public void init() {
414414
ctx.json(context.get().workflow.getWorkflowContext());
415415
} catch (WorkflowError e) {
416416
Log.err("Failed to load workflow", e);
417-
ctx.status(400).json(Map.of("message", "Failed to load workflow: " + e.getMessage()));
417+
HashMap<String, String> result = new HashMap<>();
418+
result.put("message", "Failed to load workflow: " + e.getMessage());
419+
ctx.status(400).json(result);
418420
}
419421
});
420422

@@ -444,25 +446,30 @@ public void init() {
444446
gameStats.put("unitsCreated", Vars.state.stats.unitsCreated);
445447
gameStats.put("wavesLasted", Vars.state.stats.wavesLasted);
446448

447-
data.put("executors", java.util.Map.of(
448-
"backgroundExecutor", context.get().BACKGROUND_TASK_EXECUTOR.toString(), //
449-
"backgroundScheduler", context.get().BACKGROUND_SCHEDULER.toString()//
450-
));
449+
HashMap<String, String> executors = new HashMap<>();
450+
executors.put("backgroundExecutor", context.get().BACKGROUND_TASK_EXECUTOR.toString());
451+
executors.put("backgroundScheduler", context.get().BACKGROUND_SCHEDULER.toString());
452+
453+
data.put("executors", executors);
451454

452455
data.put("gameStats", gameStats);
453456
data.put("locales", Vars.locales);
454457
data.put("threads",
455458
Thread.getAllStackTraces().keySet().stream()
456459
.sorted((a, b) -> a.getName().compareTo(b.getName()))
457-
.map(thread -> java.util.Map.of(
458-
"id", thread.getId(),
459-
"name", thread.getName(),
460-
"state", thread.getState().name(),
461-
"group",
462-
thread.getThreadGroup() == null ? "null" : thread.getThreadGroup().getName(),
463-
"stacktrace",
464-
Arrays.asList(thread.getStackTrace()).stream().map(stack -> stack.toString())
465-
.collect(Collectors.toList())))
460+
.map(thread -> {
461+
HashMap<String, Object> info = new HashMap<>();
462+
463+
info.put("id", thread.getId());
464+
info.put("name", thread.getName());
465+
info.put("state", thread.getState().name());
466+
info.put("group", thread.getThreadGroup() == null ? "null"
467+
: thread.getThreadGroup().getName());
468+
info.put("stacktrace", Arrays.asList(thread.getStackTrace()).stream()
469+
.map(stack -> stack.toString()).collect(Collectors.toList()));
470+
471+
return info;
472+
})
466473
.collect(Collectors.toList()));
467474

468475
data.put("activeRequest", activeRequests.values());
@@ -474,14 +481,18 @@ public void init() {
474481
maps.add(tags);
475482
});
476483
data.put("maps",
477-
Vars.maps.all().map(map -> java.util.Map.of(
478-
"name", map.name(), //
479-
"author", map.author(), //
480-
"file", map.file.absolutePath(),
481-
"tags", map.tags,
482-
"description", map.description(),
483-
"width", map.width,
484-
"height", map.height)).list());
484+
Vars.maps.all().map(map -> {
485+
HashMap<String, Object> info = new HashMap<>();
486+
info.put("name", map.name()); //
487+
info.put("author", map.author()); //
488+
info.put("file", map.file.absolutePath());
489+
info.put("tags", map.tags);
490+
info.put("description", map.description());
491+
info.put("width", map.width);
492+
info.put("height", map.height);
493+
494+
return info;
495+
}).list());
485496
data.put("mods", Vars.mods.list().map(mod -> mod.meta.toString()).list());
486497
data.put("votes", context.get().voteHandler.votes);
487498

@@ -499,7 +510,9 @@ public void init() {
499510
ctx.json(res);
500511
});
501512

502-
app.sse("workflow/events", client -> {
513+
app.sse("workflow/events", client ->
514+
515+
{
503516
client.keepAlive();
504517
client.sendComment("connected");
505518

@@ -514,8 +527,9 @@ public void init() {
514527
Log.err("Unhandled api exception", exception);
515528

516529
try {
517-
var result = java.util.Map.of("message",
518-
exception.getMessage() == null ? "Unknown error" : exception.getMessage());
530+
531+
HashMap<String, Object> result = new HashMap<>();
532+
result.put("message", exception.getMessage() == null ? "Unknown error" : exception.getMessage());
519533
ctx.status(500).json(result);
520534
} catch (Exception e) {
521535
Log.err("Failed to create error response", e);

src/main/java/mindustrytool/workflow/WorkflowEmitEvent.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public WorkflowEmitEvent putValue(String name, Object value) {
3030
variables.put(name, value);
3131
Log.debug("Add variable: " + name + " = " + value);
3232

33-
context.sendWorkflowEvent(new WorkflowEvent(current.getId(), "SET", Map.of(name, value.toString())));
33+
HashMap<String, Object> vars = new HashMap<>();
34+
35+
vars.put(name, value);
36+
37+
context.sendWorkflowEvent(new WorkflowEvent(current.getId(), "SET", vars));
3438

3539
return this;
3640
}
@@ -90,7 +94,9 @@ public void next(String nextId) {
9094
nextNode.execute(new WorkflowEmitEvent(step + 1, nextNode, context, variables));
9195
} catch (Exception e) {
9296
Log.err(e);
93-
context.sendWorkflowEvent(new WorkflowEvent(nextNode.getId(), "ERROR", Map.of("message", e.getMessage())));
97+
HashMap<String, Object> error = new HashMap<>();
98+
error.put("message", e.getMessage());
99+
context.sendWorkflowEvent(new WorkflowEvent(nextNode.getId(), "ERROR", error));
94100
}
95101
context.sendWorkflowEvent(new WorkflowEvent(nextNode.getId(), "EMIT", null));
96102
}

src/test/java/ExpressionParserTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import static org.junit.jupiter.api.Assertions.assertEquals;
22

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

6+
import org.junit.jupiter.api.BeforeEach;
57
import org.junit.jupiter.api.Test;
68

79
import mindustrytool.workflow.expressions.ExpressionParser;
810

911
public class ExpressionParserTest {
1012
ExpressionParser parser = new ExpressionParser();
1113

12-
Map<String, Object> variables = Map.of("a", 1d, "b", 2d);
14+
Map<String, Object> variables = new HashMap<>();
15+
16+
@BeforeEach
17+
void setUp() {
18+
variables.clear();
19+
variables.put("a", 1d);
20+
variables.put("b", 2d);
21+
}
1322

1423
@Test
1524
void testAddition() {

0 commit comments

Comments
 (0)