Skip to content

Commit eb9cc0e

Browse files
committed
More refactoring.
1 parent 9000284 commit eb9cc0e

16 files changed

+309
-119
lines changed

.gitignore

+2-11
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@ target/
44
!**/src/test/**/target/
55

66
### IntelliJ IDEA ###
7-
.idea/modules.xml
8-
.idea/jarRepositories.xml
9-
.idea/compiler.xml
10-
.idea/libraries/
11-
*.iws
12-
*.iml
13-
*.ipr
14-
15-
### VS Code ###
16-
.vscode/
7+
.idea/
178

189
### Mac OS ###
19-
.DS_Store
10+
.DS_Store

src/main/java/common/Race.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package common;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.util.Properties;
6+
7+
public abstract class Race {
8+
9+
protected Properties properties;
10+
11+
public Race(final String config_file_path) throws IOException {
12+
13+
this(readProperties(config_file_path));
14+
}
15+
16+
public Race(final Properties properties) throws IOException {
17+
18+
this.properties = properties;
19+
configure();
20+
}
21+
22+
protected abstract void configure() throws IOException;
23+
24+
public abstract void processResults() throws IOException;
25+
26+
public Properties getProperties() {
27+
return properties;
28+
}
29+
30+
protected static Properties readProperties(final String config_file_path) throws IOException {
31+
32+
try (final FileInputStream in = new FileInputStream(config_file_path)) {
33+
34+
final Properties properties = new Properties();
35+
properties.load(in);
36+
return properties;
37+
}
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package individual_race;
2+
3+
import common.Race;
4+
5+
import java.io.IOException;
6+
import java.util.Properties;
7+
8+
public class IndividualRace extends Race {
9+
10+
// TODO variable number of prizes per category; open prizes in addition to gender categories; non-binary category; optional team prizes
11+
12+
public IndividualRace(final String config_file_path) throws IOException {
13+
14+
super(config_file_path);
15+
}
16+
17+
public IndividualRace(final Properties properties) throws IOException {
18+
super(properties);
19+
}
20+
21+
@Override
22+
protected void configure() throws IOException {
23+
}
24+
25+
@Override
26+
public void processResults() throws IOException {
27+
}
28+
}

src/main/java/lap_race/Input.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
public class Input {
1010

11-
final Results results;
11+
final LapRace race;
1212

1313
Path input_directory_path;
1414
Path entries_path;
1515
Path raw_results_path;
1616
String entries_filename;
1717
String raw_results_filename;
1818

19-
public Input(final Results results) {
19+
public Input(final LapRace race) {
2020

21-
this.results = results;
21+
this.race = race;
2222
configure();
2323
}
2424

@@ -30,13 +30,13 @@ private void configure() {
3030

3131
private void readProperties() {
3232

33-
entries_filename = results.properties.getProperty("ENTRIES_FILENAME");
34-
raw_results_filename = results.properties.getProperty("RAW_RESULTS_FILENAME");
33+
entries_filename = race.getProperties().getProperty("ENTRIES_FILENAME");
34+
raw_results_filename = race.getProperties().getProperty("RAW_RESULTS_FILENAME");
3535
}
3636

3737
private void constructFilePaths() {
3838

39-
input_directory_path = results.working_directory_path.resolve("input");
39+
input_directory_path = race.working_directory_path.resolve("input");
4040
entries_path = input_directory_path.resolve(entries_filename);
4141
raw_results_path = input_directory_path.resolve(raw_results_filename);
4242
}
@@ -56,7 +56,7 @@ private void loadEntry(final Team[] entries, final List<String> lines, final int
5656

5757
final String[] team_elements = lines.get(entry_index).split("\t");
5858

59-
if (team_elements.length != results.number_of_legs + 3)
59+
if (team_elements.length != race.number_of_legs + 3)
6060
throw new RuntimeException("illegal composition for team: " + team_elements[0]);
6161

6262
final int bib_number = Integer.parseInt(team_elements[0]);

src/main/java/lap_race/Results.java src/main/java/lap_race/LapRace.java

+17-28
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package lap_race;
22

3-
import java.io.FileInputStream;
3+
import common.Race;
4+
45
import java.io.IOException;
56
import java.nio.file.Path;
67
import java.nio.file.Paths;
78
import java.time.Duration;
89
import java.util.*;
910

10-
public class Results {
11+
public class LapRace extends Race {
1112

1213
//////////////////////////////////////////// SET UP ////////////////////////////////////////////
1314
// //
@@ -24,8 +25,6 @@ private record IndividualLegStart(int bib_number, int leg_number, Duration start
2425

2526
//////////////////////////////////////////////////////////////////////////////////////////////////
2627

27-
Properties properties;
28-
2928
Input input;
3029
Output output_CSV, output_HTML, output_text, output_PDF;
3130
Prizes prizes;
@@ -42,7 +41,7 @@ private record IndividualLegStart(int bib_number, int leg_number, Duration start
4241

4342
Team[] entries;
4443
RawResult[] raw_results;
45-
OverallResult[] overall_results;
44+
TeamResult[] overall_results;
4645
Map<Category, List<Team>> prize_winners = new HashMap<>();
4746

4847
// Records for each leg whether there was a mass start.
@@ -59,15 +58,14 @@ private record IndividualLegStart(int bib_number, int leg_number, Duration start
5958

6059
//////////////////////////////////////////////////////////////////////////////////////////////////
6160

62-
public Results(final String config_file_path) throws IOException {
61+
public LapRace(final String config_file_path) throws IOException {
6362

64-
this(readProperties(config_file_path));
63+
super(config_file_path);
6564
}
6665

67-
public Results(final Properties properties) throws IOException {
66+
public LapRace(final Properties properties) throws IOException {
6867

69-
this.properties = properties;
70-
configure();
68+
super(properties);
7169
}
7270

7371
public static void main(String[] args) throws IOException {
@@ -77,22 +75,12 @@ public static void main(String[] args) throws IOException {
7775
if (args.length < 1)
7876
System.out.println("usage: java Results <config file path>");
7977
else {
80-
new Results(args[0]).processResults();
78+
new LapRace(args[0]).processResults();
8179
}
8280
}
8381

8482
//////////////////////////////////////////////////////////////////////////////////////////////////
8583

86-
private static Properties readProperties(final String config_file_path) throws IOException {
87-
88-
try (final FileInputStream in = new FileInputStream(config_file_path)) {
89-
90-
final Properties properties = new Properties();
91-
properties.load(in);
92-
return properties;
93-
}
94-
}
95-
9684
public void processResults() throws IOException {
9785

9886
initialiseResults();
@@ -110,7 +98,8 @@ public void processResults() throws IOException {
11098
printCombined();
11199
}
112100

113-
private void configure() throws IOException {
101+
@Override
102+
protected void configure() throws IOException {
114103

115104
readProperties();
116105

@@ -262,18 +251,18 @@ private String getPropertyWithDefault(final String property_key, final String de
262251

263252
private void initialiseResults() {
264253

265-
overall_results = new OverallResult[entries.length];
254+
overall_results = new TeamResult[entries.length];
266255

267256
for (int i = 0; i < overall_results.length; i++)
268-
overall_results[i] = new OverallResult(entries[i], number_of_legs, this);
257+
overall_results[i] = new TeamResult(entries[i], number_of_legs, this);
269258
}
270259

271260
private void fillLegFinishTimes() {
272261

273262
for (final RawResult raw_result : raw_results) {
274263

275264
final int team_index = findIndexOfTeamWithBibNumber(raw_result.bib_number);
276-
final OverallResult result = overall_results[team_index];
265+
final TeamResult result = overall_results[team_index];
277266
final LegResult[] leg_results = result.leg_results;
278267

279268
final int leg_index = findIndexOfNextUnfilledLegResult(leg_results);
@@ -293,7 +282,7 @@ private void fillLegFinishTimes() {
293282
final int leg_number = Integer.parseInt(swap[1]);
294283
final int leg_index = leg_number - 1;
295284

296-
final OverallResult result = overall_results[findIndexOfTeamWithBibNumber(bib_number)];
285+
final TeamResult result = overall_results[findIndexOfTeamWithBibNumber(bib_number)];
297286

298287
final Duration temp = result.leg_results[leg_index - 1].finish_time;
299288
result.leg_results[leg_index - 1].finish_time = result.leg_results[leg_index].finish_time;
@@ -321,7 +310,7 @@ private void fillDNFs() {
321310
final int leg_number = Integer.parseInt(dnf[1]);
322311
final int leg_index = leg_number - 1;
323312

324-
final OverallResult result = overall_results[findIndexOfTeamWithBibNumber(bib_number)];
313+
final TeamResult result = overall_results[findIndexOfTeamWithBibNumber(bib_number)];
325314
result.leg_results[leg_index].DNF = true;
326315
}
327316
catch (Exception e) {
@@ -333,7 +322,7 @@ private void fillDNFs() {
333322

334323
private void fillLegStartTimes() {
335324

336-
for (final OverallResult overall_result : overall_results)
325+
for (final TeamResult overall_result : overall_results)
337326
for (int leg_index = 0; leg_index < number_of_legs; leg_index++)
338327
fillLegStartTime(overall_result.leg_results, leg_index);
339328
}

src/main/java/lap_race/LegResult.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ public class LegResult implements Comparable<LegResult> {
66

77
final Team team;
88
final int leg_number;
9-
final Results results;
9+
final LapRace results;
1010
boolean DNF;
1111
boolean in_mass_start;
1212
String position_string;
1313

1414
Duration start_time; // Relative to start of leg 1.
1515
Duration finish_time; // Relative to start of leg 1.
1616

17-
public LegResult(final Team team, final int leg_number, final Results results) {
17+
public LegResult(final Team team, final int leg_number, final LapRace results) {
1818

1919
this.team = team;
2020
this.leg_number = leg_number;
@@ -24,7 +24,7 @@ public LegResult(final Team team, final int leg_number, final Results results) {
2424
}
2525

2626
public Duration duration() {
27-
return DNF ? Results.DUMMY_DURATION : finish_time.minus(start_time);
27+
return DNF ? LapRace.DUMMY_DURATION : finish_time.minus(start_time);
2828
}
2929

3030
@Override

src/main/java/lap_race/Output.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public abstract class Output {
2323
LapRaceCategory.MIXED_SENIOR,
2424
LapRaceCategory.MIXED_40);
2525

26-
final Results results;
26+
final LapRace race;
2727

2828
String year;
2929
String race_name_for_results;
@@ -33,9 +33,9 @@ public abstract class Output {
3333
String prizes_filename;
3434
Path output_directory_path;
3535

36-
public Output(final Results results) {
36+
public Output(final LapRace race) {
3737

38-
this.results = results;
38+
this.race = race;
3939
configure();
4040
}
4141

@@ -47,9 +47,9 @@ private void configure() {
4747

4848
private void readProperties() {
4949

50-
race_name_for_results = results.properties.getProperty("RACE_NAME_FOR_RESULTS");
51-
race_name_for_filenames = results.properties.getProperty("RACE_NAME_FOR_FILENAMES");
52-
year = results.properties.getProperty("YEAR");
50+
race_name_for_results = race.getProperties().getProperty("RACE_NAME_FOR_RESULTS");
51+
race_name_for_filenames = race.getProperties().getProperty("RACE_NAME_FOR_FILENAMES");
52+
year = race.getProperties().getProperty("YEAR");
5353
}
5454

5555
private void constructFilePaths() {
@@ -58,12 +58,12 @@ private void constructFilePaths() {
5858
detailed_results_filename = race_name_for_filenames + "_detailed_" + year;
5959
prizes_filename = race_name_for_filenames + "_prizes_" + year;
6060

61-
output_directory_path = results.working_directory_path.resolve("output");
61+
output_directory_path = race.working_directory_path.resolve("output");
6262
}
6363

6464
public void printLegResults() throws IOException {
6565

66-
for (int leg = 1; leg <= results.number_of_legs; leg++)
66+
for (int leg = 1; leg <= race.number_of_legs; leg++)
6767
printLegResults(leg);
6868
}
6969

@@ -77,10 +77,10 @@ Duration sumDurationsUpToLeg(final LegResult[] leg_results, final int leg) {
7777

7878
LegResult[] getLegResults(final int leg) {
7979

80-
final LegResult[] leg_results = new LegResult[results.overall_results.length];
80+
final LegResult[] leg_results = new LegResult[race.overall_results.length];
8181

8282
for (int i = 0; i < leg_results.length; i++)
83-
leg_results[i] = results.overall_results[i].leg_results[leg-1];
83+
leg_results[i] = race.overall_results[i].leg_results[leg-1];
8484

8585
// Sort in order of increasing overall leg time, as defined in LegResult.compareTo().
8686
// Ordering for DNF results doesn't matter since they're omitted in output.
@@ -98,7 +98,7 @@ void addMassStartAnnotation(final OutputStreamWriter writer, final LegResult leg
9898

9999
// Find the next mass start.
100100
int mass_start_leg = leg;
101-
while (!results.mass_start_legs[mass_start_leg-1])
101+
while (!race.mass_start_legs[mass_start_leg-1])
102102
mass_start_leg++;
103103

104104
writer.append(" (M").append(String.valueOf(mass_start_leg)).append(")");

0 commit comments

Comments
 (0)