io.github.jakaarl.iban.IbanValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of iban-validator Show documentation
Show all versions of iban-validator Show documentation
A Java library for IBAN validation
The newest version!
package io.github.jakaarl.iban;
import static io.github.jakaarl.iban.util.NullCheck.requireNonNull;
import java.util.List;
import io.github.jakaarl.iban.spi.IbanValidation;
import io.github.jakaarl.iban.spi.IbanValidationService;
/**
* Validator for IBAN account numbers.
*
* @see IbanValidationService
* @see IBAN on Wikipedia
*/
public class IbanValidator {
private static final IbanValidationService VALIDATION_SERVICE = new IbanValidationService();
private final boolean requireAdditionalValidations;
/**
* Constructs a validator which does not require additional validations.
*/
public IbanValidator() {
this(false);
}
/**
* Constructs a validator.
*
* @param requireAdditionalValidations whether or not to require additional validations.
*/
public IbanValidator(boolean requireAdditionalValidations) {
this.requireAdditionalValidations = requireAdditionalValidations;
}
/**
* Validates given IBAN string. If additional validations are required, returns false
if no
* {@link IbanValidation} can validate the IBAN. If several validations can, all of them must return
* true
in order to pass validation.
*
* @param ibanString IBAN to validate.
*
* @return true
, if valid.
*
* @throws NullPointerException if given IBAN string is null
.
* @throws IllegalArgumentException if given IBAN string does not match a generic IBAN pattern.
*/
public boolean validate(String ibanString) {
Iban iban = new Iban(ibanString);
return validate(iban);
}
/**
* Validates given Iban
. If additional validations are required, returns false
if no
* {@link IbanValidation} can validate the IBAN. If several validations can, all of them must return
* true
in order to pass validation.
*
* @param iban IBAN to validate.
*
* @return true
, if valid.
*
* @throws NullPointerException if given IBAN is null
.
*/
public boolean validate(Iban iban) {
requireNonNull(iban);
if (requireAdditionalValidations) {
List validations = VALIDATION_SERVICE.getValidations(iban);
if (validations.isEmpty()) {
return false;
}
for (IbanValidation validation : validations) {
if (!validation.isValid(iban)) {
return false;
}
}
}
return true;
}
}