refactor(parser): expand SplitName record to 5 fields

Add title, maidenName, and annotation fields (all nullable) to
SplitName. All existing call sites pass null for new fields. Test
assertions updated to document the null-by-default contract.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marcel
2026-04-08 11:46:09 +02:00
parent d6e74972eb
commit 1e1921e0fa
2 changed files with 23 additions and 5 deletions

View File

@@ -23,7 +23,13 @@ public class PersonNameParser {
private static final Pattern MULTI_SEPARATOR = Pattern.compile("\\s+(?:und|u)\\s+"); private static final Pattern MULTI_SEPARATOR = Pattern.compile("\\s+(?:und|u)\\s+");
private static final Pattern SLASH_SEPARATOR = Pattern.compile("//"); private static final Pattern SLASH_SEPARATOR = Pattern.compile("//");
public record SplitName(String firstName, String lastName) {} public record SplitName(
String title,
String firstName,
String lastName,
String maidenName,
String annotation
) {}
/** /**
* Parses the "An" field from the ODS into individual normalised name strings. * Parses the "An" field from the ODS into individual normalised name strings.
@@ -118,7 +124,7 @@ public class PersonNameParser {
*/ */
public static SplitName split(String rawName) { public static SplitName split(String rawName) {
if (rawName == null || rawName.isBlank()) { if (rawName == null || rawName.isBlank()) {
return new SplitName("?", "?"); return new SplitName(null, "?", "?", null, null);
} }
String cleaned = GEB_PATTERN.matcher(rawName).replaceAll("").trim(); String cleaned = GEB_PATTERN.matcher(rawName).replaceAll("").trim();
@@ -132,15 +138,15 @@ public class PersonNameParser {
if (lastName != null) { if (lastName != null) {
String firstName = cleaned.substring(0, cleaned.length() - lastName.length()).trim(); String firstName = cleaned.substring(0, cleaned.length() - lastName.length()).trim();
if (firstName.isBlank()) firstName = cleaned; if (firstName.isBlank()) firstName = cleaned;
return new SplitName(firstName, lastName); return new SplitName(null, firstName, lastName, null, null);
} }
int lastSpace = cleaned.lastIndexOf(' '); int lastSpace = cleaned.lastIndexOf(' ');
if (lastSpace > 0) { if (lastSpace > 0) {
return new SplitName(cleaned.substring(0, lastSpace).trim(), cleaned.substring(lastSpace + 1).trim()); return new SplitName(null, cleaned.substring(0, lastSpace).trim(), cleaned.substring(lastSpace + 1).trim(), null, null);
} }
return new SplitName(cleaned, "?"); return new SplitName(null, cleaned, "?", null, null);
} }
/** Returns the known last name that the given string ends with, or null. */ /** Returns the known last name that the given string ends with, or null. */

View File

@@ -81,6 +81,9 @@ class PersonNameParserTest {
PersonNameParser.SplitName result = PersonNameParser.split("Walter de Gruyter"); PersonNameParser.SplitName result = PersonNameParser.split("Walter de Gruyter");
assertThat(result.firstName()).isEqualTo("Walter"); assertThat(result.firstName()).isEqualTo("Walter");
assertThat(result.lastName()).isEqualTo("de Gruyter"); assertThat(result.lastName()).isEqualTo("de Gruyter");
assertThat(result.title()).isNull();
assertThat(result.maidenName()).isNull();
assertThat(result.annotation()).isNull();
} }
@Test @Test
@@ -107,22 +110,31 @@ class PersonNameParserTest {
@Test @Test
void split_gebAnnotation_stripped() { void split_gebAnnotation_stripped() {
PersonNameParser.SplitName result = PersonNameParser.split("Eugenie de Gruyter geb. Müller"); PersonNameParser.SplitName result = PersonNameParser.split("Eugenie de Gruyter geb. Müller");
assertThat(result.title()).isNull();
assertThat(result.firstName()).isEqualTo("Eugenie"); assertThat(result.firstName()).isEqualTo("Eugenie");
assertThat(result.lastName()).isEqualTo("de Gruyter"); assertThat(result.lastName()).isEqualTo("de Gruyter");
assertThat(result.maidenName()).isNull();
assertThat(result.annotation()).isNull();
} }
@Test @Test
void split_null_returnsPlaceholder() { void split_null_returnsPlaceholder() {
PersonNameParser.SplitName result = PersonNameParser.split(null); PersonNameParser.SplitName result = PersonNameParser.split(null);
assertThat(result.title()).isNull();
assertThat(result.firstName()).isEqualTo("?"); assertThat(result.firstName()).isEqualTo("?");
assertThat(result.lastName()).isEqualTo("?"); assertThat(result.lastName()).isEqualTo("?");
assertThat(result.maidenName()).isNull();
assertThat(result.annotation()).isNull();
} }
@Test @Test
void split_blank_returnsPlaceholder() { void split_blank_returnsPlaceholder() {
PersonNameParser.SplitName result = PersonNameParser.split(" "); PersonNameParser.SplitName result = PersonNameParser.split(" ");
assertThat(result.title()).isNull();
assertThat(result.firstName()).isEqualTo("?"); assertThat(result.firstName()).isEqualTo("?");
assertThat(result.lastName()).isEqualTo("?"); assertThat(result.lastName()).isEqualTo("?");
assertThat(result.maidenName()).isNull();
assertThat(result.annotation()).isNull();
} }
@Test @Test