Skip to content

Commit

Permalink
Speed up SvSe number validation (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin authored Aug 21, 2023
1 parent 6f1bf54 commit 6269e3f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/main/java/net/datafaker/idnumbers/EnZAIdNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.datafaker.providers.base.BaseProviders;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;

/**
Expand All @@ -13,7 +12,6 @@

public class EnZAIdNumber implements IdNumbers {

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");
private static final String[] VALID_PATTERN = {"##########08#", "##########18#"};

/**
Expand Down Expand Up @@ -89,9 +87,9 @@ boolean validSsn(String ssn) {
*/
private boolean parseDate(String ssn) {
String dateString = ssn.substring(0, 6);
if (ChronoField.YEAR.range().isValidIntValue(Integer.parseInt(ssn.substring(0, 2)))) {
if (ChronoField.MONTH_OF_YEAR.range().isValidIntValue(Integer.parseInt(ssn.substring(2, 4)))) {
if (ChronoField.DAY_OF_MONTH.range().isValidIntValue(Integer.parseInt(ssn.substring(4)))) {
if (ChronoField.YEAR.range().isValidIntValue(Integer.parseInt(dateString.substring(0, 2)))) {
if (ChronoField.MONTH_OF_YEAR.range().isValidIntValue(Integer.parseInt(dateString.substring(2, 4)))) {
if (ChronoField.DAY_OF_MONTH.range().isValidIntValue(Integer.parseInt(dateString.substring(4)))) {
LocalDate date = LocalDate.parse(dateString, DATE_TIME_FORMATTER);
// want to check that the parsed date is equal to the supplied data, most of the attempts will fail
String reversed = date.format(DATE_TIME_FORMATTER);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/datafaker/idnumbers/IdNumbers.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package net.datafaker.idnumbers;

import java.time.format.DateTimeFormatter;

public interface IdNumbers {
DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");
}
26 changes: 15 additions & 11 deletions src/main/java/net/datafaker/idnumbers/SvSEIdNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import net.datafaker.providers.base.BaseProviders;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDate;
import java.time.temporal.ChronoField;

/**
* Implementation based on the definition at
Expand Down Expand Up @@ -48,7 +47,7 @@ boolean validSwedishSsn(String ssn) {
if (parseDate(ssn)) {
return false;
}
} catch (ParseException e) {
} catch (Exception e) {
return false;
}

Expand All @@ -57,14 +56,19 @@ boolean validSwedishSsn(String ssn) {
return checksum == calculatedChecksum;
}

private boolean parseDate(String ssn) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
private boolean parseDate(String ssn) {
String dateString = ssn.substring(0, 6);
Date date = sdf.parse(dateString);

// want to check that the parsed date is equal to the supplied data, most of the attempts will fail
String reversed = sdf.format(date);
return !reversed.equals(dateString);
if (ChronoField.YEAR.range().isValidIntValue(Integer.parseInt(dateString.substring(0, 2)))) {
if (ChronoField.MONTH_OF_YEAR.range().isValidIntValue(Integer.parseInt(dateString.substring(2, 4)))) {
if (ChronoField.DAY_OF_MONTH.range().isValidIntValue(Integer.parseInt(dateString.substring(4)))) {
LocalDate date = LocalDate.parse(dateString, DATE_TIME_FORMATTER);
// want to check that the parsed date is equal to the supplied data, most of the attempts will fail
String reversed = date.format(DATE_TIME_FORMATTER);
return !reversed.equals(dateString);
}
}
}
return false;
}

private int calculateChecksum(String number) {
Expand Down

0 comments on commit 6269e3f

Please sign in to comment.