package com.es.lib.common.validation.ogrn;

import com.es.lib.common.validation.BadLengthException;
import com.es.lib.common.validation.BadValueException;
import com.es.lib.common.validation.ValidationUtil;

/**
 * @author Zuzoev Dmitry - zuzoev.d@ext-system.com
 * @since 25.07.16
 */
public class OGRNValidatorUtil {

    private OGRNValidatorUtil() {
    }

    /**
     * General validate OGRN with 13 and 15 length
     *
     * @param value string with OGRN
     * @throws BadValueException  when invalid length
     * @throws BadLengthException when invalid value
     */
    public static void validate(String value) throws BadLengthException, BadValueException {
        if (value == null) {
            throw new BadLengthException();
        }
        int length = value.length();
        if (length != 13 && length != 15) {
            throw new BadLengthException();
        }
        try{
            Long.parseLong(value);
        }catch (NumberFormatException e){
            throw new BadValueException();
        }
        if (length == 13) {
            validate13(value, false);
        } else {
            validate15(value, false);
        }
    }

    /**
     * Validate OGRN with length equal 13
     *
     * @param value string with OGRN
     * @throws BadValueException  when invalid length
     * @throws BadLengthException when invalid value
     */
    public static void validate13(String value) throws BadLengthException, BadValueException {
        validate13(value, true);
    }

    /**
     * Validate OGRN with length equal 15
     *
     * @param value string with OGRN
     * @throws BadValueException  when invalid length
     * @throws BadLengthException when invalid value
     */
    public static void validate15(String value) throws BadLengthException, BadValueException {
        validate15(value, true);
    }

    /**
     * Validate OGRN with length equal 13
     *
     * @param value    string with OGRN
     * @param preCheck Execute common check
     * @throws BadValueException  when invalid length
     * @throws BadLengthException when invalid value
     */
    private static void validate13(String value, boolean preCheck) throws BadLengthException, BadValueException {
        if (preCheck) {
            if (value == null) {
                throw new BadLengthException();
            }
            int length = value.length();
            if (length != 13) {
                throw new BadLengthException();
            }
            try{
                Long.parseLong(value);
            }catch (NumberFormatException e){
                throw new BadValueException();
            }
        }
        long num12 = (long) Math.floor((Long.valueOf(value) / 10) % 11);
        long dgt13 = num12 == 10 ? 0 : num12;
        if (ValidationUtil.getInt(value, 12) != dgt13) {
            throw new BadValueException();
        }
    }

    /**
     * Validate OGRN with length equal 15
     *
     * @param value    string with OGRN
     * @param preCheck Execute common check
     * @throws BadValueException  when invalid length
     * @throws BadLengthException when invalid value
     */
    private static void validate15(String value, boolean preCheck) throws BadLengthException, BadValueException {
        if (preCheck) {
            if (value == null) {
                throw new BadLengthException();
            }
            int length = value.length();
            if (length != 15) {
                throw new BadLengthException();
            }
            if (value.matches("\\D")) {
                throw new BadValueException();
            }
        }
        long num14 = (long) Math.floor((Long.valueOf(value) / 10) % 13);
        long dgt15 = num14 % 10;
        if (ValidationUtil.getInt(value, 14) != dgt15) {
            throw new BadValueException();
        }
    }
}

@@ -886,34 +886,35 @@
    %7D%0A        try
+ 
 %7B%0A
-
             Long
@@ -933,32 +933,33 @@
 alue);%0A        %7D
+ 
 catch (NumberFor
@@ -965,32 +965,33 @@
 rmatException e)
+ 
 %7B%0A            th
@@ -2494,16 +2494,17 @@
      try
+ 
 %7B%0A      
@@ -2549,16 +2549,17 @@
        %7D
+ 
 catch (N
@@ -2581,16 +2581,17 @@
 ption e)
+ 
 %7B%0A      
@@ -2647,32 +2647,50 @@
     %7D%0A        %7D%0A
+        try %7B%0A    
         long num
@@ -2746,32 +2746,36 @@
  %25 11);%0A        
+    
 long dgt13 = num
@@ -2796,32 +2796,36 @@
  num12;%0A        
+   
+ 
 if (ValidationUt
@@ -2853,24 +2853,129 @@
 != dgt13) %7B%0A
+                throw new BadValueException();%0A            %7D%0A        %7D catch (NumberFormatException e) %7B%0A
             
@@ -3749,32 +3749,50 @@
     %7D%0A        %7D%0A
+        try %7B%0A    
         long num
@@ -3848,32 +3848,36 @@
  %25 13);%0A        
+    
 long dgt15 = num
@@ -3885,32 +3885,36 @@
 4 %25 10;%0A        
+   
+ 
 if (ValidationUt
@@ -3942,24 +3942,129 @@
 != dgt15) %7B%0A
+                throw new BadValueException();%0A            %7D%0A        %7D catch (NumberFormatException e) %7B%0A
             

Add OGRN 15 and 13 additional exception check