it.cnr.contab.anagraf00.util.CodiceFiscaleControllo Maven / Gradle / Ivy
/*
* Copyright (C) 2019 Consiglio Nazionale delle Ricerche
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package it.cnr.contab.anagraf00.util;
import java.util.*;
import it.cnr.jada.util.*;
/**
* Classe per il controllo dell'esattezza del codice Fiscale
* Gestisce il controllo e il calcolo del codice fiscale sulla base di dizionari statici interni
*/
public class CodiceFiscaleControllo {
static String CODICE_NON_VALIDO = "Codice fiscale non valido: ";
static String[] Vocali = {"A","E", "I", "U", "O"};
static String[] Consonanti = {"B","C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "V", "W", "X", "Y", "Z"};
static String[] Numeri = {"0", "1","2", "3", "4", "5", "6", "7", "8", "9"};
static String[] Alfanum = {"A","B","C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1","2", "3", "4", "5", "6", "7", "8", "9"};
static String[] CodificaMesi = {"A","B", "C", "D", "E", "H","L", "M", "P", "R", "S", "T"};
static String[] MapCC = {"A","B","C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
static Hashtable MapVocaliAccentate;
static Hashtable MapCCDispari, MapCCPari;
static {
MapVocaliAccentate = new Hashtable();
MapVocaliAccentate.put("à".toUpperCase(),"A");
MapVocaliAccentate.put("á".toUpperCase(),"A");
MapVocaliAccentate.put("è".toUpperCase(),"E");
MapVocaliAccentate.put("é".toUpperCase(),"E");
MapVocaliAccentate.put("ò".toUpperCase(),"O");
MapVocaliAccentate.put("ó".toUpperCase(),"O");
MapVocaliAccentate.put("ù".toUpperCase(),"U");
MapVocaliAccentate.put("ú".toUpperCase(),"U");
MapVocaliAccentate.put("ì".toUpperCase(),"I");
MapVocaliAccentate.put("í".toUpperCase(),"I");
MapCCDispari = new Hashtable();
MapCCDispari.put("A", new Integer(1));
MapCCDispari.put("0", new Integer(1));
MapCCDispari.put("B", new Integer(0));
MapCCDispari.put("1", new Integer(0));
MapCCDispari.put("C", new Integer(5));
MapCCDispari.put("2", new Integer(5));
MapCCDispari.put("D", new Integer(7));
MapCCDispari.put("3", new Integer(7));
MapCCDispari.put("E", new Integer(9));
MapCCDispari.put("4", new Integer(9));
MapCCDispari.put("F", new Integer(13));
MapCCDispari.put("5", new Integer(13));
MapCCDispari.put("G", new Integer(15));
MapCCDispari.put("6", new Integer(15));
MapCCDispari.put("H", new Integer(17));
MapCCDispari.put("7", new Integer(17));
MapCCDispari.put("I", new Integer(19));
MapCCDispari.put("8", new Integer(19));
MapCCDispari.put("J", new Integer(21));
MapCCDispari.put("9", new Integer(21));
MapCCDispari.put("K", new Integer(2));
MapCCDispari.put("L", new Integer(4));
MapCCDispari.put("M", new Integer(18));
MapCCDispari.put("N", new Integer(20));
MapCCDispari.put("O", new Integer(11));
MapCCDispari.put("P", new Integer(3));
MapCCDispari.put("Q", new Integer(6));
MapCCDispari.put("R", new Integer(8));
MapCCDispari.put("S", new Integer(12));
MapCCDispari.put("T", new Integer(14));
MapCCDispari.put("U", new Integer(16));
MapCCDispari.put("V", new Integer(10));
MapCCDispari.put("W", new Integer(22));
MapCCDispari.put("X", new Integer(25));
MapCCDispari.put("Y", new Integer(24));
MapCCDispari.put("Z", new Integer(23));
MapCCPari = new Hashtable();
MapCCPari.put("A", new Integer(0));
MapCCPari.put("0", new Integer(0));
MapCCPari.put("B", new Integer(1));
MapCCPari.put("1", new Integer(1));
MapCCPari.put("C", new Integer(2));
MapCCPari.put("2", new Integer(2));
MapCCPari.put("D", new Integer(3));
MapCCPari.put("3", new Integer(3));
MapCCPari.put("E", new Integer(4));
MapCCPari.put("4", new Integer(4));
MapCCPari.put("F", new Integer(5));
MapCCPari.put("5", new Integer(5));
MapCCPari.put("G", new Integer(6));
MapCCPari.put("6", new Integer(6));
MapCCPari.put("H", new Integer(7));
MapCCPari.put("7", new Integer(7));
MapCCPari.put("I", new Integer(8));
MapCCPari.put("8", new Integer(8));
MapCCPari.put("J", new Integer(9));
MapCCPari.put("9", new Integer(9));
MapCCPari.put("K", new Integer(10));
MapCCPari.put("L", new Integer(11));
MapCCPari.put("M", new Integer(12));
MapCCPari.put("N", new Integer(13));
MapCCPari.put("O", new Integer(14));
MapCCPari.put("P", new Integer(15));
MapCCPari.put("Q", new Integer(16));
MapCCPari.put("R", new Integer(17));
MapCCPari.put("S", new Integer(18));
MapCCPari.put("T", new Integer(19));
MapCCPari.put("U", new Integer(20));
MapCCPari.put("V", new Integer(21));
MapCCPari.put("W", new Integer(22));
MapCCPari.put("X", new Integer(23));
MapCCPari.put("Y", new Integer(24));
MapCCPari.put("Z", new Integer(25));
};
/**
* Calcola la parte di CF corrispondente al cognome
* @param aS cognome
*/
public static String calcolaCodCognome(String aS) {
String aCodCognomeCalcolato="";
String aSVocali = vocali(aS);
String aSConsonanti = consonanti(aS);
if(aSConsonanti.length() >= 3)
aCodCognomeCalcolato = aSConsonanti.substring(0,3);
else if(aS.length() >= 3)
aCodCognomeCalcolato = aSConsonanti + aSVocali.substring(0, (3 - aSConsonanti.length()) );
if(aS.length() == 2) {
aCodCognomeCalcolato = aSConsonanti + aSVocali + "X";
}
if(aS.length() == 1) {
aCodCognomeCalcolato = aSConsonanti + aSVocali + "XX";
}
return aCodCognomeCalcolato;
}
/**
* Calcola la parte di CF corrispondente al nome
* @param aS nome
*/
public static String calcolaCodNome(String aS) {
String aCodNomeCalcolato="";
String aSVocali = vocali(aS);
String aSConsonanti = consonanti(aS);
if(aSConsonanti.length() >= 4)
aSConsonanti = aSConsonanti.substring(0,1) + aSConsonanti.substring(2,4);
if(aSConsonanti.length() >= 3)
aCodNomeCalcolato = aSConsonanti.substring(0,3);
else if(aS.length() >= 3)
aCodNomeCalcolato = aSConsonanti + aSVocali.substring(0, (3 - aSConsonanti.length()));
if(aS.length() == 2) {
aCodNomeCalcolato = aSConsonanti + aSVocali + "X";
}
if(aS.length() == 1) {
aCodNomeCalcolato = aSConsonanti + aSVocali + "XX";
}
return aCodNomeCalcolato;
}
/**
* Controllo correttezza del carattere di controllo terminale del CF
* @param codice CF completo da verificare
*/
public static boolean checkCC(String codice){
// Il codice passato per il controllo ha lunghezza n-1 dove n è la lunghezza del codice fiscale
return getCC(codice.substring(0,codice.length() - 1)).equals(codice.substring(15,16));
}
/**
* Ritorna una stringa composta dalle consonanti di aS
* @param aS Stringa da cui estrarre le consonanti
*/
private static String consonanti(String aS){
String aOut="";
for(int iC=0;iC © 2015 - 2025 Weber Informatics LLC | Privacy Policy