Skip to content

Commit

Permalink
feat: Implemented interim numbers.
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Tegnér <johannes@jitesoft.com>
  • Loading branch information
Johannestegner committed Mar 13, 2023
1 parent 0865ef2 commit 09eec33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
19 changes: 17 additions & 2 deletions src/main/java/dev/personnummer/Personnummer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
*/
public final class Personnummer implements Comparable<Personnummer> {
private static final Pattern regexPattern;
private static final Pattern interimPatternTest;
private static final String interimTestStr = "(?![-+])\\D";

static {
regexPattern = Pattern.compile("^(\\d{2})?(\\d{2})(\\d{2})(\\d{2})([-+]?)?((?!000)\\d{3})(\\d?)$");
regexPattern = Pattern.compile("^(\\d{2})?(\\d{2})(\\d{2})(\\d{2})([-+]?)?((?!000)\\d{3}|[TRSUWXJKLMN]\\d{2})(\\d?)$");
interimPatternTest = Pattern.compile(interimTestStr);
}

/**
Expand Down Expand Up @@ -112,6 +115,13 @@ public Personnummer(String personnummer, Options options) throws PersonnummerExc
throw new PersonnummerException("Failed to parse personal identity number. Invalid input.");
}

if (!options.allowInterimNumbers && interimPatternTest.matcher(personnummer).find()) {
throw new PersonnummerException(
personnummer +
" contains non-integer characters and options are set to not allow interim numbers"
);
}

Matcher matches = regexPattern.matcher(personnummer);
if (!matches.find()) {
throw new PersonnummerException("Failed to parse personal identity number. Invalid input.");
Expand Down Expand Up @@ -154,9 +164,14 @@ public Personnummer(String personnummer, Options options) throws PersonnummerExc

this.isMale = Integer.parseInt(Character.toString(this.numbers.charAt(2))) % 2 == 1;

String nums = matches.group(6);
if (options.allowInterimNumbers) {
nums = nums.replaceFirst(interimTestStr, "1");
}

// The format passed to Luhn method is supposed to be YYmmDDNNN
// Hence all numbers that are less than 10 (or in last case 100) will have leading 0's added.
if (luhn(String.format("%s%s%s%s", this.year, this.month, this.day, matches.group(6))) != Integer.parseInt(this.controlNumber)) {
if (luhn(String.format("%s%s%s%s", this.year, this.month, this.day, nums)) != Integer.parseInt(this.controlNumber)) {
throw new PersonnummerException("Invalid personal identity number.");
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/DataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ public static void initialize() throws IOException {
JSONObject current = rootObject.getJSONObject(i);
interimNr.add(new PersonnummerData(
current.getLong("integer"),
current.getString("long_format"),
current.getString("short_format"),
current.getString("separated_format"),
current.getString("separated_long"),
current.getBoolean("valid"),
current.getString("type")
));
current.getString("type"),
false, // ignore
false // ignore
)
);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/InterimnummerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ public void testValidateInvalidInterim(PersonnummerData ssn) {
public void testFormatLongInterim(PersonnummerData ssn) throws PersonnummerException {
Personnummer pnr = Personnummer.parse(ssn.longFormat, opts);

assertEquals(pnr.format(false), ssn.longFormat);
assertEquals(pnr.format(), ssn.separatedFormat);
assertEquals(pnr.format(false), ssn.separatedFormat);
assertEquals(pnr.format(true), ssn.longFormat);
}

@ParameterizedTest
@MethodSource("DataProvider#getValidInterimNumbers")
public void testFormatShortInterim(PersonnummerData ssn) throws PersonnummerException {
Personnummer pnr = Personnummer.parse(ssn.shortFormat, opts);

assertEquals(pnr.format(false), ssn.longFormat);
assertEquals(pnr.format(), ssn.separatedFormat);
assertEquals(pnr.format(false), ssn.separatedFormat);
assertEquals(pnr.format(true), ssn.longFormat);
}

@ParameterizedTest
Expand Down

0 comments on commit 09eec33

Please sign in to comment.