org.hibernate.validator.internal.constraintvalidators.hv.pl.PESELValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bean-validator Show documentation
Show all versions of bean-validator Show documentation
JSR 380's RI, Hibernate Validator version ${hibernate-validator.version} and its dependencies repackaged as OSGi bundle
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or .
*/
package org.hibernate.validator.internal.constraintvalidators.hv.pl;
import java.util.Collections;
import java.util.List;
import javax.validation.ConstraintValidator;
import org.hibernate.validator.constraints.pl.PESEL;
import org.hibernate.validator.internal.constraintvalidators.hv.ModCheckBase;
import org.hibernate.validator.internal.util.ModUtil;
/**
* Validator for {@link PESEL}.
*
* @author Marko Bekhta
*/
public class PESELValidator extends ModCheckBase implements ConstraintValidator {
private static final int[] WEIGHTS_PESEL = { 1, 3, 7, 9, 1, 3, 7, 9, 1, 3 };
@Override
public void initialize(PESEL constraintAnnotation) {
super.initialize(
0,
Integer.MAX_VALUE,
-1,
true
);
}
@Override
public boolean isCheckDigitValid(List digits, char checkDigit) {
Collections.reverse( digits );
int modResult = ModUtil.calculateModXCheckWithWeights( digits, 10, Integer.MAX_VALUE, WEIGHTS_PESEL );
switch ( modResult ) {
case 10:
return checkDigit == '0';
default:
return Character.isDigit( checkDigit ) && modResult == extractDigit( checkDigit );
}
}
}