Skip to content

Commit e662ffc

Browse files
committed
Add Java Pair goody
Hypothetically we could use [`javafx.util.Pair`](https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html) but I don't love that the fields are named key and value. So let's reinvent the wheel!
1 parent 2c78683 commit e662ffc

File tree

8 files changed

+54
-17
lines changed

8 files changed

+54
-17
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package pair;
2+
3+
public record Pair<TFirst, TSecond>(TFirst first, TSecond second) {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "Pair"
3+
}

tools/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`App can equip single goody: Java Pair 1`] = `
4+
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
5+
// Adventure Pack commit fake-commit-hash
6+
// Running at: https://example.com/
7+
8+
record Pair<TFirst, TSecond>(TFirst first, TSecond second) {
9+
10+
}
11+
12+
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
13+
`;
14+
315
exports[`App can equip single goody: Java UnionFind 1`] = `
416
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
517
// Adventure Pack commit fake-commit-hash

tools/adventure-pack/src/app/__tests__/__snapshots__/render-test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`App can render goody: Java Pair 1`] = `
4+
"package pair;
5+
6+
record Pair<TFirst, TSecond>(TFirst first, TSecond second) {
7+
8+
}"
9+
`;
10+
311
exports[`App can render goody: Java UnionFind 1`] = `
412
"package union_find;
513

tools/adventure-pack/src/app/mergeJavaCode.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ import type { JavaGoody } from "./parsers/javaGoodyParser";
66
const ADVENTURE_PACK_CLASS_NAME = "AP";
77

88
export function mergeJavaCode(goodies: Iterable<ReadonlyDeep<JavaGoody>>) {
9-
const classes: Record<string, { code: string[]; modifiers: Set<string> }> =
10-
{};
9+
const classes: Record<string, { code: string[]; declaration: string }> = {};
1110
for (const goody of goodies) {
1211
for (const className of Object.keys(goody.codeByClass)) {
1312
invariant(
1413
classes[className] == null || className === ADVENTURE_PACK_CLASS_NAME,
1514
`Only the ${ADVENTURE_PACK_CLASS_NAME} class can exist in multiple goodies!`,
1615
);
1716

18-
classes[className] ??= { code: [], modifiers: new Set() };
19-
for (const modifier of goody.codeByClass[className].modifiers) {
20-
classes[className].modifiers.add(modifier);
21-
}
17+
classes[className] ??= {
18+
code: [],
19+
declaration: goody.codeByClass[className].declaration,
20+
};
2221
classes[className].code.push(goody.codeByClass[className].code);
2322
}
2423
}
@@ -33,7 +32,7 @@ export function mergeJavaCode(goodies: Iterable<ReadonlyDeep<JavaGoody>>) {
3332
const res: string[] = [];
3433
for (const className of Object.keys(classes)) {
3534
res.push(
36-
`${[...classes[className].modifiers, "class", className].join(" ")} {\n${classes[className].code.map((section) => section + "\n").join("\n")}}`,
35+
`${classes[className].declaration}\n${classes[className].code.map((section) => section + "\n").join("\n")}}`,
3736
);
3837
}
3938

tools/adventure-pack/src/app/parsers/javaGoodyParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const javaGoodyParser = goodyBaseParser
1111
z
1212
.object({
1313
code: z.string(),
14-
modifiers: z.array(nonBlankStringParser),
14+
declaration: nonBlankStringParser,
1515
})
1616
.strict(),
1717
),

tools/adventure-pack/src/scripts/package-goodies/java/splitCodeIntoClasses.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,35 @@ import {
88

99
export function splitCodeIntoClasses(
1010
code: string,
11-
): Record<string, { code: string; modifiers: string[] }> {
12-
const classes: Record<string, { code: string[]; modifiers: Set<string> }> =
13-
{};
11+
): Record<string, { code: string; declaration: string }> {
12+
const classes: Record<string, { code: string[]; declaration: string }> = {};
1413

1514
let currentClassName: string | null = null;
1615
for (const line of getLines(code)) {
1716
const classMatch = line.match(
18-
/^((?:(?:abstract|final|public)\s+)*)class\s+(\S+)\s*{/,
17+
/^((?:(?:abstract|final|public)\s+)*)((?:class|record)\s+(\S+)\s*[{<].*)$/s,
1918
);
2019
if (classMatch != null) {
2120
invariant(currentClassName == null, "Top-level class nesting?");
2221

2322
const modifiers = new Set(classMatch[1].trim().split(/\s+/));
2423
modifiers.delete("public");
2524

26-
currentClassName = classMatch[2];
27-
classes[currentClassName] = { code: [], modifiers };
25+
currentClassName = classMatch[3];
26+
classes[currentClassName] = {
27+
code: [],
28+
declaration: [
29+
...Array.from(modifiers).sort(),
30+
classMatch[2].replace(/}?\n*$/, ""),
31+
]
32+
.filter(Boolean)
33+
.join(" "),
34+
};
35+
36+
if (/}\n*$/.test(line)) {
37+
currentClassName = null;
38+
}
39+
2840
continue;
2941
}
3042

@@ -44,8 +56,8 @@ export function splitCodeIntoClasses(
4456

4557
invariant(currentClassName == null, "Unfinished class?");
4658

47-
return mapObjectValues(classes, ({ code, modifiers }) => ({
59+
return mapObjectValues(classes, ({ code, declaration }) => ({
4860
code: code.join("").replace(/^\n+/, "").replace(/\n+$/, ""),
49-
modifiers: Array.from(modifiers),
61+
declaration,
5062
}));
5163
}

tools/adventure-pack/src/scripts/package-goodies/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async function main(): Promise<void> {
1010
await fsPromises.writeFile(
1111
path.join("dist", "goodies.json"),
1212
// TODO: pretty print if NODE_ENV is "development"
13-
JSON.stringify(goodies) + "\n",
13+
JSON.stringify(goodies, null, 2) + "\n",
1414
);
1515
}
1616

0 commit comments

Comments
 (0)