From 65ea2a206e41c5bb4128f3060d910e685b62f8ee Mon Sep 17 00:00:00 2001 From: Graham Kirby Date: Tue, 24 Dec 2024 17:06:34 +0000 Subject: [PATCH] Tidying. --- .../race_timing/common/output/RaceOutput.java | 33 ++++++++++++------- .../common/output/RaceOutputCSV.java | 2 +- .../common/output/RaceOutputHTML.java | 20 +++-------- .../common/output/RaceOutputPDF.java | 4 +-- .../common/output/RaceOutputText.java | 8 ++--- .../relay_race/RelayRaceOutputCSV.java | 22 +++---------- .../relay_race/RelayRaceOutputHTML.java | 7 ++-- .../relay_race/RelayRaceOutputText.java | 14 ++------ .../series_race/tour/TourRaceOutputHTML.java | 3 +- 9 files changed, 42 insertions(+), 71 deletions(-) diff --git a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutput.java b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutput.java index 4eff9bf..f74db3e 100644 --- a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutput.java +++ b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutput.java @@ -25,7 +25,9 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.file.Files; +import java.nio.file.OpenOption; import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.util.Collection; import java.util.List; import java.util.function.Function; @@ -43,19 +45,16 @@ public abstract class RaceOutput { protected final Race race; - protected Path output_directory_path; protected String year; protected String race_name_for_results; protected String race_name_for_filenames; - private String overall_results_filename; - String prizes_filename; - String notes_filename; + private Path output_directory_path; ////////////////////////////////////////////////////////////////////////////////////////////////// protected abstract String getFileSuffix(); protected abstract String getResultsHeader(); - protected abstract String getPrizesSectionHeader(); + protected abstract String getPrizesHeader(); protected abstract String getPrizeCategoryHeader(final PrizeCategory category) ; protected abstract String getPrizeCategoryFooter(); @@ -79,7 +78,7 @@ public abstract class RaceOutput { */ public void printResults() throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(overall_results_filename + getFileSuffix())); + final OutputStream stream = getOutputStream(race_name_for_filenames, "overall", year); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { @@ -90,17 +89,31 @@ public void printResults() throws IOException { public void printPrizes() throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(prizes_filename + getFileSuffix())); + final OutputStream stream = getOutputStream(race_name_for_filenames, "prizes", year); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { - writer.append(getPrizesSectionHeader()); + writer.append(getPrizesHeader()); printPrizes(writer); } } ////////////////////////////////////////////////////////////////////////////////////////////////// + protected OutputStream getOutputStream(final String race_name, final String output_type, final String year) throws IOException { + return getOutputStream(race_name, output_type, year, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); + } + + protected OutputStream getOutputStream(final String race_name, final String output_type, final String year, final OpenOption... options) throws IOException { + return Files.newOutputStream(getOutputFilePath(race_name, output_type, year), options); + } + + Path getOutputFilePath(final String race_name, final String output_type, final String year) { + return output_directory_path.resolve(STR."\{race_name}_\{output_type}_\{year}\{getFileSuffix()}"); + } + + ////////////////////////////////////////////////////////////////////////////////////////////////// + private void configure() { readProperties(); @@ -117,10 +130,6 @@ private void readProperties() { protected void constructFilePaths() { - overall_results_filename = STR."\{race_name_for_filenames}_overall_\{year}"; - prizes_filename = STR."\{race_name_for_filenames}_prizes_\{year}"; - notes_filename = STR."\{race_name_for_filenames}_processing_notes_\{year}"; - output_directory_path = race.getPath("../output"); } diff --git a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputCSV.java b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputCSV.java index 6a58242..bdea8a9 100644 --- a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputCSV.java +++ b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputCSV.java @@ -45,7 +45,7 @@ public String getFileSuffix() { } @Override - public String getPrizesSectionHeader() { + public String getPrizesHeader() { return ""; } diff --git a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputHTML.java b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputHTML.java index bfc94cd..2d4e372 100644 --- a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputHTML.java +++ b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputHTML.java @@ -22,16 +22,13 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.nio.file.Files; import java.nio.file.StandardOpenOption; import static org.grahamkirby.race_timing.common.Race.LINE_SEPARATOR; public abstract class RaceOutputHTML extends RaceOutput { - protected static final String SOFTWARE_CREDIT_LINK_TEXT = "

Results generated using race-timing.

"; - - protected String combined_results_filename; + private static final String SOFTWARE_CREDIT_LINK_TEXT = "

Results generated using race-timing.

"; ////////////////////////////////////////////////////////////////////////////////////////////////// @@ -43,7 +40,7 @@ protected RaceOutputHTML(final Race race) { public void printCreditLink() throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(combined_results_filename + getFileSuffix()), StandardOpenOption.APPEND); + final OutputStream stream = getOutputStream(race_name_for_filenames, "combined", year, StandardOpenOption.APPEND); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { writer.append(SOFTWARE_CREDIT_LINK_TEXT); @@ -52,12 +49,12 @@ public void printCreditLink() throws IOException { public void printCombined() throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(combined_results_filename + getFileSuffix())); + final OutputStream stream = getOutputStream(race_name_for_filenames, "combined", year); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { writer.append("

Results

").append(LINE_SEPARATOR); - writer.append(getPrizesSectionHeader()); + writer.append(getPrizesHeader()); printPrizes(writer); @@ -68,20 +65,13 @@ public void printCombined() throws IOException { ////////////////////////////////////////////////////////////////////////////////////////////////// - @Override - protected void constructFilePaths() { - - super.constructFilePaths(); - combined_results_filename = STR."\{race_name_for_filenames}_combined_\{year}"; - } - @Override public String getFileSuffix() { return ".html"; } @Override - public String getPrizesSectionHeader() { + public String getPrizesHeader() { return STR."

Prizes

\{LINE_SEPARATOR}"; } diff --git a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputPDF.java b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputPDF.java index c8bd041..44ac979 100644 --- a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputPDF.java +++ b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputPDF.java @@ -48,7 +48,7 @@ protected RaceOutputPDF(final Race race) { @Override public void printPrizes() throws IOException { - final PdfWriter writer = new PdfWriter(output_directory_path.resolve(prizes_filename + getFileSuffix()).toString()); + final PdfWriter writer = new PdfWriter(getOutputFilePath(race_name_for_filenames, "prizes", year).toString()); try (final Document document = new Document(new PdfDocument(writer))) { printPrizes(document); @@ -61,7 +61,7 @@ protected String getFileSuffix() { } @Override - protected String getPrizesSectionHeader() { + protected String getPrizesHeader() { return ""; } diff --git a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputText.java b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputText.java index 6f82d53..70e1969 100644 --- a/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputText.java +++ b/src/main/java/org/grahamkirby/race_timing/common/output/RaceOutputText.java @@ -20,9 +20,7 @@ import org.grahamkirby.race_timing.common.categories.PrizeCategory; import java.io.IOException; -import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.nio.file.Files; import static org.grahamkirby.race_timing.common.Race.LINE_SEPARATOR; @@ -38,7 +36,7 @@ protected String getFileSuffix() { } @Override - protected String getPrizesSectionHeader() { + protected String getPrizesHeader() { return STR.""" \{race_name_for_results} Results \{year} ============================ @@ -74,9 +72,7 @@ public void printNotes() throws IOException { reduce((s1, s2) -> STR."\{s1}, \{s2}"). ifPresent(s -> race.getNotes().append("Converted to title case: ").append(s)); - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(notes_filename + getFileSuffix())); - - try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { + try (final OutputStreamWriter writer = new OutputStreamWriter(getOutputStream(race_name_for_filenames, "processing_notes", year))) { writer.append(race.getNotes().toString()); } } diff --git a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputCSV.java b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputCSV.java index 40219fe..706aaec 100644 --- a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputCSV.java +++ b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputCSV.java @@ -25,18 +25,13 @@ import org.grahamkirby.race_timing.common.output.ResultPrinterCSV; import java.io.IOException; -import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.nio.file.Files; import java.util.List; import static org.grahamkirby.race_timing.common.Normalisation.format; import static org.grahamkirby.race_timing.common.Race.LINE_SEPARATOR; -import static org.grahamkirby.race_timing.common.Race.SUFFIX_CSV; -public class RelayRaceOutputCSV extends RaceOutputCSV { - - private final String detailed_results_filename = STR."\{race_name_for_filenames}_detailed_\{year}"; +class RelayRaceOutputCSV extends RaceOutputCSV { private static final String OVERALL_RESULTS_HEADER = "Pos,No,Team,Category,"; @@ -63,9 +58,7 @@ protected ResultPrinter getOverallResultPrinter(final OutputStreamWriter writer) void printDetailedResults() throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(detailed_results_filename + SUFFIX_CSV)); - - try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { + try (final OutputStreamWriter writer = new OutputStreamWriter(getOutputStream(race_name_for_filenames, "detailed", year))) { printDetailedResultsHeader(writer); printDetailedResults(writer); @@ -91,11 +84,8 @@ private void printDetailedResults(final OutputStreamWriter writer) throws IOExce final ResultPrinter printer = new DetailedResultPrinter(race, writer); - for (final PrizeCategoryGroup group : race.prize_category_groups) { - - final List results = race.getOverallResults(group.categories()); - printer.print(results); - } + for (final PrizeCategoryGroup group : race.prize_category_groups) + printer.print(race.getOverallResults(group.categories())); } ////////////////////////////////////////////////////////////////////////////////////////////////// @@ -108,9 +98,7 @@ void printLegResults() throws IOException { private void printLegResults(final int leg) throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(STR."\{race_name_for_filenames}_leg_\{leg}_\{year}.csv")); - - try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { + try (final OutputStreamWriter writer = new OutputStreamWriter(getOutputStream(race_name_for_filenames, STR."leg_\{leg}", year))) { printLegResults(writer, leg); } } diff --git a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputHTML.java b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputHTML.java index a6ded13..f48ffbf 100644 --- a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputHTML.java +++ b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputHTML.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.nio.file.Files; import java.nio.file.StandardOpenOption; import java.util.List; @@ -49,7 +48,7 @@ public void printCombined() throws IOException { super.printCombined(); - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(combined_results_filename + getFileSuffix()), StandardOpenOption.APPEND); + final OutputStream stream = getOutputStream(race_name_for_filenames, "combined", year, StandardOpenOption.APPEND); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { @@ -87,7 +86,7 @@ protected ResultPrinter getPrizeResultPrinter(final OutputStreamWriter writer) { void printDetailedResults() throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(detailed_results_filename + getFileSuffix())); + final OutputStream stream = getOutputStream(race_name_for_filenames, "detailed", year); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { printDetailedResults(writer); @@ -109,7 +108,7 @@ void printLegResults() throws IOException { private void printLegResults(final int leg) throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(STR."\{race_name_for_filenames}_leg_\{leg}_\{year}.html")); + final OutputStream stream = getOutputStream(race_name_for_filenames, STR."leg_\{leg}", year); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { printLegResults(writer, leg); diff --git a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputText.java b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputText.java index e64949d..fde1ccf 100644 --- a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputText.java +++ b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceOutputText.java @@ -17,15 +17,14 @@ package org.grahamkirby.race_timing.relay_race; import org.grahamkirby.race_timing.common.*; -import org.grahamkirby.race_timing.common.output.ResultPrinterText; import org.grahamkirby.race_timing.common.output.RaceOutputText; import org.grahamkirby.race_timing.common.output.ResultPrinter; +import org.grahamkirby.race_timing.common.output.ResultPrinterText; import org.grahamkirby.race_timing.single_race.SingleRace; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.nio.file.Files; import java.time.Duration; import java.util.*; import java.util.stream.Stream; @@ -36,8 +35,6 @@ class RelayRaceOutputText extends RaceOutputText { - private String collated_times_filename; - RelayRaceOutputText(final RelayRace results) { super(results); @@ -45,17 +42,10 @@ class RelayRaceOutputText extends RaceOutputText { ////////////////////////////////////////////////////////////////////////////////////////////////// - @Override - protected void constructFilePaths() { - - super.constructFilePaths(); - collated_times_filename = STR."\{race_name_for_filenames}_times_collated_\{year}"; - } - @SuppressWarnings("IncorrectFormatting") void printCollatedResults() throws IOException { - final OutputStream stream = Files.newOutputStream(output_directory_path.resolve(STR."\{collated_times_filename}.txt")); + final OutputStream stream = getOutputStream(race_name_for_filenames, "times_collated", year); try (final OutputStreamWriter writer = new OutputStreamWriter(stream)) { diff --git a/src/main/java/org/grahamkirby/race_timing/series_race/tour/TourRaceOutputHTML.java b/src/main/java/org/grahamkirby/race_timing/series_race/tour/TourRaceOutputHTML.java index a06e66b..e56d960 100644 --- a/src/main/java/org/grahamkirby/race_timing/series_race/tour/TourRaceOutputHTML.java +++ b/src/main/java/org/grahamkirby/race_timing/series_race/tour/TourRaceOutputHTML.java @@ -31,7 +31,6 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.nio.file.Files; import java.util.Collection; import java.util.List; @@ -70,7 +69,7 @@ private void printIndividualRaceResults(final int race_number) throws IOExceptio if (individual_race != null) { - final OutputStream race_stream = Files.newOutputStream(output_directory_path.resolve(STR."\{race_name_for_filenames}_race\{race_number}_\{year}\{getFileSuffix()}")); + final OutputStream race_stream = getOutputStream(race_name_for_filenames, STR."race\{race_number}", year); try (final OutputStreamWriter writer = new OutputStreamWriter(race_stream)) {