All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.validator.internal.constraintvalidators.hv.pl.PolishNumberValidator Maven / Gradle / Ivy

There is a newer version: 8.0.1.Final
Show newest version
/*
 * 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.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;
import javax.validation.ConstraintValidator;

import org.hibernate.validator.internal.constraintvalidators.hv.ModCheckBase;
import org.hibernate.validator.internal.util.ModUtil;

/**
 * A base class validator for different Polish identification numbers. They differs in the lengths and weights used to calculate the mod sum.
 * In order to implement one you need to provide a method that gives an array of weights
 * and {@link ConstraintValidator#initialize(Annotation)} methods.
 *
 * @author Marko Bekhta
 */
public abstract class PolishNumberValidator extends ModCheckBase implements ConstraintValidator {

	@Override
	public boolean isCheckDigitValid(List digits, char checkDigit) {
		Collections.reverse( digits );

		// as we need sum % 11 rather than 11 - (sum % 11) returned by Mod11 algorithm:
		int modResult = 11 - ModUtil.calculateModXCheckWithWeights( digits, 11, Integer.MAX_VALUE, getWeights( digits ) );
		switch ( modResult ) {
			case 10:
			case 11:
				return checkDigit == '0';
			default:
				return Character.isDigit( checkDigit ) && modResult == extractDigit( checkDigit );
		}
	}

	protected abstract int[] getWeights(List digits);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy