Skip to content

Commit

Permalink
Added message to exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannestegner committed Jun 17, 2020
1 parent ffd15ac commit a3fa287
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
96 changes: 48 additions & 48 deletions src/main/java/dev/personnummer/Personnummer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.");
}
}

Expand All @@ -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)
Expand All @@ -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.
*
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/dev/personnummer/PersonnummerException.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package dev.personnummer;

public class PersonnummerException extends Exception { }
public class PersonnummerException extends Exception {
PersonnummerException(String message) {
super(message);
}
}

0 comments on commit a3fa287

Please sign in to comment.