Skip to content

Commit

Permalink
Tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamkirby committed Dec 24, 2024
1 parent d0b983f commit 65ea2a2
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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)) {

Expand All @@ -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();
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String getFileSuffix() {
}

@Override
public String getPrizesSectionHeader() {
public String getPrizesHeader() {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<p style=\"font-size:smaller; font-style:italic;\">Results generated using <a href=\"https://github.com/grahamkirby/race-timing\">race-timing</a>.</p>";

protected String combined_results_filename;
private static final String SOFTWARE_CREDIT_LINK_TEXT = "<p style=\"font-size:smaller; font-style:italic;\">Results generated using <a href=\"https://github.com/grahamkirby/race-timing\">race-timing</a>.</p>";

//////////////////////////////////////////////////////////////////////////////////////////////////

Expand All @@ -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);
Expand All @@ -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("<h3>Results</h3>").append(LINE_SEPARATOR);
writer.append(getPrizesSectionHeader());
writer.append(getPrizesHeader());

printPrizes(writer);

Expand All @@ -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."<h4>Prizes</h4>\{LINE_SEPARATOR}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -61,7 +61,7 @@ protected String getFileSuffix() {
}

@Override
protected String getPrizesSectionHeader() {
protected String getPrizesHeader() {
return "";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +36,7 @@ protected String getFileSuffix() {
}

@Override
protected String getPrizesSectionHeader() {
protected String getPrizesHeader() {
return STR."""
\{race_name_for_results} Results \{year}
============================
Expand Down Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,";

Expand All @@ -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);
Expand All @@ -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<RaceResult> results = race.getOverallResults(group.categories());
printer.print(results);
}
for (final PrizeCategoryGroup group : race.prize_category_groups)
printer.print(race.getOverallResults(group.categories()));
}

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)) {

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,26 +35,17 @@

class RelayRaceOutputText extends RaceOutputText {

private String collated_times_filename;

RelayRaceOutputText(final RelayRace results) {

super(results);
}

//////////////////////////////////////////////////////////////////////////////////////////////////

@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)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)) {

Expand Down

0 comments on commit 65ea2a2

Please sign in to comment.