public class CreditCardValidator extends Object implements IValidator<String>
Card prefixes and lengths have been taken from Wikipedia.
Modifier and Type | Class and Description |
---|---|
static class |
CreditCardValidator.CreditCard |
Constructor and Description |
---|
CreditCardValidator()
Construct.
|
CreditCardValidator(boolean failOnUnkown)
Construct.
|
Modifier and Type | Method and Description |
---|---|
protected IValidationError |
decorate(IValidationError error,
IValidatable<String> validatable)
Allows subclasses to decorate reported errors
|
CreditCardValidator.CreditCard |
determineCardId(String creditCardNumber)
Checks if the credit card number can be determined as a valid number.
|
CreditCardValidator.CreditCard |
getCardId() |
protected boolean |
isChecksumCorrect(String creditCardNumber)
Calculates the checksum of a credit card number using the Luhn algorithm (the so-called
"mod 10" algorithm).
|
protected boolean |
isLengthAndPrefixCorrect(String creditCardNumber)
Checks if the credit card number can be determined as a valid number.
|
protected CreditCardValidator.CreditCard |
isUnknown(String creditCardNumber)
Can be used (subclassed) to extend the test with a credit card not yet known by the
validator.
|
protected void |
setCardId(CreditCardValidator.CreditCard cardId)
Allow subclasses to set the card id
|
void |
validate(IValidatable<String> validatable)
Validates the
IValidatable instance. |
public CreditCardValidator()
public CreditCardValidator(boolean failOnUnkown)
failOnUnkown
- public final CreditCardValidator.CreditCard getCardId()
protected void setCardId(CreditCardValidator.CreditCard cardId)
cardId
- public void validate(IValidatable<String> validatable)
IValidator
IValidatable
instance. Validation errors should be reported using
the IValidatable.error(IValidationError)
method.validate
in interface IValidator<String>
validatable
- the IValidatable
instance being validatedprotected IValidationError decorate(IValidationError error, IValidatable<String> validatable)
error
- validatable
- protected boolean isLengthAndPrefixCorrect(String creditCardNumber)
creditCardNumber
- the credit card number as a stringTRUE
if the credit card number could be determined as a valid number,
else FALSE
is returnedpublic final CreditCardValidator.CreditCard determineCardId(String creditCardNumber)
creditCardNumber
- the credit card number as a stringTRUE
if the credit card number could be determined as a valid number,
else FALSE
is returnedprotected CreditCardValidator.CreditCard isUnknown(String creditCardNumber)
creditCardNumber
- the credit card number as a stringprotected final boolean isChecksumCorrect(String creditCardNumber)
creditCardNumber
- the credit card number for which the checksum should be calculatedTRUE
if the checksum for the given credit card number is valid, else
return FALSE
Copyright © 2006–2022 Apache Software Foundation. All rights reserved.