The way you do this varies depending on what you used above:
- if you’ve downloaded or forked source from github, you can use IntelliJ. Open source/subprojects.ipr and start hacking
- if you’ve downloaded the jars (and source zips), create a project or add them to an existing project in your favourite IDE (if it isn’t IntelliJ, then switch now)
You can either add the hdn-number-client.jar as is (simple option), or, for more refinement and better integration with other HDN tools, its dependent jar files:
- number
- common-digits
- and the third-party library annotations.jar. This is a compile-time only dependency
This list may change. To find the most up-to-date list, either extract META-INF/MANIFEST.MF from hdn-number-client.jar and read the Class-Path entry, or open the IntelliJ project (source/subprojects.ipr) and look at the dependencies of the module dbs-request-client (sensibly, module names match jar names and source zip names). Note that you’ll not need the common-commandLine module (jar) or jopt-simple library.
Modelling NHS Numbers
The ‘guts’ of the java library’s API is the class NhsNumber. It’s in the package uk.nhs.hdn.number.
The simplest way to create NhsNumber is to use the static valueOf method:
final NhsNumber nhsNumber = NhsNumber.valueOf("1234567881");
Sometimes NHS Numbers are used with separators such as space or hyphen. valueOf supports these too, provided they delineate the standard 3-3-4 pattern of digits.
final NhsNumber nhsNumberWithSpaceSeparators = NhsNumber.valueOf("123 456 7881");
final NhsNumber nhsNumberWithHyphenSeparators = NhsNumber.valueOf("123-456-7881");
You’ll get an IllegalArgumentException if the number’s format is not understood.
In addition, the number’s check digit is also computed and compared with the tenth digit parsed. If they don’t match, an IllegalArgumentException is thrown.
It’s possible to format a NHS number for display purposes. To get it with spaces, use the formattedForDisplay method:
final String fomattedWithSpaceSeparatorsForHumanDisplay = nhsNumber.formattedForDisplay();
Any other separator, such as a hyphen, can be specified:
final String fomattedWithHyphenSeparatorsForHumanDisplay = nhsNumber.formattedForDisplay('-');
Calculating check digits
This uses the class NhsNumberExtractingCheckDigitCalculator. It’s in the package uk.nhs.hdn.number.
The normal way to use it is to do as follows:
final Digits nineDigits = Digits.digits("123456788");
final Digit digit = NhsNumberExtractingCheckDigitCalculator.NhsNumberExtractingCheckDigitCalculatorInstance.calculateCheckDigit(nineDigits);
Digit is a class that models, as an enum, the digits 0 to 9. Digits is a class that models a sequence of Digit.
If it is impossible to calculate a check digit (as some sequences are unpermitted), an IncorrectNumberCouldNotCalculateCheckDigitIllegalStateException will be thrown.