diff --git a/src/main/java/dev/personnummer/Personnummer.java b/src/main/java/dev/personnummer/Personnummer.java index e1ba985..baae5bb 100644 --- a/src/main/java/dev/personnummer/Personnummer.java +++ b/src/main/java/dev/personnummer/Personnummer.java @@ -51,6 +51,50 @@ public static Personnummer parse(String personnummer) throws PersonnummerExcepti private final boolean isMale; private final boolean isFemale; + public Boolean isMale() { + return this.isMale; + } + + public Boolean isFemale() { + return this.isFemale; + } + + public String separator() { + return this.getAge() >= 100 ? "+" : "-"; + } + + public String getFullYear() { + return fullYear; + } + + public String getCentury() { + return century; + } + + public String getYear() { + return year; + } + + public String getMonth() { + return month; + } + + public String getDay() { + return day; + } + + public String getNumbers() { + return numbers; + } + + public String getControlNumber() { + return controlNumber; + } + + public int getAge() { + return (LocalDate.of(Integer.parseInt(this.fullYear), Integer.parseInt(this.month), this.realDay).until(LocalDate.now())).getYears(); + } + /** * Create a new Personnummber object from a string. * In case options is not passed, they will default to accept any personal and coordination numbers. @@ -61,12 +105,12 @@ public static Personnummer parse(String personnummer) throws PersonnummerExcepti */ public Personnummer(String personnummer, Options options) throws PersonnummerException { if (personnummer == null) { - throw new PersonnummerException(); + throw new PersonnummerException("Failed to parse personal identity number. Invalid input."); } Matcher matches = regexPattern.matcher(personnummer); if (!matches.find()) { - throw new PersonnummerException(); + throw new PersonnummerException("Failed to parse personal identity number. Invalid input."); } String century; @@ -88,7 +132,7 @@ public Personnummer(String personnummer, Options options) throws PersonnummerExc if (options.allowCoordinationNumber) { day = day > 60 ? day - 60 : day; } else if(day > 60) { - throw new PersonnummerException(); + throw new PersonnummerException("Invalid personal identity number."); } this.realDay = day; @@ -106,7 +150,7 @@ public Personnummer(String personnummer, Options options) throws PersonnummerExc // 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)) { - throw new PersonnummerException(); + throw new PersonnummerException("Invalid personal identity number."); } } @@ -121,10 +165,6 @@ public Personnummer(String personnummer) throws PersonnummerException { this(personnummer, new Options()); } - public int getAge() { - return (LocalDate.of(Integer.parseInt(this.fullYear), Integer.parseInt(this.month), this.realDay).until(LocalDate.now())).getYears(); - } - /** * Format the personal identity number into a valid string (YYMMDD-/+XXXX) * If longFormat is true, it will include the century (YYYYMMDD-/+XXXX) @@ -146,46 +186,6 @@ public String format(boolean longFormat) { return (longFormat ? this.fullYear : this.year) + this.month + this.day + separator() + numbers; } - public Boolean isMale() { - return this.isMale; - } - - public Boolean isFemale() { - return this.isFemale; - } - - public String separator() { - return this.getAge() >= 100 ? "+" : "-"; - } - - public String getFullYear() { - return fullYear; - } - - public String getCentury() { - return century; - } - - public String getYear() { - return year; - } - - public String getMonth() { - return month; - } - - public String getDay() { - return day; - } - - public String getNumbers() { - return numbers; - } - - public String getControlNumber() { - return controlNumber; - } - /** * Validate a Swedish personal identity number. * diff --git a/src/main/java/dev/personnummer/PersonnummerException.java b/src/main/java/dev/personnummer/PersonnummerException.java index 508c0c7..be13965 100644 --- a/src/main/java/dev/personnummer/PersonnummerException.java +++ b/src/main/java/dev/personnummer/PersonnummerException.java @@ -1,3 +1,7 @@ package dev.personnummer; -public class PersonnummerException extends Exception { } +public class PersonnummerException extends Exception { + PersonnummerException(String message) { + super(message); + } +}