|
| 1 | +package br.com.caelum.stella.validation; |
| 2 | + |
| 3 | +import br.com.caelum.stella.DigitoGenerator; |
| 4 | +import br.com.caelum.stella.MessageProducer; |
| 5 | +import br.com.caelum.stella.SimpleMessageProducer; |
| 6 | +import br.com.caelum.stella.ValidationMessage; |
| 7 | +import br.com.caelum.stella.validation.error.AgenciaBancariaError; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.regex.Matcher; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +/** |
| 15 | + * Representa um validador de agencia bancária. |
| 16 | + * |
| 17 | + * @author Thiago Nascimento |
| 18 | + */ |
| 19 | +public class AgenciaBancariaValidator implements Validator<String> { |
| 20 | + |
| 21 | + public static final Pattern COM_DV = Pattern.compile("(\\d+)\\-([\\dX])"); |
| 22 | + public static final Pattern SEM_DV = Pattern.compile("\\d+"); |
| 23 | + |
| 24 | + private boolean isComDigito = true; |
| 25 | + private MessageProducer messageProducer; |
| 26 | + |
| 27 | + public AgenciaBancariaValidator() { |
| 28 | + this.messageProducer = new SimpleMessageProducer(); |
| 29 | + } |
| 30 | + |
| 31 | + public AgenciaBancariaValidator(boolean isComDigito) { |
| 32 | + this(); |
| 33 | + this.isComDigito = isComDigito; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void assertValid(String agencia) { |
| 38 | + |
| 39 | + List<ValidationMessage> errors = this.invalidMessagesFor(agencia); |
| 40 | + |
| 41 | + if (!errors.isEmpty()) { |
| 42 | + throw new InvalidStateException(errors); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public List<ValidationMessage> invalidMessagesFor(String agencia) { |
| 48 | + |
| 49 | + List<ValidationMessage> errors = new ArrayList<ValidationMessage>(); |
| 50 | + |
| 51 | + if (this.isEligible(agencia)) { |
| 52 | + |
| 53 | + if (this.isComDigito) { |
| 54 | + |
| 55 | + Matcher matcher = COM_DV.matcher(agencia); |
| 56 | + |
| 57 | + if (!matcher.find()) { |
| 58 | + throw new InvalidStateException(this.messageProducer.getMessage(AgenciaBancariaError.INVALID_FORMAT)); |
| 59 | + } |
| 60 | + |
| 61 | + String dvInformado = matcher.group(2); |
| 62 | + String dvComputado = this.computarDigitoVerificador(matcher.group(1)); |
| 63 | + |
| 64 | + if (!dvInformado.equals(dvComputado)) { |
| 65 | + errors.add(this.messageProducer.getMessage(AgenciaBancariaError.INVALID_CHECK_DIGIT)); |
| 66 | + } |
| 67 | + |
| 68 | + } else { |
| 69 | + errors.add(this.messageProducer.getMessage(AgenciaBancariaError.CHECK_DIGIT_NOT_FOUND)); |
| 70 | + } |
| 71 | + |
| 72 | + } else { |
| 73 | + errors.add(this.messageProducer.getMessage(AgenciaBancariaError.INVALID_FORMAT)); |
| 74 | + } |
| 75 | + |
| 76 | + return errors; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean isEligible(String value) { |
| 81 | + |
| 82 | + if (value == null || value.trim().isEmpty()) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + return this.isComDigito ? |
| 87 | + COM_DV.matcher(value).matches() : SEM_DV.matcher(value).matches(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String generateRandomValid() { |
| 92 | + final String agenciaSemDigitos = new DigitoGenerator().generate(4); |
| 93 | + return String.format("%s-%s", agenciaSemDigitos, this.computarDigitoVerificador(agenciaSemDigitos)); |
| 94 | + } |
| 95 | + |
| 96 | + public String computarDigitoVerificador(String agenciaSemDV) { |
| 97 | + |
| 98 | + String[] algarisms = agenciaSemDV.split(""); |
| 99 | + int multiplier = 9; |
| 100 | + int sum = 0; |
| 101 | + |
| 102 | + for (int index = algarisms.length - 1; index >= 0; --index) { |
| 103 | + sum += Integer.valueOf(algarisms[index]) * multiplier--; |
| 104 | + } |
| 105 | + |
| 106 | + int rest = sum % 11; |
| 107 | + return rest == 10 ? "X" : String.valueOf(rest); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments