Skip to content

Commit ac083ef

Browse files
committed
Separate compute method from result
1 parent 08e9345 commit ac083ef

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

SNIPPETS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This file was generated by running [AggregateSnippets.java](src/main/java/Aggreg
2020
- [**Verify**](#verify)
2121
- [**Verify v2**](#verify-v2)
2222
- [**Voice**](#voice)
23+
2324
## Initialize
2425
### Application Auth With Key Contents
2526

src/main/java/AggregateSnippets.java

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,47 @@
77
import java.util.*;
88

99
public final class AggregateSnippets {
10-
record CodeSnippetFile(
10+
public record CodeSnippetFile(
1111
Path file,
1212
int mainStartIndex, int mainEndIndex,
1313
int clientStartIndex, int clientEndIndex
1414
) {
1515

1616
}
1717

18-
private final StringBuilder sb = new StringBuilder(1 << 17);
18+
private StringBuilder sb;
1919
private final Path snippetsSrcRoot;
20-
private final Collection<CodeSnippetFile> snippetFiles = new LinkedHashSet<>(256);
20+
private Collection<CodeSnippetFile> snippetFiles;
2121

2222
public AggregateSnippets(Path snippetsSrcRoot) {
2323
this.snippetsSrcRoot = Objects.requireNonNull(snippetsSrcRoot);
2424
}
2525

26-
public String computeContents() throws IOException {
27-
final String classFileName = "AggregateSnippets.java";
28-
sb.append("# Vonage Java SDK Code Snippets\n")
26+
private void checkComputed() {
27+
if (sb == null || sb.isEmpty()) {
28+
throw new IllegalStateException("Contents not computed yet.");
29+
}
30+
}
31+
32+
public Collection<CodeSnippetFile> getLineNumbers() {
33+
checkComputed();
34+
return snippetFiles;
35+
}
36+
37+
public String getContents() {
38+
checkComputed();
39+
return sb.toString();
40+
}
41+
42+
public void saveToFile(Path destPath) throws IOException {
43+
Files.writeString(destPath, getContents(), StandardOpenOption.CREATE);
44+
}
45+
46+
public void computeContents() throws IOException {
47+
snippetFiles = new LinkedHashSet<>(256);
48+
final String classFileName = getClass().getSimpleName() + ".java";
49+
sb = new StringBuilder(1 << 17)
50+
.append("# Vonage Java SDK Code Snippets\n")
2951
.append("Here are all the snippets in this repository.\n")
3052
.append("This file was generated by running [").append(classFileName)
3153
.append("](src/main/java/").append(classFileName)
@@ -39,13 +61,11 @@ public String computeContents() throws IOException {
3961
sb.append("\n- [**").append(title).append("**](#")
4062
.append(title.toLowerCase().replace(' ', '-')).append(")");
4163
}
42-
sb.append("\n");
64+
sb.append("\n\n");
4365

4466
for (var file : allDirs) {
4567
appendSnippetContent(file, 2);
4668
}
47-
48-
return sb.toString();
4969
}
5070

5171
private List<File> getAllDirsSorted() {
@@ -95,6 +115,10 @@ else if (level > 2 && path.getName().endsWith(".java")) {
95115
.stripTrailing().stripIndent().replace("\t", " ");
96116

97117
sb.append("\n```java\n").append(nugget).append("\n```\n");
118+
119+
snippetFiles.add(new CodeSnippetFile(
120+
path.toPath(), startIndex, endIndex, clientInitStartIndex, clientInitEndIndex
121+
));
98122
}
99123
}
100124

@@ -130,9 +154,9 @@ private static String toHeadingTitle(String title) {
130154
public static void main(String[] args) throws Throwable {
131155
final var repoRoot = Paths.get("").toAbsolutePath();
132156
final var snippetsSrcRoot = repoRoot.resolve("src/main/java/com/vonage/quickstart");
133-
final String contents = new AggregateSnippets(snippetsSrcRoot).computeContents();
157+
final var aggregator = new AggregateSnippets(snippetsSrcRoot);
158+
aggregator.computeContents();
134159
var destPath = repoRoot.resolve("SNIPPETS.md");
135-
Files.deleteIfExists(destPath);
136-
Files.writeString(destPath, contents, StandardOpenOption.CREATE_NEW);
160+
aggregator.saveToFile(destPath);
137161
}
138162
}

0 commit comments

Comments
 (0)