Skip to content

Commit 904bb33

Browse files
authored
Add Java toIterable goody (#164)
To be used in a for-each loop, an object must implement the Iterable interface. Similar to the TypeScript counterpart, let's add a Java goody that can transform an Iterator into an Iterable.
1 parent 722277e commit 904bb33

File tree

10 files changed

+70
-3
lines changed

10 files changed

+70
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.Iterator;
2+
3+
final class AP {
4+
5+
public static <T> Iterable<T> toIterable(Iterator<T> iterator) {
6+
return () -> iterator;
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "toIterable"
3+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ final class AP {
108108
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
109109
`;
110110

111+
exports[`App can equip single goody: Java toIterable 1`] = `
112+
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
113+
// Adventure Pack commit fake-commit-hash
114+
// Running at: https://example.com/
115+
116+
import java.util.Iterator;
117+
118+
final class AP {
119+
private AP() {}
120+
121+
public static <T> Iterable<T> toIterable(Iterator<T> iterator) {
122+
return () -> iterator;
123+
}
124+
}
125+
126+
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
127+
`;
128+
111129
exports[`App can equip single goody: JavaScript Array.prototype.slidingWindows 1`] = `
112130
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
113131
// Adventure Pack commit fake-commit-hash

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ final class AP {
7878
}"
7979
`;
8080

81+
exports[`App can render goody: Java toIterable 1`] = `
82+
"package to_iterable;
83+
84+
import java.util.Iterator;
85+
86+
final class AP {
87+
private AP() {}
88+
89+
public static <T> Iterable<T> toIterable(Iterator<T> iterator) {
90+
return () -> iterator;
91+
}
92+
}"
93+
`;
94+
8195
exports[`App can render goody: JavaScript Array.prototype.slidingWindows 1`] = `
8296
"class ArraySlice {
8397
constructor(array, start, end) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ const ADVENTURE_PACK_CLASS_NAME = "AP";
77

88
export function mergeJavaCode(goodies: Iterable<ReadonlyDeep<JavaGoody>>) {
99
const classes: Record<string, { code: string[]; declaration: string }> = {};
10+
const coreImports: Set<string> = new Set();
11+
1012
for (const goody of goodies) {
13+
for (const im of goody.coreImports) {
14+
coreImports.add(im);
15+
}
16+
1117
for (const className of Object.keys(goody.codeByClass)) {
1218
invariant(
1319
classes[className] == null || className === ADVENTURE_PACK_CLASS_NAME,
@@ -30,6 +36,10 @@ export function mergeJavaCode(goodies: Iterable<ReadonlyDeep<JavaGoody>>) {
3036
}
3137

3238
const res: string[] = [];
39+
if (coreImports.size > 0) {
40+
res.push([...coreImports].sort().join("\n"));
41+
}
42+
3343
for (const className of Object.keys(classes)) {
3444
const classData = classes[className];
3545
const codeSections = classData.code.filter(Boolean);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const javaGoodyParser = goodyBaseParser
1515
})
1616
.strict(),
1717
),
18+
coreImports: z.array(nonBlankStringParser),
1819
importsCode: z.string(),
1920
language: z.literal("java"),
2021
packageName: z

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { getLines, isStringEmptyOrWhitespaceOnly } from "@code-chronicles/util";
22

33
export function extractJavaesqueImports(code: string): {
44
codeWithoutImports: string;
5+
coreImports: string[];
56
imports: Set<string>;
67
importsCode: string;
78
} {
89
const lines = Array.from(getLines(code));
10+
11+
const coreImports: string[] = [];
912
const imports = new Set<string>();
1013
const importsCode: string[] = [];
1114

@@ -22,6 +25,14 @@ export function extractJavaesqueImports(code: string): {
2225
continue;
2326
}
2427

28+
const coreImmportMatch = lines[0].match(
29+
/^import\s+(?:static\s+)?(?:java|javafx|javax\.)/,
30+
);
31+
if (coreImmportMatch != null) {
32+
coreImports.push(lines.shift()!.replace(/\n+$/, ""));
33+
continue;
34+
}
35+
2536
const importMatch = lines[0].match(/^import\s+(?:static\s+)?([^\.]+)\./);
2637
if (importMatch != null) {
2738
importsCode.push(lines.shift()!);
@@ -41,6 +52,7 @@ export function extractJavaesqueImports(code: string): {
4152

4253
return {
4354
codeWithoutImports: lines.join(""),
55+
coreImports,
4456
imports,
4557
importsCode: importsCode.join(""),
4658
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ export async function readBaseGoody(
1616
readMetadata(packageName),
1717
]);
1818

19-
const { codeWithoutImports, imports, importsCode } =
19+
const { codeWithoutImports, coreImports, imports, importsCode } =
2020
extractJavaesqueImports(codeWithImports);
2121

2222
const codeByClass = splitCodeIntoClasses(codeWithoutImports);
2323

2424
return {
2525
codeByClass,
26+
coreImports,
2627
imports: Array.from(imports),
2728
importsCode,
2829
language: "java",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function readGoodies(): Promise<Record<string, JavaGoody>> {
4848
const importedBaseGoody = baseGoodiesByPackageName[im];
4949
invariant(
5050
importedBaseGoody != null,
51-
`Unknown import ${JSON.stringify(im)} in ${importedBaseGoody.name}`,
51+
`Unknown import ${JSON.stringify(im)} in goody ${JSON.stringify(baseGoody.name)}.`,
5252
);
5353
return importedBaseGoody.name;
5454
});

tools/adventure-pack/src/scripts/package-goodies/kotlin/readGoodies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function readGoodies(): Promise<Record<string, KotlinGoody>> {
4848
const importedBaseGoody = baseGoodiesByPackageName[im];
4949
invariant(
5050
importedBaseGoody != null,
51-
`Unknown import ${JSON.stringify(im)} in ${importedBaseGoody.name}`,
51+
`Unknown import ${JSON.stringify(im)} in goody ${JSON.stringify(baseGoody.name)}.`,
5252
);
5353
return importedBaseGoody.name;
5454
});

0 commit comments

Comments
 (0)