Skip to content

Commit

Permalink
Tidying.
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamkirby committed Dec 18, 2024
1 parent 4f134f4 commit 5119194
Show file tree
Hide file tree
Showing 45 changed files with 517 additions and 459 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

public enum CompletionStatus {

COMPLETED, // Completed race or series.
DNS, // Did Not Start: runner appears in entry but no finish(es) recorded.
DNF // Did Not Finish: did not complete all legs or sufficient component races.
/**
* Completed race or series.
*/
COMPLETED,

/**
* Did Not Start: runner appears in entry but no finish(es) recorded.
*/
DNS,

/**
* Did Not Finish: did not complete all legs or sufficient component races.
*/
DNF
}
50 changes: 26 additions & 24 deletions src/main/java/org/grahamkirby/race_timing/common/Normalisation.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.Set;

@SuppressWarnings("StringTemplateMigration")
public class Normalisation {

private static final int SECONDS_PER_HOUR = 3600;
Expand All @@ -20,11 +21,11 @@ public Normalisation(final Race race) {
this.race = race;
}

public String getFirstName(final String name) {
static String getFirstName(final String name) {
return name.split(" ")[0];
}

public String getLastName(final String name) {
static String getLastName(final String name) {

return Arrays.stream(name.split(" ")).toList().getLast();
}
Expand Down Expand Up @@ -106,76 +107,77 @@ private String lookupInStopWords(final String word) {

// Try case insensitive match.
return race.capitalisation_stop_words.stream().
filter(w -> w.equalsIgnoreCase(word)).
findFirst().
orElse(null);
filter(w -> w.equalsIgnoreCase(word)).
findFirst().
orElse(null);
}

private boolean isTitleCase(final String input) {
@SuppressWarnings("TypeMayBeWeakened")
private static boolean isTitleCase(final String input) {

return !Character.isLowerCase(input.charAt(0)) &&
input.chars().boxed().skip(1).noneMatch(Character::isUpperCase);
input.chars().boxed().skip(1).noneMatch(Character::isUpperCase);
}

private static String replaceAllMapEntries(final String s, final Map<String, String> normalisation_map) {

String result = s;

for (final String key : normalisation_map.keySet()) {
for (final Map.Entry<String, String> entry : normalisation_map.entrySet()) {

final String value = normalisation_map.get(key);
result = result.replaceAll("(?i)" + key, value);
final String value = entry.getValue();
result = result.replaceAll(STR."(?i)\{entry.getKey()}", value);
}
return result;
}

public static Duration parseTime(String element) {
public static Duration parseTime(final String element) {

try {
return parseTime(element, ":");
}
catch (Exception _) {
} catch (final RuntimeException _) {
return parseTime(element, "\\.");
}
}

private static Duration parseTime(String element, final String separator) {

element = element.strip();
if (element.startsWith(separator)) element = "0" + element;
if (element.endsWith(separator)) element = element + "0";
if (element.startsWith(separator)) element = STR."0\{element}";
if (element.endsWith(separator)) element = STR."\{element}0";

try {
final String[] parts = element.split(separator);
final String time_as_ISO = STR."PT\{hours(parts)}\{minutes(parts)}\{seconds(parts)}";

return Duration.parse(time_as_ISO);
}
catch (Exception _) {
throw new RuntimeException("illegal time: " + element);
return Duration.parse(STR."PT\{hours(parts)}\{minutes(parts)}\{seconds(parts)}");

} catch (final RuntimeException _) {
throw new RuntimeException(STR."illegal time: \{element}");
}
}

public static String format(final Duration duration) {

final long s = duration.getSeconds();
final long seconds = duration.getSeconds();
final int n = duration.getNano();

String result = String.format("0%d:%02d:%02d", s / SECONDS_PER_HOUR, (s % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE, (s % SECONDS_PER_MINUTE));
String result = String.format("0%d:%02d:%02d", seconds / SECONDS_PER_HOUR, (seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE, (seconds % SECONDS_PER_MINUTE));
if (n > 0) {
double fractional_seconds = n / NANOSECONDS_PER_SECOND;
final double fractional_seconds = n / NANOSECONDS_PER_SECOND;
result += String.format("%1$,.3f", fractional_seconds).substring(1);
while (result.endsWith("0")) result = result.substring(0, result.length() - 1);
}
return result;
}

private static String hours(final String[] parts) {
return parts.length > 2 ? parts[0] + "H" : "";
return parts.length > 2 ? STR."\{parts[0]}H" : "";
}

private static String minutes(final String[] parts) {
return (parts.length > 2 ? parts[1] : parts[0]) + "M";
}

private static String seconds(final String[] parts) {
return (parts.length > 2 ? parts[2] : parts[1]) + "S";
}
Expand Down
Loading

0 comments on commit 5119194

Please sign in to comment.