Skip to content

Commit

Permalink
Make IbanCheckDigit#validate return false for all invalid strings (
Browse files Browse the repository at this point in the history
…closes #188)

It does not make sense to raise an exception in a validation method that returns a boolean.
  • Loading branch information
marcwrobel committed Oct 15, 2022
1 parent 2ae909d commit 9e0394e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
the `SwiftPattern` rewrite. And because this feature is probably not used outside of jbanking code, we choose to drop
it. But if you were using it, please let us know by [raising an issue](https://github.com/marcwrobel/jbanking/issues),
so we can add it back.
- (**breaking change**) Make `IbanCheckDigit#validate` return `false` for `null` or less than 5 characters strings
(#188).
- Get rid of regexes to validate BICs (#170). This significantly increased the performances of BIC validation and
creation (+200-300%).
- Get rid of regexes to validate Creditor Identifiers (#172). This significantly increased the performances of
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/fr/marcwrobel/jbanking/iban/IbanCheckDigit.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public enum IbanCheckDigit {
* @throws IllegalArgumentException if the given IBAN is {@code null} or if its size is not at least four characters.
*/
public boolean validate(String iban) {
validateString(iban);
if (iban == null || iban.length() <= BBAN_INDEX) {
return false;
}

// It is easier to extract the check digit string and compare it with the invalid check digits, but we want to avoid
// unnecessary objects creation.
Expand All @@ -59,23 +61,19 @@ public boolean validate(String iban) {
* @return the given IBAN check digit.
*/
public String calculate(String iban) {
validateString(iban);

int modulusResult = modulus(iban);
int charValue = (98 - modulusResult);
String checkDigit = Integer.toString(charValue);

return (charValue > 9 ? checkDigit : "0" + checkDigit);
}

private void validateString(String iban) {
if (iban == null) {
throw new IllegalArgumentException("the iban argument cannot be null");
}

if (iban.length() <= BBAN_INDEX) {
throw new IllegalArgumentException("the iban argument size must be greater than " + BBAN_INDEX);
}

int modulusResult = modulus(iban);
int charValue = (98 - modulusResult);
String checkDigit = Integer.toString(charValue);

return (charValue > 9 ? checkDigit : "0" + checkDigit);
}

private int modulus(String iban) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void nullIsNotValidForCalculation() {
}

@Test
void nullIsNotValidForValidation() {
assertThrows(IllegalArgumentException.class, () -> IbanCheckDigit.INSTANCE.validate(null));
void nullIsNotValid() {
assertFalse(IbanCheckDigit.INSTANCE.validate(null));
}

@Test
Expand All @@ -60,8 +60,8 @@ void ibanSizeLowerThanFourIsNotValidForCalculation() {
}

@Test
void ibanSizeLowerThanFourIsIsNotValidForValidation() {
assertThrows(IllegalArgumentException.class, () -> IbanCheckDigit.INSTANCE.validate("123"));
void ibanSizeLowerThanFourIsNotValid() {
assertFalse(IbanCheckDigit.INSTANCE.validate("123"));
}

@ParameterizedTest
Expand Down

0 comments on commit 9e0394e

Please sign in to comment.