Skip to content

Commit

Permalink
Tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamkirby committed Nov 27, 2024
1 parent 43b5b7f commit e05a300
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 45 deletions.
17 changes: 13 additions & 4 deletions src/main/java/org/grahamkirby/race_timing/common/Race.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -205,12 +209,17 @@ public final boolean entryCategoryIsEligibleForPrizeCategory(final EntryCategory

public boolean entryCategoryIsEligibleInSomePrizeCategory(final EntryCategory entry_category, final List<PrizeCategory> 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<PrizeCategory> 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<RaceResult> getOverallResults() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
38 changes: 23 additions & 15 deletions src/main/java/org/grahamkirby/race_timing/relay_race/RelayRace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -408,24 +408,25 @@ 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);
}

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

Expand Down Expand Up @@ -565,18 +566,25 @@ private boolean isInMassStart(final Duration individual_start_time, final Durati

private int findIndexOfNextUnfilledLegResult(final List<LegResult> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -97,7 +98,7 @@ private static void updateResult(final List<RawResult> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand Down Expand Up @@ -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.");
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void printResultsHeader() throws IOException {
<thead>
<tr>
<th>Pos</th>
<th>Runner\{((RelayRace)race).paired_legs.get(leg-1) ? "s" : ""}</th>
<th>Runner\{((RelayRace)race).paired_legs.get(leg - 1) ? "s" : ""}</th>
<th>Time</th>
</tr>
</thead>
Expand All @@ -205,7 +205,7 @@ public void printResult(final RaceResult r) throws IOException {
writer.append(STR."""
<tr>
<td>\{leg_result.position_string}</td>
<td>\{race.normalisation.htmlEncode(leg_result.entry.team.runner_names().get(leg_result.leg_number-1))}</td>
<td>\{race.normalisation.htmlEncode(leg_result.entry.team.runner_names().get(leg_result.leg_number - 1))}</td>
<td>\{format(leg_result.duration())}</td>
</tr>
""");
Expand Down Expand Up @@ -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."""
<th>Runner\{((RelayRace)race).paired_legs.get(leg_number-1) ? "s" : ""} \{leg_number}</th>
<th>Runner\{((RelayRace)race).paired_legs.get(leg_number - 1) ? "s" : ""} \{leg_number}</th>
<th>Leg \{leg_number}</th>
<th>\{leg_number < ((RelayRace)race).number_of_legs ? STR."Split \{leg_number}" : "Total"}</th>
""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -147,7 +148,7 @@ private List<Duration> 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());
}

Expand Down Expand Up @@ -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()) : "?");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

public class MidweekRaceResult extends SeriesRaceResult {

protected final List<Integer> scores;

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

super(runner, race);
this.scores = scores;
}

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

0 comments on commit e05a300

Please sign in to comment.