Skip to content

Commit

Permalink
Write empty tags.json
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderploegsma committed Oct 27, 2023
1 parent 23a86de commit 4b9b961
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
37 changes: 26 additions & 11 deletions src/main/java/analyzer/exercises/Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public abstract class Exercise {
private CompilationUnit compilationUnit;

private final JSONObject analysis = new JSONObject();
private final FileWriter fileWriter;
private final JSONObject tags = new JSONObject();
private final FileWriter analysisFileWriter;
private final FileWriter tagsFileWriter;

public enum WriteAnalysisToFile {YES, NO}

Expand All @@ -28,15 +30,17 @@ protected Exercise(String directory,
String outputDirectory,
WriteAnalysisToFile writeAnalysisToFile) {
this(getSolutionFile(directory, solutionFile),
getFileWriter(outputDirectory, writeAnalysisToFile));
getAnalysisFileWriter(outputDirectory, writeAnalysisToFile),
getTagsFileWriter(directory, writeAnalysisToFile));
}

protected Exercise(File solutionFile) {
this(solutionFile, NO_FILE_WRITER);
this(solutionFile, NO_FILE_WRITER, NO_FILE_WRITER);
}

private Exercise(File solutionFile, FileWriter fileWriter) {
this.fileWriter = fileWriter;
private Exercise(File solutionFile, FileWriter analysisFileWriter, FileWriter tagsFileWriter) {
this.analysisFileWriter = analysisFileWriter;
this.tagsFileWriter = tagsFileWriter;

try {
this.compilationUnit = JavaParser.parse(solutionFile);
Expand Down Expand Up @@ -65,20 +69,29 @@ protected Exercise(String directory,
String solutionFile,
WriteAnalysisToFile writeAnalysisToFile) {
this(getSolutionFile(directory, solutionFile),
getFileWriter(directory, writeAnalysisToFile));
getAnalysisFileWriter(directory, writeAnalysisToFile),
getTagsFileWriter(directory, writeAnalysisToFile));
}

private static File getSolutionFile(String directory, String solutionFile) {
return new File(directory + "src/main/java/" + solutionFile);
}

private static FileWriter getAnalysisFileWriter(String directory, WriteAnalysisToFile writeAnalysisToFile) {
return getFileWriter(directory, writeAnalysisToFile, "analysis.json");
}

private static FileWriter getTagsFileWriter(String directory, WriteAnalysisToFile writeAnalysisToFile) {
return getFileWriter(directory, writeAnalysisToFile, "tags.json");
}

private static FileWriter getFileWriter(
String directory, WriteAnalysisToFile writeAnalysisToFile) {
String directory, WriteAnalysisToFile writeAnalysisToFile, String filename) {
if (writeAnalysisToFile == WriteAnalysisToFile.NO) {
return null;
}
try {
return new FileWriter(directory + "analysis.json");
return new FileWriter(directory + filename);
} catch (IOException e) {
e.printStackTrace();
return null;
Expand Down Expand Up @@ -115,13 +128,15 @@ public JSONObject getAnalysis() {
}

public void writeAnalysisToFile() {
if (fileWriter == null) {
if (analysisFileWriter == null) {
return;
}

try {
fileWriter.write(analysis.toString());
fileWriter.flush();
analysisFileWriter.write(analysis.toString());
analysisFileWriter.flush();
tagsFileWriter.write(tags.toString());
tagsFileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/hamming/expected_tags.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{}

0 comments on commit 4b9b961

Please sign in to comment.