package de.ivu.elect.business.kandidatur.boundary;

public class NlOrdnungsmerkmalPruefer {


  /*
  * Vorlage: de.ivu.wahl.wus.foundation.ui.validater.NationalIdentificationNumberValidator (L5.backup)
  * */

  public boolean isValid(final String nationalIdentificationNumber) {
    return nationalIdentificationNumber != null &&
            nationalIdentificationNumber.matches("(\\d{9})?") &&
            validateChecksum(nationalIdentificationNumber);
  }

  private boolean validateChecksum(final String nationalIdentificationNumber) {
    char[] charArray = nationalIdentificationNumber.toCharArray();
    long checksum = 0;
    long multiplier = -1;
    for (int i = charArray.length - 1; i >= 0; i--) {
      checksum += multiplier * Long.parseLong(String.valueOf(charArray[i]));
      if (multiplier == -1) {
        multiplier = 2;
      } else {
        multiplier++;
      }
    }
    return checksum % 11 == 0;
  }

}
