1
1
package lap_race ;
2
2
3
+ import common .Category ;
3
4
import common .Race ;
5
+ import common .RawResult ;
4
6
5
7
import java .io .IOException ;
6
- import java .nio .file .Path ;
7
8
import java .nio .file .Paths ;
8
9
import java .time .Duration ;
9
10
import java .util .*;
@@ -17,6 +18,7 @@ public class LapRace extends Race {
17
18
//////////////////////////////////////////////////////////////////////////////////////////////////
18
19
19
20
private record IndividualLegStart (int bib_number , int leg_number , Duration start_time ) {}
21
+ private record ResultWithLegIndex (TeamResult result , int leg_index ) {}
20
22
21
23
static final String DUMMY_DURATION_STRING = "23:59:59" ;
22
24
static final Duration DUMMY_DURATION = parseTime (DUMMY_DURATION_STRING );
@@ -25,11 +27,9 @@ private record IndividualLegStart(int bib_number, int leg_number, Duration start
25
27
26
28
//////////////////////////////////////////////////////////////////////////////////////////////////
27
29
28
- Input input ;
29
- Output output_CSV , output_HTML , output_text , output_PDF ;
30
- Prizes prizes ;
31
-
32
- Path working_directory_path ;
30
+ LapRaceInput input ;
31
+ LapRaceOutput output_CSV , output_HTML , output_text , output_PDF ;
32
+ LapRacePrizes prizes ;
33
33
34
34
int number_of_legs ;
35
35
@@ -121,14 +121,14 @@ private void readProperties() {
121
121
122
122
private void configureHelpers () {
123
123
124
- input = new Input (this );
124
+ input = new LapRaceInput (this );
125
125
126
- output_CSV = new OutputCSV (this );
127
- output_HTML = new OutputHTML (this );
128
- output_text = new OutputText (this );
129
- output_PDF = new OutputPDF (this );
126
+ output_CSV = new LapRaceOutputCSV (this );
127
+ output_HTML = new LapRaceOutputHTML (this );
128
+ output_text = new LapRaceOutputText (this );
129
+ output_PDF = new LapRaceOutputPDF (this );
130
130
131
- prizes = new Prizes (this );
131
+ prizes = new LapRacePrizes (this );
132
132
}
133
133
134
134
private void configureInputData () throws IOException {
@@ -243,12 +243,6 @@ private static IndividualLegStart getIndividualLegStart(String individual_leg_st
243
243
return new IndividualLegStart (bib_number , leg_number , start_time );
244
244
}
245
245
246
- private String getPropertyWithDefault (final String property_key , final String default_value ) {
247
-
248
- final String value = properties .getProperty (property_key );
249
- return value == null || value .isBlank () ? default_value : value ;
250
- }
251
-
252
246
private void initialiseResults () {
253
247
254
248
overall_results = new TeamResult [entries .length ];
@@ -261,34 +255,52 @@ private void fillLegFinishTimes() {
261
255
262
256
for (final RawResult raw_result : raw_results ) {
263
257
264
- final int team_index = findIndexOfTeamWithBibNumber (raw_result .bib_number );
258
+ final int team_index = findIndexOfTeamWithBibNumber (raw_result .getBibNumber () );
265
259
final TeamResult result = overall_results [team_index ];
266
260
final LegResult [] leg_results = result .leg_results ;
267
261
268
262
final int leg_index = findIndexOfNextUnfilledLegResult (leg_results );
269
263
270
- leg_results [leg_index ].finish_time = raw_result .recorded_finish_time .plus (start_offset );
264
+ leg_results [leg_index ].finish_time = raw_result .getRecordedFinishTime () .plus (start_offset );
271
265
272
266
// Provisionally this leg is not DNF since a finish time was recorded.
273
267
// However, it might still be set to DNF in fillDNFs() if the runner missed a checkpoint.
274
268
leg_results [leg_index ].DNF = false ;
275
269
}
276
270
277
- if (leg_times_swap_string != null ) {
278
- for ( final String leg_time_swap_string : leg_times_swap_string . split ( "," )) {
271
+ if (leg_times_swap_string != null ) swapLegTimes ();
272
+ }
279
273
280
- final String [] swap = leg_time_swap_string . split ( "/" );
281
- final int bib_number = Integer . parseInt ( swap [ 0 ]);
282
- final int leg_number = Integer . parseInt ( swap [ 1 ] );
283
- final int leg_index = leg_number - 1 ;
274
+ private void swapLegTimes () {
275
+ for ( final String leg_time_swap : leg_times_swap_string . split ( "," ))
276
+ swapLegTimes ( leg_time_swap );
277
+ }
284
278
285
- final TeamResult result = overall_results [ findIndexOfTeamWithBibNumber ( bib_number )];
279
+ private void swapLegTimes ( final String leg_time_swap ) {
286
280
287
- final Duration temp = result .leg_results [leg_index - 1 ].finish_time ;
288
- result .leg_results [leg_index - 1 ].finish_time = result .leg_results [leg_index ].finish_time ;
289
- result .leg_results [leg_index ].finish_time = temp ;
290
- }
291
- }
281
+ final ResultWithLegIndex result_with_leg = getResultWithLegIndex (leg_time_swap );
282
+
283
+ final LegResult [] leg_results = result_with_leg .result ().leg_results ;
284
+ final int leg_index = result_with_leg .leg_index ();
285
+
286
+ final Duration temp = leg_results [leg_index - 1 ].finish_time ;
287
+
288
+ leg_results [leg_index - 1 ].finish_time = leg_results [leg_index ].finish_time ;
289
+ leg_results [leg_index ].finish_time = temp ;
290
+ }
291
+
292
+ private ResultWithLegIndex getResultWithLegIndex (final String bib_and_leg ) {
293
+
294
+ // String of form "bib-number/leg-number"
295
+
296
+ final String [] elements = bib_and_leg .split ("/" );
297
+ final int bib_number = Integer .parseInt (elements [0 ]);
298
+ final int leg_number = Integer .parseInt (elements [1 ]);
299
+ final int leg_index = leg_number - 1 ;
300
+
301
+ final TeamResult result = overall_results [findIndexOfTeamWithBibNumber (bib_number )];
302
+
303
+ return new ResultWithLegIndex (result , leg_index );
292
304
}
293
305
294
306
private void fillDNFs () {
@@ -305,13 +317,9 @@ private void fillDNFs() {
305
317
for (final String dnf_string : dnf_legs_string .split ("," )) {
306
318
307
319
try {
308
- final String [] dnf = dnf_string .split ("/" );
309
- final int bib_number = Integer .parseInt (dnf [0 ]);
310
- final int leg_number = Integer .parseInt (dnf [1 ]);
311
- final int leg_index = leg_number - 1 ;
320
+ final ResultWithLegIndex result_with_leg = getResultWithLegIndex (dnf_string );
312
321
313
- final TeamResult result = overall_results [findIndexOfTeamWithBibNumber (bib_number )];
314
- result .leg_results [leg_index ].DNF = true ;
322
+ result_with_leg .result .leg_results [result_with_leg .leg_index ].DNF = true ;
315
323
}
316
324
catch (Exception e ) {
317
325
throw new RuntimeException ("illegal DNF time" );
@@ -392,12 +400,12 @@ private void calculateResults() {
392
400
Arrays .sort (overall_results );
393
401
}
394
402
395
- Integer getRecordedLegPosition (final int bib_number , final int leg_number ) {
403
+ int getRecordedLegPosition (final int bib_number , final int leg_number ) {
396
404
397
405
int legs_completed = 0 ;
398
406
399
407
for (int i = 0 ; i < raw_results .length ; i ++) {
400
- if (raw_results [i ].bib_number == bib_number ) {
408
+ if (raw_results [i ].getBibNumber () == bib_number ) {
401
409
legs_completed ++;
402
410
if (legs_completed == leg_number ) return i + 1 ;
403
411
}
@@ -421,6 +429,7 @@ int findIndexOfTeamWithBibNumber(final int bib_number) {
421
429
422
430
throw new RuntimeException ("unregistered team: " + bib_number );
423
431
}
432
+
424
433
private void allocatePrizes () {
425
434
426
435
prizes .allocatePrizes ();
@@ -453,33 +462,6 @@ private void printPrizes() throws IOException {
453
462
454
463
private void printCombined () throws IOException {
455
464
456
- ((OutputHTML )output_HTML ).printCombined ();
457
- }
458
-
459
- static Duration parseTime (String element ) {
460
-
461
- element = element .strip ();
462
-
463
- try {
464
- final String [] parts = element .split (":" );
465
- final String time_as_ISO = "PT" + hours (parts ) + minutes (parts ) + seconds (parts );
466
-
467
- return Duration .parse (time_as_ISO );
468
- }
469
- catch (Exception e ) {
470
- throw new RuntimeException ("illegal time: " + element );
471
- }
472
- }
473
-
474
- static String hours (final String [] parts ) {
475
- return parts .length > 2 ? parts [0 ] + "H" : "" ;
476
- }
477
-
478
- static String minutes (final String [] parts ) {
479
- return (parts .length > 2 ? parts [1 ] : parts [0 ]) + "M" ;
480
- }
481
-
482
- static String seconds (final String [] parts ) {
483
- return (parts .length > 2 ? parts [2 ] : parts [1 ]) + "S" ;
465
+ ((LapRaceOutputHTML )output_HTML ).printCombined ();
484
466
}
485
467
}
0 commit comments