-
Notifications
You must be signed in to change notification settings - Fork 241
add Hungarian id number generator #1555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kingthorin
merged 1 commit into
datafaker-net:main
from
cbxp:feature/hungarian-id-number
May 19, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/net/datafaker/idnumbers/HungarianIdNumber.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package net.datafaker.idnumbers; | ||
|
|
||
| import net.datafaker.providers.base.BaseProviders; | ||
| import net.datafaker.providers.base.IdNumber; | ||
| import net.datafaker.providers.base.PersonIdNumber; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| import static java.lang.Character.getNumericValue; | ||
| import static net.datafaker.idnumbers.Utils.randomGender; | ||
|
|
||
| /** | ||
| * Hungarian personal identification number | ||
| * <p> | ||
| * The structure of such number is GYYMMDDXXC, whereas | ||
| * <ul> | ||
| * <li>G indicates century of birth and gender,</li> | ||
| * <li>YYMMDD indicates birth year, month and day,</li> | ||
| * <li>XX is the serial number, and</li> | ||
| * <li>C is a checksum digit.</li> | ||
| * </ul> | ||
| * <p> | ||
| * See <a href="https://en.wikipedia.org/wiki/National_identification_number#Hungary">Hungarian identification number</a> | ||
| */ | ||
| public class HungarianIdNumber implements IdNumberGenerator { | ||
|
|
||
| private static final DateTimeFormatter BIRTHDAY_FORMAT = DateTimeFormatter.ofPattern("yyMMdd"); | ||
|
|
||
| @Override | ||
| public String countryCode() { | ||
| return "HU"; | ||
| } | ||
|
|
||
| @Override | ||
| public String generateInvalid(BaseProviders faker) { | ||
| String validIdNumber = generateValid(faker); | ||
| String basePart = validIdNumber.substring(0, 9); | ||
| int validCheckDigit = getNumericValue(validIdNumber.charAt(9)); | ||
| int invalidCheckDigit = (validCheckDigit + 1) % 10; | ||
| return basePart + invalidCheckDigit; | ||
| } | ||
|
|
||
| @Override | ||
| public PersonIdNumber generateValid(BaseProviders faker, IdNumber.IdNumberRequest request) { | ||
| LocalDate birthday = faker.timeAndDate().birthday(); | ||
| PersonIdNumber.Gender gender = randomGender(faker); | ||
| int checkDigit; | ||
| String basePart; | ||
| do { | ||
| basePart = basePart(faker, birthday, gender); | ||
| checkDigit = getCheckDigit(basePart); | ||
| } while (checkDigit == 10); | ||
|
|
||
| String idNumber = basePart + checkDigit; | ||
| return new PersonIdNumber(idNumber, birthday, gender); | ||
| } | ||
|
|
||
| private String basePart(BaseProviders faker, LocalDate birthday, PersonIdNumber.Gender gender) { | ||
| return firstDigit(birthday.getYear(), gender) + | ||
| BIRTHDAY_FORMAT.format(birthday) + | ||
| faker.number().digits(2); | ||
| } | ||
|
|
||
| static int getCheckDigit(String basePart) { | ||
| char[] numbers = basePart.toCharArray(); | ||
| int summ = 0; | ||
| for (int i = 0; i < numbers.length; i++) { | ||
| summ += getNumericValue(numbers[i]) * (i + 1); | ||
| } | ||
|
|
||
| return summ % 11; | ||
| } | ||
|
|
||
| static int firstDigit(int birthYear, PersonIdNumber.Gender gender) { | ||
| return switch (gender) { | ||
| case MALE -> isInRange(birthYear) ? 1 : 3; | ||
| case FEMALE -> isInRange(birthYear) ? 2 : 4; | ||
| }; | ||
| } | ||
|
|
||
| private static boolean isInRange(int birthYear) { | ||
| return birthYear >= 1900 && birthYear <= 1999; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/java/net/datafaker/idnumbers/HungarianIdNumberTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package net.datafaker.idnumbers; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static net.datafaker.providers.base.PersonIdNumber.Gender.FEMALE; | ||
| import static net.datafaker.providers.base.PersonIdNumber.Gender.MALE; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| class HungarianIdNumberTest { | ||
|
|
||
| @Test | ||
| void firstDigit() { | ||
| assertThat(HungarianIdNumber.firstDigit(1899, MALE)).isEqualTo(3); | ||
| assertThat(HungarianIdNumber.firstDigit(1900, MALE)).isEqualTo(1); | ||
| assertThat(HungarianIdNumber.firstDigit(1901, MALE)).isEqualTo(1); | ||
| assertThat(HungarianIdNumber.firstDigit(1899, FEMALE)).isEqualTo(4); | ||
| assertThat(HungarianIdNumber.firstDigit(1900, FEMALE)).isEqualTo(2); | ||
| assertThat(HungarianIdNumber.firstDigit(1901, FEMALE)).isEqualTo(2); | ||
| assertThat(HungarianIdNumber.firstDigit(1999, MALE)).isEqualTo(1); | ||
| assertThat(HungarianIdNumber.firstDigit(2000, MALE)).isEqualTo(3); | ||
| assertThat(HungarianIdNumber.firstDigit(1999, FEMALE)).isEqualTo(2); | ||
| assertThat(HungarianIdNumber.firstDigit(2000, FEMALE)).isEqualTo(4); | ||
| } | ||
|
|
||
| @Test | ||
| void checkDigit() { | ||
| assertThat(HungarianIdNumber.getCheckDigit("807159215")).isEqualTo(3); | ||
| assertThat(HungarianIdNumber.getCheckDigit("100000000")).isEqualTo(1); | ||
| assertThat(HungarianIdNumber.getCheckDigit("010000000")).isEqualTo(2); | ||
| assertThat(HungarianIdNumber.getCheckDigit("001000000")).isEqualTo(3); | ||
| assertThat(HungarianIdNumber.getCheckDigit("000000001")).isEqualTo(9); | ||
| assertThat(HungarianIdNumber.getCheckDigit("170010103")).isEqualTo(10); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.