03.04.2014 version 0.2-SNAPSHOT
This project provides a simple API to manage National Id numbers. The API uses the Java 8 functional programming paradigm which results in a much lighter, flexible and extensible API.
At the moment there is an implementation for 5 nordic countries and Ireland:
For each of these countries the library provides:
- Validation
- Gender calculation
- Birthday calculation
- Age calculation
forId("13020955966");
new NorwegianIdNumber("13020955966");
new NorwegianIdNumber("13020955966", LOCALE_NOR);
forId("13020955966").isValid(NorwegianIdNumber::valid)
forId("540629-7407").isValid(FinnishIdNumber::Valid)
forId("13020955966", LOCALE_NOR).gender(NorwegianIdNumber::gender)
swedishIdNumber.gender(SwedishIdNumber::gender)
forId("1406108548").birthday(DanishIdNumber::birthday)
danishIdNumber.age(DanishIdNumber::birthday)
- JDK 8
- Gradle v. 1.9 or newer (optional, you can also just use gradlew (*nix) or gradlew.bat (win)
- Clone git repository at: https://github.com/kantega/KantId.git
- Compile with Java 8 and Gradle: "gradle build"
- Gradle: group: 'no.kantega', name: 'KantId', version: '0.1'
- Maven:
<dependency>
<groupId>no.kantega</groupId>
<artifactId>KantId</artifactId>
<version>0.1</version>
</dependency>
One of our main object is to encourage further API development by addition of more countries or functionality. We have attempted to make the API easy to extend by leveraging the Java 8 functional paradigm. Here follows two ways to extend the API: #####By extending IdNumber class
- Create a class with a proper constructor or factory method for new instances.
public MyIdNumber(final String idToken) {
super(idToken);
}
public static MyIdNumber forId(String idToken) {
return new MyIdNumber(idToken);
}
- Implement methods you want to use with your class, this doesn't mean that you have to override methods! Just implement needed functionality. Use optionally static methods for more fluent usage pattern.
public static Optional<Gender> gender(IdNumber idNumber) {
char genderBit = idNumber.getIdToken().charAt(GENDER_BIT);
if (isDigit(genderBit)) {
return genderBit % 2 == 0 ? Optional.of(FEMALE) : Optional.of(MALE);
}
return empty();
}
- See usage patterns above, use your class via static methods or instance methods.
- Implement supports(Locale locale) method in no.kantega.id.api.LocalIdNumber to support your locale(s).
@Override
protected boolean supports(Locale locale) {
return locale != null && MY_COUNTRY.equals(locale.getCountry());
}
- JavaDoc
- Wiki pages for National identification numbers
Published under Apache License, version 2.0