Skip to content

Commit

Permalink
Tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamkirby committed Nov 19, 2024
1 parent d338fc5 commit 293a4ee
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -131,7 +127,6 @@ protected void outputResults() throws IOException {
@Override
public List<Comparator<RaceResult>> getComparators() {

// return List.of(this::compareRecordedPosition, this::comparePerformance, this::compareCompletion);
return List.of(this::compareCompletion, this::comparePerformance, this::compareRecordedPosition);
}

Expand Down Expand Up @@ -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);
}
Expand Down
47 changes: 22 additions & 25 deletions src/main/java/org/grahamkirby/race_timing/relay_race/RelayRace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -194,12 +195,12 @@ protected List<Comparator<RaceResult>> 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<Comparator<RaceResult>> getDNFComparators() {

return List.of(this::compareBibNumber);
}

Expand All @@ -211,8 +212,8 @@ private List<Comparator<RaceResult>> 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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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<LegResult> leg_results, final int leg) {

Duration total = Duration.ZERO;
Expand All @@ -318,7 +301,21 @@ protected Duration sumDurationsUpToLeg(final List<LegResult> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -114,7 +100,6 @@ protected RaceOutputPDF getOutputPDF() {
@Override
protected List<Comparator<RaceResult>> getComparators() {

// return List.of(this::compareRunnerFirstName, this::compareRunnerLastName, this::comparePerformance, this::compareCompletion);
return List.of(this::compareCompletion, this::comparePerformance, this::compareRunnerLastName, this::compareRunnerFirstName);
}

Expand All @@ -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<Integer> scores = races.stream().
map(race -> calculateRaceScore(race, runner)).
toList();

return result;
return new MidweekRaceResult(runner, scores, this);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class MidweekRaceResult extends SeriesRaceResult {

protected final List<Integer> scores;

public MidweekRaceResult(final Runner runner, final Race race) {
public MidweekRaceResult(final Runner runner, final List<Integer> scores, final Race race) {

super(runner, race);
scores = new ArrayList<>();
this.scores = scores;
}

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

//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -105,7 +105,6 @@ protected void printCombined() throws IOException {
@Override
protected List<Comparator<RaceResult>> 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);
}

Expand Down

0 comments on commit 293a4ee

Please sign in to comment.