Skip to content

Commit 618d43d

Browse files
committed
refactor(iban): derive iban length and end indices from patterns
Remove redundant data from IbanRegistry's enum constants: - IBAN length is now computed from the BBAN pattern instead of an explicit withIbanLength(...) call. - withBankCode/withBranchCode/withAccountNumber/withNationalCheckDigit now take (pattern, beginIndex); the end index is derived from the pattern's segment length. - StructureData stores its four positional components in an internal ComponentType -> Component map instead of eight parallel fields; public getters keep their existing signatures. - IbanPatternConverter.Segment's static list helpers (calculateTotalLength, allMatch, isAllNumeric) moved onto IbanPatternConverter itself; Segment stays a pure value object. - Updated call sites in IbanBuilder and the related tests accordingly.
1 parent 0847a0e commit 618d43d

25 files changed

Lines changed: 1700 additions & 1262 deletions

iban-commons/src/main/java/de/speedbanking/iban/AbstractNcdCountryValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public final boolean validateNationalCheckDigit(final char[] iban) {
110110
public final char[] calculateNationalCheckDigit(final char[] iban) {
111111
return ncdCalculator == null
112112
|| !IbanConfig.isCalculateNcd()
113-
? getCountryData().getNationalCheckDigitIndexRange().applyTo(iban)
113+
? getCountryData().getNationalCheckDigitComponent().extractFrom(iban)
114114
: ncdCalculator.calculateNationalCheckDigit(iban);
115115
}
116116

iban-commons/src/main/java/de/speedbanking/iban/CountryValidators.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,11 @@ public boolean validateIban(final char[] iban) {
925925
}
926926
}
927927

928-
/** NCD-aware Validator for Togo (TG), IBAN length: 28, BBAN pattern: {@code 5!c5!n12!n2!n} */
929-
static final class TG extends AbstractNcdCountryValidator {
928+
/** Validator for Togo (TG), IBAN length: 28, BBAN pattern: {@code 5!c5!n12!n2!n} */
929+
static final class TG extends AbstractCountryValidator {
930930
@Override
931931
public boolean validateIban(final char[] iban) {
932-
return isAllDigits(iban, 9, 28)
933-
&& validateNationalCheckDigit(iban);
932+
return isAllDigits(iban, 9, 28);
934933
}
935934
}
936935

iban-commons/src/main/java/de/speedbanking/iban/Iban.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import de.speedbanking.util.Country;
2121
import de.speedbanking.util.Currency;
22-
import de.speedbanking.util.IndexRange;
2322

2423
import java.io.IOException;
2524
import java.io.InvalidObjectException;
@@ -409,8 +408,8 @@ public String getBban() {
409408
* @since 1.8.0
410409
*/
411410
public String getBankCode() {
412-
if (bankCode == null && countryData.getBankCodeIndexRange() != null) {
413-
bankCode = countryData.getBankCodeIndexRange().applyTo(ibanStr);
411+
if (bankCode == null) {
412+
bankCode = countryData.getBankCodeComponent().extractFrom(ibanStr);
414413
}
415414
return bankCode;
416415
}
@@ -423,8 +422,11 @@ public String getBankCode() {
423422
* @since 1.8.0
424423
*/
425424
public String getBranchCode() {
426-
if (branchCode == null && countryData.getBranchCodeIndexRange() != null) {
427-
branchCode = countryData.getBranchCodeIndexRange().applyTo(ibanStr);
425+
if (branchCode == null) {
426+
IbanComponent component = countryData.getBranchCodeComponent();
427+
if (component != null) {
428+
branchCode = component.extractFrom(ibanStr);
429+
}
428430
}
429431
return branchCode;
430432
}
@@ -456,7 +458,7 @@ public String getBankAndBranchCode() {
456458
*/
457459
public String getAccountNumber() {
458460
if (accountNumber == null) {
459-
accountNumber = countryData.getAccountNumberIndexRange().applyTo(ibanStr);
461+
accountNumber = countryData.getAccountNumberComponent().extractFrom(ibanStr);
460462
}
461463
return accountNumber;
462464
}
@@ -469,8 +471,11 @@ public String getAccountNumber() {
469471
* @since 1.8.1
470472
*/
471473
public String getNationalCheckDigit() {
472-
if (nationalCheckDigit == null && countryData.hasNationalCheckDigit()) {
473-
nationalCheckDigit = countryData.getNationalCheckDigitIndexRange().applyTo(ibanStr);
474+
if (nationalCheckDigit == null) {
475+
IbanComponent component = countryData.getNationalCheckDigitComponent();
476+
if (component != null) {
477+
nationalCheckDigit = component.extractFrom(ibanStr);
478+
}
474479
}
475480
return nationalCheckDigit;
476481
}
@@ -538,10 +543,10 @@ public String toFormattedString() {
538543
* @since 1.8.5
539544
*/
540545
public String toComponentString() {
541-
IndexRange bankRange = countryData.getBankCodeIndexRange();
542-
IndexRange branchRange = countryData.getBranchCodeIndexRange();
543-
IndexRange accountRange = countryData.getAccountNumberIndexRange();
544-
IndexRange ncdRange = countryData.getNationalCheckDigitIndexRange();
546+
IbanComponent bankCompo = countryData.getBankCodeComponent();
547+
IbanComponent branchCompo = countryData.getBranchCodeComponent();
548+
IbanComponent accountCompo = countryData.getAccountNumberComponent();
549+
IbanComponent ncdCompo = countryData.getNationalCheckDigitComponent();
545550

546551
// fixed-size stack array (max 12 entries)
547552
int[] idx = new int[12];
@@ -550,20 +555,20 @@ public String toComponentString() {
550555
idx[count++] = IbanRegistry.INDEX_CHECK_DIGIT1;
551556
idx[count++] = IbanRegistry.INDEX_BBAN;
552557

553-
idx[count++] = bankRange.getBegin();
554-
idx[count++] = bankRange.getEnd();
558+
idx[count++] = bankCompo.getBeginIndex();
559+
idx[count++] = bankCompo.getEndIndex();
555560

556-
if (branchRange != null) {
557-
idx[count++] = branchRange.getBegin();
558-
idx[count++] = branchRange.getEnd();
561+
if (branchCompo != null) {
562+
idx[count++] = branchCompo.getBeginIndex();
563+
idx[count++] = branchCompo.getEndIndex();
559564
}
560565

561-
idx[count++] = accountRange.getBegin();
562-
idx[count++] = accountRange.getEnd();
566+
idx[count++] = accountCompo.getBeginIndex();
567+
idx[count++] = accountCompo.getEndIndex();
563568

564-
if (ncdRange != null) {
565-
idx[count++] = ncdRange.getBegin();
566-
idx[count++] = ncdRange.getEnd();
569+
if (ncdCompo != null) {
570+
idx[count++] = ncdCompo.getBeginIndex();
571+
idx[count++] = ncdCompo.getEndIndex();
567572
}
568573

569574
// insertion sort - optimal for small fixed arrays, avoids Arrays.sort overhead

0 commit comments

Comments
 (0)