Skip to content

Commit

Permalink
Atualização da regra validacão do titulo de eleitor (#300)
Browse files Browse the repository at this point in the history
* Adicionando regra de substituição de DV 0 por 1 na validação do título
eleitoral

* Adequação do cômputo dos DVs do título de acordo com algoritmo do TSE
  • Loading branch information
tnas authored Oct 25, 2024
1 parent 7d4484b commit c985e19
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 37 deletions.
78 changes: 49 additions & 29 deletions stella-core/src/main/java/br/com/caelum/stella/type/Estado.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,170 +38,172 @@
*/
public enum Estado {

AC(12) {
AC(12, "24") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEAcreValidator(messageProducer, isFormatted);
}

},
AL(27) {
AL(27, "17") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEAlagoasValidator(messageProducer, isFormatted);
}

},
AP(16) {
AP(16, "25") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEAmapaValidator(messageProducer, isFormatted);
}

},
AM(13) {
AM(13, "22") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEAmazonasValidator(messageProducer, isFormatted);
}

},
BA(29) {
BA(29, "05") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEBahiaValidator(messageProducer, isFormatted);
}

},
CE(23) {
CE(23, "07") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IECearaValidator(messageProducer, isFormatted);
}

},
DF(53) {
DF(53, "20") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEDistritoFederalValidator(messageProducer, isFormatted);
}

},
ES(32) {
ES(32, "14") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEEspiritoSantoValidator(messageProducer, isFormatted);
}

},
GO(52) {
GO(52, "10") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEGoiasValidator(messageProducer, isFormatted);
}

},
MA(21) {
MA(21, "11") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEMaranhaoValidator(messageProducer, isFormatted);
}

},
MT(51) {
MT(51, "18") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEMatoGrossoValidator(messageProducer, isFormatted);
}

},
MS(50) {
MS(50, "19") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEMatoGrossoDoSulValidator(messageProducer, isFormatted);
}

},
MG(31) {
MG(31, "02") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEMinasGeraisValidator(messageProducer, isFormatted);
}

},
PA(15) {
PA(15, "13") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEParaValidator(messageProducer, isFormatted);
}

},
PB(25) {
PB(25, "12") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEParaibaValidator(messageProducer, isFormatted);
}

},
PR(41) {
PR(41, "06") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEParanaValidator(messageProducer, isFormatted);
}

},
PE(26) {
PE(26, "08") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEPernambucoValidator(messageProducer, isFormatted);
}

},
PI(22) {
PI(22, "15") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IEPiauiValidator(messageProducer, isFormatted);
}

},
RJ(33) {
RJ(33, "03") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IERioDeJaneiroValidator(messageProducer, isFormatted);
}

},
RN(24) {
RN(24, "16") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IERioGrandeDoNorteValidator(messageProducer, isFormatted);
}

},
RS(43) {
RS(43, "04") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IERioGrandeDoSulValidator(messageProducer, isFormatted);
}

},
RO(11) {
RO(11, "23") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IERondoniaValidator(messageProducer, isFormatted);
}

},
RR(14) {
RR(14, "26") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IERoraimaValidator(messageProducer, isFormatted);
}

},
SC(42) {
SC(42, "09") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IESantaCatarinaValidator(messageProducer, isFormatted);
}

},
SP(35) {
SP(35, "01") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IESaoPauloValidator(messageProducer, isFormatted);
}

},
SE(28) {
SE(28, "21") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IESergipeValidator(messageProducer, isFormatted);
}

},
TO(17) {
TO(17, "27") {
public Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted) {
return new IETocantinsValidator(messageProducer, isFormatted);
}

};

private final int codigoIBGE;

private final String codigoEleitoral;

/**
* Retorna um validador de Inscrição Estadual.
Expand All @@ -215,15 +217,20 @@ public Validator<String> getIEValidator(MessageProducer messageProducer, boolean
*/
public abstract Validator<String> getIEValidator(MessageProducer messageProducer, boolean isFormatted);

private Estado(int codigoIBGE) {
private Estado(int codigoIBGE, String codigoEleitoral) {
this.codigoIBGE = codigoIBGE;
this.codigoEleitoral = codigoEleitoral;
}

public int getCodigoIBGE() {
return codigoIBGE;
}

public String getCodigoEleitoral() {
return codigoEleitoral;
}

/**
/**
* A região do território brasileiro em que esse estado está localizado.
*
* @return Retorna a região em que esse estado está localizado.
Expand All @@ -249,4 +256,17 @@ public Regiao regiao() {
public boolean localizadoEm(Regiao regiao) {
return regiao() == regiao;
}

public static Estado deCodigoEleitoral(String codigo) {

Estado[] estados = Estado.values();

for (Estado estado : estados) {
if (estado.getCodigoEleitoral().equals(codigo)) {
return estado;
}
}

throw new IllegalStateException("Não foi possível determinar o estado a partir do código eleitoral " + codigo);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package br.com.caelum.stella.validation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;
Expand All @@ -11,6 +12,7 @@
import br.com.caelum.stella.SimpleMessageProducer;
import br.com.caelum.stella.ValidationMessage;
import br.com.caelum.stella.format.TituloEleitoralFormatter;
import br.com.caelum.stella.type.Estado;
import br.com.caelum.stella.validation.error.TituloEleitoralError;

/**
Expand Down Expand Up @@ -80,6 +82,8 @@ public class TituloEleitoralValidator implements Validator<String> {
public static final Pattern FORMATED = Pattern.compile("(\\d{10})/(\\d{2})");
public static final Pattern UNFORMATED = Pattern.compile("(\\d{10})(\\d{2})");

private static final List<Estado> estadosSubstitoresDigito = Arrays.asList(Estado.SP, Estado.MG);

private boolean isFormatted = false;
private MessageProducer messageProducer;

Expand Down Expand Up @@ -160,24 +164,46 @@ private List<ValidationMessage> getInvalidValues(String tituloDeEleitor) {
}
return errors;
}

private String calculaDigitos(String tituloSemDigito) {

int length = tituloSemDigito.length();

String sequencial = tituloSemDigito.substring(0,length - 2);
String digito1 = new DigitoPara(sequencial).complementarAoModulo().trocandoPorSeEncontrar("0",10,11).mod(11).calcula();
String codigoEstado = tituloSemDigito.substring(length - 2, length);
boolean ehEstadoSubstitutorDigito = estadosSubstitoresDigito.contains(Estado.deCodigoEleitoral(codigoEstado));

String digito1 = this.geraDigito(ehEstadoSubstitutorDigito, sequencial);
String digito2 = this.geraDigito(ehEstadoSubstitutorDigito, codigoEstado + digito1);

String codigoEstado = tituloSemDigito.substring(length - 2, length);
String digito2 = new DigitoPara(codigoEstado + digito1).complementarAoModulo().trocandoPorSeEncontrar("0",10,11).mod(11).calcula();

return digito1 + digito2;
}

private String geraDigito(boolean ehEstadoSubstitutorDigito, String base) {

String digito = new DigitoPara(base).mod(11).calcula();

if (ehEstadoSubstitutorDigito) {
if (digito.equals("1")) {
digito = "0";
} else if (digito.equals("0")) {
digito = "1";
} else {
digito = new DigitoPara(base).complementarAoModulo().mod(11).calcula();
}
} else {
digito = digito.equals("1") || digito.equals("0") ? "0" :
new DigitoPara(base).complementarAoModulo().mod(11).calcula();
}

return digito;
}

private boolean hasCodigoDeEstadoInvalido(String tituloDeEleitor) {
int codigo= Integer.parseInt(tituloDeEleitor.substring(tituloDeEleitor.length() - 4, tituloDeEleitor.length() - 2));
return !(codigo >= 01 && codigo <= 28);
}

public boolean isEligible(String value) {
boolean result;
if (isFormatted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class TituloEleitoralValidatorTest {

private final String[] validStrings = { "543275360116","142501480248", "557833330370", "013785610434",
"253346440540", "033734180663", "585353130710", "884328631058", "553505611201", "028565701333",
"245770031481", "713782341503", "403374181694", "452083221724" };
"245770031481", "713782341503", "403374181694", "452083221724", "162749070141" };

private final String[] validStringsFormatted = { "5432753601/16","1425014802/48", "5578333303/70", "0137856104/34",
"2533464405/40", "0337341806/63", "5853531307/10", "8843286310/58", "5535056112/01", "0285657013/33",
"2457700314/81", "7137823415/03", "4033741816/94", "4520832217/24"};
"2457700314/81", "7137823415/03", "4033741816/94", "4520832217/24", "2800440202/05"};

private final String[] invalidFirstDigitStrings = { "543275360106", "452083221714", "253346440520", "553505611231",
"884328631048" };

Expand Down

0 comments on commit c985e19

Please sign in to comment.