From 293a4ee6372446fd3b61673e95258876eae63c95 Mon Sep 17 00:00:00 2001 From: Graham Kirby Date: Tue, 19 Nov 2024 22:28:44 +0000 Subject: [PATCH] Tidying. --- .../individual_race/IndividualRace.java | 45 ++++++++---------- .../race_timing/relay_race/RelayRace.java | 47 +++++++++---------- .../fife_ac_midweek/MidweekRace.java | 28 +++-------- .../fife_ac_midweek/MidweekRaceResult.java | 4 +- .../fife_ac_minitour/MinitourRace.java | 7 ++- 5 files changed, 53 insertions(+), 78 deletions(-) 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 7df8d7c9..dd32df81 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 @@ -16,7 +16,8 @@ */ package org.grahamkirby.race_timing.individual_race; -import org.grahamkirby.race_timing.common.*; +import org.grahamkirby.race_timing.common.RaceInput; +import org.grahamkirby.race_timing.common.RaceResult; import org.grahamkirby.race_timing.common.categories.EntryCategory; import org.grahamkirby.race_timing.common.categories.PrizeCategory; import org.grahamkirby.race_timing.common.output.RaceOutputCSV; @@ -71,15 +72,10 @@ public EntryCategory findCategory(final int bib_number) { private int compareRecordedPosition(final RaceResult r1, final RaceResult r2) { - final IndividualRace individual_race = (IndividualRace) r1.race; + final int recorded_position1 = getRecordedPosition(((IndividualRaceResult) r1).entry.bib_number); + final int recorded_position2 = getRecordedPosition(((IndividualRaceResult) r2).entry.bib_number); - IndividualRaceEntry entry1 = ((IndividualRaceResult) r1).entry; - IndividualRaceEntry entry2 = ((IndividualRaceResult) r2).entry; - - final int this_recorded_position = individual_race.getRecordedPosition(entry1.bib_number); - final int other_recorded_position = individual_race.getRecordedPosition(entry2.bib_number); - - return Integer.compare(this_recorded_position, other_recorded_position); + return Integer.compare(recorded_position1, recorded_position2); } ////////////////////////////////////////////////////////////////////////////////////////////////// @@ -131,7 +127,6 @@ protected void outputResults() throws IOException { @Override public List> getComparators() { -// return List.of(this::compareRecordedPosition, this::comparePerformance, this::compareCompletion); return List.of(this::compareCompletion, this::comparePerformance, this::compareRecordedPosition); } @@ -170,43 +165,43 @@ protected void fillDNF(final String dnf_string) { private void fillFinishTimes() { - for (final RawResult raw_result : raw_results) { + raw_results.forEach(raw_result -> { final IndividualRaceResult result = getResultWithBibNumber(raw_result.getBibNumber()); - result.finish_time = raw_result.getRecordedFinishTime(); // Provisionally this result is not DNF since a finish time was recorded. // However, it might still be set to DNF in fillDNF() if the runner didn't complete the course. result.DNF = false; - } + }); } private IndividualRaceResult getResultWithBibNumber(final int bib_number) { return overall_results.stream(). - map(result -> ((IndividualRaceResult) result)). - filter(result -> result.entry.bib_number == bib_number). - findFirst(). - orElseThrow(() -> new RuntimeException("unregistered bib number: " + bib_number)); + map(result -> ((IndividualRaceResult) result)). + filter(result -> result.entry.bib_number == bib_number). + findFirst(). + orElseThrow(() -> new RuntimeException("unregistered bib number: " + bib_number)); } private IndividualRaceEntry getEntryWithBibNumber(final int bib_number) { return entries.stream(). - map(entry -> ((IndividualRaceEntry) entry)). - filter(entry -> entry.bib_number == bib_number). - findFirst(). - orElseThrow(() -> new RuntimeException("unregistered bib number: " + bib_number)); + map(entry -> ((IndividualRaceEntry) entry)). + filter(entry -> entry.bib_number == bib_number). + findFirst(). + orElseThrow(() -> new RuntimeException("unregistered bib number: " + bib_number)); } - public int getRecordedPosition(final int bib_number) { + private int getRecordedPosition(final int bib_number) { - final AtomicInteger i = new AtomicInteger(); + final AtomicInteger position = new AtomicInteger(0); return raw_results.stream(). - filter(r -> { i.getAndIncrement(); return r.getBibNumber() == bib_number;}). - map(_ -> i.get()). + peek(_ -> position.incrementAndGet()). + filter(result -> result.getBibNumber() == bib_number). + map(_ -> position.get()). findFirst(). orElse(Integer.MAX_VALUE); } 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 9fcebb0c..52a362a1 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 @@ -35,6 +35,7 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import static org.grahamkirby.race_timing.common.Normalisation.parseTime; @@ -194,12 +195,12 @@ protected List> getComparators() { // DNF results are sorted in increasing order of bib number. // Where two teams have the same overall time, the order in which their last leg runner_names were recorded is preserved. -// return List.of(this::compareLastLegPosition, this::comparePerformance, this::compareCompletion); return List.of(this::compareCompletion, this::comparePerformance, this::compareLastLegPosition); } @Override protected List> getDNFComparators() { + return List.of(this::compareBibNumber); } @@ -211,8 +212,8 @@ private List> getLegResultComparators(final int leg_numbe // OutputCSV.printLegResults deals with dead heats. return leg_number == 1 ? - List.of(this::comparePerformance, this::compareRecordedLegPosition): - List.of(this::comparePerformance, this::compareRunnerLastName, this::compareRunnerFirstName); + List.of(this::comparePerformance, this::compareRecordedLegPosition): + List.of(this::comparePerformance, this::compareRunnerLastName, this::compareRunnerFirstName); } @Override @@ -244,17 +245,17 @@ protected void fillDNF(final String dnf_string) { ////////////////////////////////////////////////////////////////////////////////////////////////// - public int compareBibNumber(final RaceResult r1, final RaceResult r2) { + private int compareBibNumber(final RaceResult r1, final RaceResult r2) { return Integer.compare(((RelayRaceResult) r1).entry.bib_number, ((RelayRaceResult) r2).entry.bib_number); } - public int compareLastLegPosition(final RaceResult r1, final RaceResult r2) { + private int compareLastLegPosition(final RaceResult r1, final RaceResult r2) { return Integer.compare(getRecordedLastLegPosition(((RelayRaceResult) r1)), getRecordedLastLegPosition(((RelayRaceResult) r2))); } - public int compareRecordedLegPosition(final RaceResult r1, final RaceResult r2) { + private int compareRecordedLegPosition(final RaceResult r1, final RaceResult r2) { final int leg_number = ((LegResult) r1).leg_number; @@ -290,24 +291,6 @@ protected String getMassStartAnnotation(final LegResult leg_result, final int le else return ""; } - protected int getRecordedLegPosition(final int bib_number, final int leg_number) { - - // TODO check whether can be rationalised with overall getRecordedPosition. - int legs_completed = 0; - - for (int i = 0; i < raw_results.size(); i++) { - - final int result_bib_number = raw_results.get(i).getBibNumber(); - - if (result_bib_number == bib_number) { - legs_completed++; - if (legs_completed == leg_number) return i + 1; - } - } - - return Integer.MAX_VALUE; - } - protected Duration sumDurationsUpToLeg(final List leg_results, final int leg) { Duration total = Duration.ZERO; @@ -318,7 +301,21 @@ protected Duration sumDurationsUpToLeg(final List leg_results, final ////////////////////////////////////////////////////////////////////////////////////////////////// - public int getRecordedLastLegPosition(final RelayRaceResult result) { + private int getRecordedLegPosition(final int bib_number, final int leg_number) { + + final AtomicInteger position = new AtomicInteger(0); + final AtomicInteger legs_completed = new AtomicInteger(0); + + return raw_results.stream(). + peek(_ -> position.incrementAndGet()). + filter(result -> result.getBibNumber() == bib_number). + filter(_ -> legs_completed.incrementAndGet() == leg_number). + map(_ -> position.get()). + findFirst(). + orElse(Integer.MAX_VALUE); + } + + private int getRecordedLastLegPosition(final RelayRaceResult result) { return getRecordedLegPosition(result.entry.bib_number, number_of_legs); } diff --git a/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRace.java b/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRace.java index a951da42..b4749038 100644 --- a/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRace.java +++ b/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_midweek/MidweekRace.java @@ -69,22 +69,8 @@ protected void configureInputData() throws IOException { super.configureInputData(); - for (final String runner_name : getRunnerNames()) - checkClubsForRunner(runner_name); - } - -// @Override -// protected void configureHelpers() { -// -// input = new SeriesRaceInput(this); -// -// output_CSV = new MidweekRaceOutputCSV(this); -// output_HTML = new MidweekRaceOutputHTML(this); -// output_text = new MidweekRaceOutputText(this); -// output_PDF = new MidweekRaceOutputPDF(this); -// -// prizes = new RacePrizes(this); -// } + getRunnerNames().forEach(this::checkClubsForRunner); + } @Override protected RaceInput getInput() { @@ -114,7 +100,6 @@ protected RaceOutputPDF getOutputPDF() { @Override protected List> getComparators() { -// return List.of(this::compareRunnerFirstName, this::compareRunnerLastName, this::comparePerformance, this::compareCompletion); return List.of(this::compareCompletion, this::comparePerformance, this::compareRunnerLastName, this::compareRunnerFirstName); } @@ -136,12 +121,11 @@ protected EntryCategory getEntryCategory(final RaceResult result) { @Override protected RaceResult getOverallResult(final Runner runner) { - final MidweekRaceResult result = new MidweekRaceResult(runner, this); - - for (final IndividualRace individual_race : races) - result.scores.add(calculateRaceScore(individual_race, runner)); + final List scores = races.stream(). + map(race -> calculateRaceScore(race, runner)). + toList(); - return result; + return new MidweekRaceResult(runner, scores, this); } ////////////////////////////////////////////////////////////////////////////////////////////////// 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 68bf1357..ff5c484e 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 @@ -29,10 +29,10 @@ public class MidweekRaceResult extends SeriesRaceResult { protected final List scores; - public MidweekRaceResult(final Runner runner, final Race race) { + public MidweekRaceResult(final Runner runner, final List scores, final Race race) { super(runner, race); - scores = new ArrayList<>(); + this.scores = scores; } @Override diff --git a/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_minitour/MinitourRace.java b/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_minitour/MinitourRace.java index 39df0ac3..7ef58645 100644 --- a/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_minitour/MinitourRace.java +++ b/src/main/java/org/grahamkirby/race_timing/series_race/fife_ac_minitour/MinitourRace.java @@ -58,10 +58,10 @@ public static void main(final String[] args) throws IOException { public int compareCompletionSoFar(final RaceResult r1, final RaceResult r2) { - if (((MinitourRaceResult) r1).completedAllRacesSoFar() && !((MinitourRaceResult) r2).completedAllRacesSoFar()) return -1; - if (!((MinitourRaceResult) r1).completedAllRacesSoFar() && ((MinitourRaceResult) r2).completedAllRacesSoFar()) return 1; + final boolean r1_completed_all_races_so_far = ((MinitourRaceResult) r1).completedAllRacesSoFar(); + final boolean r2_completed_all_races_so_far = ((MinitourRaceResult) r2).completedAllRacesSoFar(); - return 0; + return Boolean.compare(r2_completed_all_races_so_far, r1_completed_all_races_so_far); } ////////////////////////////////////////////////////////////////////////////////////////////////// @@ -105,7 +105,6 @@ protected void printCombined() throws IOException { @Override protected List> getComparators() { // TODO unify compareCompletionSoFar and compareCompletion into compareCanComplete -// return List.of(this::compareRunnerFirstName, this::compareRunnerLastName, this::comparePerformance, this::compareCompletionSoFar, this::compareCompletion); return List.of(this::compareCompletion, this::compareCompletionSoFar, this::comparePerformance, this::compareRunnerLastName, this::compareRunnerFirstName); }