|
|
package ru.pfr.chel;
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
public class Snils {
|
|
|
|
|
|
/**
|
|
|
*Возвращент СНИЛС если введенный параметр проходит проверку на корректность СНИЛС
|
|
|
* @param pSnils Строка для проверки
|
|
|
* @return Возвращент СНИЛС
|
|
|
* @throws SnilsException если введенный параметр не проходит проверку на корректность СНИЛС
|
|
|
*/
|
|
|
public static String GetSnils(String pSnils) throws SnilsException {
|
|
|
if((pSnils.length()!=0)&&(pSnils != null) && (ChekFormatLongSnils(pSnils))){
|
|
|
return pSnils;
|
|
|
}else{
|
|
|
throw new SnilsException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static boolean ChekFormatLongSnils(String pSnils) throws SnilsException {
|
|
|
boolean _result = false;
|
|
|
pSnils = pSnils.replace("-", "").replace(" ", "");
|
|
|
if(pSnils.length()!=11){
|
|
|
throw new SnilsException("Invalid snils length");
|
|
|
}
|
|
|
if(isDigitSnils(pSnils)
|
|
|
&& isChekSummSnils(pSnils)
|
|
|
&& isCheckSnils(pSnils)){
|
|
|
_result =true;
|
|
|
}
|
|
|
return _result;
|
|
|
}
|
|
|
|
|
|
//проверка на контрольную сумму
|
|
|
private static boolean isChekSummSnils(String pSnils) throws SnilsException {
|
|
|
boolean _result = true;
|
|
|
int _controlSumm = 0;
|
|
|
String _sSnils = pSnils.substring(0,9);
|
|
|
String _sControlSumSnils = pSnils.substring(9,11);
|
|
|
_controlSumm = getControlSumm(pSnils);
|
|
|
|
|
|
if(_controlSumm == Integer.parseInt(_sControlSumSnils)){
|
|
|
_result = true;
|
|
|
}
|
|
|
return _result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* вычисление контрольной суммы
|
|
|
* @param pSnils СНИЛС в формате ХХХХХХХХХХХ
|
|
|
* @return возвращает контрольное число
|
|
|
*/
|
|
|
private static int getControlSumm(String pSnils) {
|
|
|
int _controlSumm = 0;
|
|
|
int _Summ = 0;
|
|
|
int _pos = 1;
|
|
|
for(int i = 8; i >= 0; i--){
|
|
|
String _Digit = pSnils.substring(i,i+1);
|
|
|
int _iSnils = Integer.parseInt(_Digit);
|
|
|
_Summ = _Summ + _iSnils * _pos;
|
|
|
_pos++;
|
|
|
}
|
|
|
if(_Summ < 100){
|
|
|
_controlSumm = _Summ;
|
|
|
}
|
|
|
if((_Summ == 100) || (_Summ == 101)){
|
|
|
_controlSumm = 0;
|
|
|
}
|
|
|
if(_Summ > 101){
|
|
|
_controlSumm = _Summ % 101;
|
|
|
}
|
|
|
return _controlSumm;
|
|
|
}
|
|
|
|
|
|
private static boolean isCheckSnils(String pSnils) throws SnilsException {
|
|
|
if(pSnils.length()!=11){
|
|
|
throw new SnilsException("Length Snils != 11");
|
|
|
}
|
|
|
boolean _result = true;
|
|
|
char[] _chrSnils = pSnils.toCharArray();
|
|
|
char _oldChar = ' ';
|
|
|
int _coincidences = 0;
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
if (_oldChar == _chrSnils[i]) {
|
|
|
_coincidences++;
|
|
|
} else {
|
|
|
_coincidences = 0;
|
|
|
}
|
|
|
if (_coincidences == 2) {
|
|
|
throw new SnilsException("3 identical numbers in a row");
|
|
|
}
|
|
|
_oldChar = _chrSnils[i];
|
|
|
};
|
|
|
return _result;
|
|
|
}
|
|
|
|
|
|
private static boolean isDigitSnils(String pSnils) throws SnilsException {
|
|
|
boolean _result = false;
|
|
|
try {
|
|
|
BigInteger _biSnils = new BigInteger(pSnils);
|
|
|
BigInteger MINSNILS = BigInteger.valueOf(100199800);
|
|
|
if (_biSnils.compareTo(MINSNILS) == 1){
|
|
|
_result = true;
|
|
|
}
|
|
|
}catch (NumberFormatException ex){
|
|
|
throw new SnilsException("Invalid characters in snils");
|
|
|
}
|
|
|
return _result;
|
|
|
}
|
|
|
|
|
|
public static String GetSnils(BigInteger pSnils) throws SnilsException {
|
|
|
String _Snils = pSnils.toString();
|
|
|
int _lengthSnils = _Snils.length();
|
|
|
if(_lengthSnils == 9){
|
|
|
_Snils = "00"+_Snils;
|
|
|
}
|
|
|
if(_lengthSnils == 10){
|
|
|
_Snils = "0"+_Snils;
|
|
|
}
|
|
|
|
|
|
if((_Snils.length()!=0)&&(pSnils != null) && (ChekFormatLongSnils(_Snils))){
|
|
|
return _Snils;
|
|
|
}else{
|
|
|
throw new SnilsException();
|
|
|
}
|
|
|
}
|
|
|
}
|