From e05a300007ebb68642758a5a0cd7e441b85200e1 Mon Sep 17 00:00:00 2001 From: Graham Kirby Date: Wed, 27 Nov 2024 20:43:13 +0000 Subject: [PATCH] Tidying. --- .../grahamkirby/race_timing/common/Race.java | 17 +++++++-- .../race_timing/common/RawResult.java | 4 +- .../individual_race/IndividualRace.java | 3 +- .../race_timing/relay_race/RelayRace.java | 38 +++++++++++-------- .../relay_race/RelayRaceInput.java | 3 +- .../relay_race/RelayRaceMissingData.java | 6 ++- .../relay_race/RelayRaceOutputHTML.java | 6 +-- .../relay_race/RelayRaceOutputText.java | 5 ++- .../relay_race/RelayRaceRawResult.java | 4 +- .../fife_ac_midweek/MidweekRaceResult.java | 25 ++++++------ 10 files changed, 66 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/grahamkirby/race_timing/common/Race.java b/src/main/java/org/grahamkirby/race_timing/common/Race.java index ad5303e7..07d1c977 100644 --- a/src/main/java/org/grahamkirby/race_timing/common/Race.java +++ b/src/main/java/org/grahamkirby/race_timing/common/Race.java @@ -83,6 +83,10 @@ public abstract class Race { public static final String SUFFIX_CSV = ".csv"; public static final String SUFFIX_PDF = ".pdf"; + public static final int UNKNOWN_BIB_NUMBER = 0; + public static final int UNKNOWN_LEG_NUMBER = 0; + public static final int UNKNOWN_RACE_POSITION = 0; + private static final String DEFAULT_ENTRY_MAP_PATH = "/src/main/resources/configuration/default_entry_map" + SUFFIX_CSV; private static final String DEFAULT_NORMALISED_HTML_ENTITIES_PATH = "/src/main/resources/configuration/html_entities" + SUFFIX_CSV; private static final String DEFAULT_NORMALISED_CLUB_NAMES_PATH = "/src/main/resources/configuration/club_names" + SUFFIX_CSV; @@ -194,8 +198,8 @@ protected void printCombined() throws IOException { public Path getPath(final String path) { return path.startsWith("/") ? - getPathRelativeToProjectRoot(path) : - getPathRelativeToRaceConfigFile(path); + getPathRelativeToProjectRoot(path) : + getPathRelativeToRaceConfigFile(path); } public final boolean entryCategoryIsEligibleForPrizeCategory(final EntryCategory entry_category, final PrizeCategory prize_category) { @@ -205,12 +209,17 @@ public final boolean entryCategoryIsEligibleForPrizeCategory(final EntryCategory public boolean entryCategoryIsEligibleInSomePrizeCategory(final EntryCategory entry_category, final List prize_categories) { - return prize_categories.stream().map(category -> entryCategoryIsEligibleForPrizeCategory(entry_category, category)).reduce(Boolean::logicalOr).orElseThrow(); + return prize_categories.stream(). + map(category -> entryCategoryIsEligibleForPrizeCategory(entry_category, category)). + reduce(Boolean::logicalOr). + orElseThrow(); } public List getPrizeCategories() { - return prize_category_groups.stream().flatMap(group -> group.categories().stream()).toList(); + return prize_category_groups.stream(). + flatMap(group -> group.categories().stream()). + toList(); } public List getOverallResults() { diff --git a/src/main/java/org/grahamkirby/race_timing/common/RawResult.java b/src/main/java/org/grahamkirby/race_timing/common/RawResult.java index d5e57e3d..194c592a 100644 --- a/src/main/java/org/grahamkirby/race_timing/common/RawResult.java +++ b/src/main/java/org/grahamkirby/race_timing/common/RawResult.java @@ -19,6 +19,7 @@ import java.time.Duration; import static org.grahamkirby.race_timing.common.Normalisation.parseTime; +import static org.grahamkirby.race_timing.common.Race.UNKNOWN_BIB_NUMBER; public class RawResult { @@ -33,8 +34,7 @@ public RawResult(final String file_line) { final String bib_number_as_string = elements[0]; final String time_as_string = elements[1]; - // TODO investigate not using special value. - bib_number = bib_number_as_string.equals("?") ? -1 : Integer.parseInt(bib_number_as_string); + bib_number = bib_number_as_string.equals("?") ? UNKNOWN_BIB_NUMBER : Integer.parseInt(bib_number_as_string); recorded_finish_time = time_as_string.equals("?") ? null : parseTime(time_as_string); } diff --git a/src/main/java/org/grahamkirby/race_timing/individual_race/IndividualRace.java b/src/main/java/org/grahamkirby/race_timing/individual_race/IndividualRace.java index 1911d4a6..43cc79b5 100644 --- a/src/main/java/org/grahamkirby/race_timing/individual_race/IndividualRace.java +++ b/src/main/java/org/grahamkirby/race_timing/individual_race/IndividualRace.java @@ -205,7 +205,6 @@ private int getRecordedPosition(final int bib_number) { filter(result -> result.getBibNumber() == bib_number). map(_ -> position.get()). findFirst(). - // TODO investigate getting rid of special value. - orElse(Integer.MAX_VALUE); + orElse(UNKNOWN_RACE_POSITION); } } diff --git a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRace.java b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRace.java index 3eae374d..b6545f2e 100644 --- a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRace.java +++ b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRace.java @@ -308,7 +308,7 @@ private int getRecordedLegPosition(final int bib_number, final int leg_number) { filter(_ -> legs_completed.incrementAndGet() == leg_number). map(_ -> position.get()). findFirst(). - orElse(Integer.MAX_VALUE); + orElse(UNKNOWN_RACE_POSITION); } private int getRecordedLastLegPosition(final RelayRaceResult result) { @@ -408,7 +408,9 @@ private void configurePairedLegs() { // Example: PAIRED_LEGS = 2,3 - paired_legs = new ArrayList<>(Collections.nCopies(number_of_legs, false)); + paired_legs = Stream.generate(() -> false). + limit(number_of_legs). + collect(Collectors.toList()); for (final String leg_number_as_string : paired_legs_string.split(",")) paired_legs.set(Integer.parseInt(leg_number_as_string) - 1, true); @@ -416,16 +418,15 @@ private void configurePairedLegs() { private void configureIndividualLegStarts() { - final String individual_leg_starts_string = getProperty(KEY_INDIVIDUAL_LEG_STARTS, ""); + final String individual_leg_starts_string = getProperty(KEY_INDIVIDUAL_LEG_STARTS); // bib number / leg number / start time // Example: INDIVIDUAL_LEG_STARTS = 2/1/0:10:00,26/3/2:41:20 - individual_leg_starts = new ArrayList<>(); - - if (!individual_leg_starts_string.isBlank()) - for (final String s : individual_leg_starts_string.split(",")) - individual_leg_starts.add(getIndividualLegStart(s)); + individual_leg_starts = individual_leg_starts_string == null ? new ArrayList<>() : + Arrays.stream(individual_leg_starts_string.split(",")). + map(this::getIndividualLegStart). + toList(); } private IndividualLegStart getIndividualLegStart(final String individual_leg_starts_string) { @@ -448,7 +449,7 @@ private void fillFinishTimes() { private void recordLegResults() { raw_results.stream(). - filter(result -> result.getBibNumber() > -1). + filter(result -> result.getBibNumber() != UNKNOWN_BIB_NUMBER). forEach(result -> recordLegResult((RelayRaceRawResult) result)); } @@ -565,18 +566,25 @@ private boolean isInMassStart(final Duration individual_start_time, final Durati private int findIndexOfNextUnfilledLegResult(final List leg_results) { - for (int i = 0; i < leg_results.size(); i++) - if (leg_results.get(i).finish_time == null) return i; + final int index = (int) leg_results.stream(). + takeWhile(result -> result.finish_time != null). + count(); + + if (index == leg_results.size()) throw new RuntimeException("surplus result recorded for team: " + leg_results.getFirst().entry.bib_number); - throw new RuntimeException("surplus result recorded for team: " + leg_results.getFirst().entry.bib_number); + return index; } private int findIndexOfTeamWithBibNumber(final int bib_number) { - for (int i = 0; i < overall_results.size(); i++) - if (((RelayRaceResult)overall_results.get(i)).entry.bib_number == bib_number) return i; + final int index = (int) overall_results.stream(). + map(result -> (RelayRaceResult) result). + takeWhile(result -> result.entry.bib_number != bib_number). + count(); + + if (index == overall_results.size()) throw new RuntimeException("unregistered team: " + bib_number); - throw new RuntimeException("unregistered team: " + bib_number); + return index; } private void addPaperRecordingComments() { diff --git a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceInput.java b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceInput.java index 497672f5..f2de13fd 100644 --- a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceInput.java +++ b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceInput.java @@ -26,6 +26,7 @@ import java.util.List; import static org.grahamkirby.race_timing.common.Normalisation.parseTime; +import static org.grahamkirby.race_timing.common.Race.UNKNOWN_BIB_NUMBER; import static org.grahamkirby.race_timing.relay_race.RelayRace.KEY_ANNOTATIONS_PATH; import static org.grahamkirby.race_timing.relay_race.RelayRace.KEY_PAPER_RESULTS_PATH; @@ -97,7 +98,7 @@ private static void updateResult(final List raw_results, final String final int position = Integer.parseInt(elements[1]); final RawResult raw_result = raw_results.get(position - 1); - if (elements[2].equals("?")) raw_result.setBibNumber(-1); + if (elements[2].equals("?")) raw_result.setBibNumber(UNKNOWN_BIB_NUMBER); else if (!elements[2].isEmpty()) raw_result.setBibNumber(Integer.parseInt(elements[2])); if (elements[3].equals("?")) raw_result.setRecordedFinishTime(null); diff --git a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceMissingData.java b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceMissingData.java index 3761bc5a..dfb35f64 100644 --- a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceMissingData.java +++ b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceMissingData.java @@ -23,6 +23,8 @@ import java.util.Comparator; import java.util.List; +import static org.grahamkirby.race_timing.common.Race.UNKNOWN_BIB_NUMBER; + public class RelayRaceMissingData { private record TeamSummaryAtPosition(int team_number, int finishes_before, int finishes_after, Duration previous_finish, Duration next_finish) { } @@ -175,7 +177,7 @@ private void setTimesForResultsAfterLastRecordedTime(final int missing_times_sta private void recordCommentsForNonGuessedResults() { for (final RawResult result : race.getRawResults()) - if (result.getBibNumber() == -1) + if (result.getBibNumber() == UNKNOWN_BIB_NUMBER) result.appendComment("Time but not bib number recorded electronically. Bib number not recorded on paper. Too many missing times to guess from DNF teams."); } @@ -197,7 +199,7 @@ private void guessMissingBibNumbersWithAllTimesRecorded() { private int getPositionOfNextMissingBibNumber() { for (int i = 0; i < race.getRawResults().size(); i++) - if (race.getRawResults().get(i).getBibNumber() == -1) return i + 1; + if (race.getRawResults().get(i).getBibNumber() == UNKNOWN_BIB_NUMBER) return i + 1; return 0; } 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 7f44405b..5b806ea9 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 @@ -188,7 +188,7 @@ public void printResultsHeader() throws IOException { Pos - Runner\{((RelayRace)race).paired_legs.get(leg-1) ? "s" : ""} + Runner\{((RelayRace)race).paired_legs.get(leg - 1) ? "s" : ""} Time @@ -205,7 +205,7 @@ public void printResult(final RaceResult r) throws IOException { writer.append(STR.""" \{leg_result.position_string} - \{race.normalisation.htmlEncode(leg_result.entry.team.runner_names().get(leg_result.leg_number-1))} + \{race.normalisation.htmlEncode(leg_result.entry.team.runner_names().get(leg_result.leg_number - 1))} \{format(leg_result.duration())} """); @@ -271,7 +271,7 @@ public void printResultsHeader() throws IOException { for (int leg_number = 1; leg_number <= ((RelayRace)race).number_of_legs; leg_number++) { writer.append(STR.""" - Runner\{((RelayRace)race).paired_legs.get(leg_number-1) ? "s" : ""} \{leg_number} + Runner\{((RelayRace)race).paired_legs.get(leg_number - 1) ? "s" : ""} \{leg_number} Leg \{leg_number} \{leg_number < ((RelayRace)race).number_of_legs ? STR."Split \{leg_number}" : "Total"} """); 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 e9abba8d..8a3c8854 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 @@ -36,6 +36,7 @@ import java.util.stream.Stream; import static org.grahamkirby.race_timing.common.Normalisation.format; +import static org.grahamkirby.race_timing.common.Race.UNKNOWN_BIB_NUMBER; public class RelayRaceOutputText extends RaceOutputText { @@ -147,7 +148,7 @@ private List getTimesWithMissingBibNumbers() { for (final RawResult raw_result : ((RelayRace)race).getRawResults()) { - if (raw_result.getBibNumber() == -1) + if (raw_result.getBibNumber() == UNKNOWN_BIB_NUMBER) times_with_missing_bib_numbers.add(raw_result.getRecordedFinishTime()); } @@ -178,7 +179,7 @@ private void printBibNumberAndTime(final OutputStreamWriter writer, final RawRes final int bib_number = raw_result.getBibNumber(); - writer.append(bib_number != -1 ? String.valueOf(bib_number) : "?"). + writer.append(bib_number != UNKNOWN_BIB_NUMBER ? String.valueOf(bib_number) : "?"). append("\t"). append(raw_result.getRecordedFinishTime() != null ? format(raw_result.getRecordedFinishTime()) : "?"); } diff --git a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceRawResult.java b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceRawResult.java index 94e84a09..f8ab65ac 100644 --- a/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceRawResult.java +++ b/src/main/java/org/grahamkirby/race_timing/relay_race/RelayRaceRawResult.java @@ -18,6 +18,8 @@ import org.grahamkirby.race_timing.common.RawResult; +import static org.grahamkirby.race_timing.common.Race.UNKNOWN_LEG_NUMBER; + public class RelayRaceRawResult extends RawResult { // Leg number is optional, depending on whether it was recorded on paper sheet. @@ -28,7 +30,7 @@ public RelayRaceRawResult(final String file_line) { super(file_line); final String[] elements = file_line.split("\t"); - leg_number = elements.length == 2 ? -1 : Integer.parseInt(elements[2]); + leg_number = elements.length == 2 ? UNKNOWN_LEG_NUMBER : Integer.parseInt(elements[2]); } public int getLegNumber() { diff --git a/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRaceResult.java b/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRaceResult.java index 3b519949..e2a17a09 100644 --- a/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRaceResult.java +++ b/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRaceResult.java @@ -26,9 +26,12 @@ public class MidweekRaceResult extends SeriesRaceResult { + protected final List scores; + public MidweekRaceResult(final Runner runner, final List scores, final Race race) { super(runner, race); + this.scores = scores; } @Override @@ -55,19 +58,15 @@ public boolean shouldDisplayPosition() { protected int totalScore() { - MidweekRace midweek_race = (MidweekRace)race; - - final int minimum_number_of_races = midweek_race.getMinimumNumberOfRaces(); - final int number_of_races_taken_place = midweek_race.getNumberOfRacesTakenPlace(); + final int number_of_races_to_count = Math.min( + ((MidweekRace)race).getNumberOfRacesTakenPlace(), + ((MidweekRace)race).getMinimumNumberOfRaces()); - return midweek_race.getRaces(). - subList(0, number_of_races_taken_place).stream(). - map(race -> midweek_race.calculateRaceScore(race, runner)). - sorted(). - toList(). - reversed(). - subList(0, Math.min(number_of_races_taken_place, minimum_number_of_races)).stream(). - reduce(Integer::sum). - orElse(0); + return scores.stream(). + sorted(). + toList(). + reversed(). + subList(0, number_of_races_to_count).stream(). + reduce(0, Integer::sum); } }