CPF Validation on Android - Error in ArrayList

2

I have this class to validate CPF field in android, however it is giving error in line: ArrayList<Separador> posicaoSeparadoresTexto(String mascara)

Here is the complete code:

package com.mcavalini.lib;

import java.util.ArrayList;

public class Utils {

    public static int tamanhoMaximo(String mascara) {
        String novaString = removeCaracteresEspeciais(mascara);
        return novaString.length();
    }

    public static String removeCaracteresEspeciais(String str) {

        String[] caracteresEspeciais = {"\.", ",", "-", ":", "\(", "\)", "ª", "\|", "\\", "°", "\/"};

        for (int i = 0; i < caracteresEspeciais.length; i++) {
            str = str.replaceAll(caracteresEspeciais[i], "");
        }

        return str;
    }

    public static ArrayList<Separador> posicaoSeparadoresTexto(String mascara) {
        ArrayList<Separador> listaSeparadores = new ArrayList<Separador>();

        for (int i = 0; i < mascara.length(); i++) {
            if ( (mascara.substring(i, i+1).equals(new String(","))) ||
                 (mascara.substring(i, i+1).equals(new String("."))) ||
                 (mascara.substring(i, i+1).equals(new String("-"))) ||
                 (mascara.substring(i, i+1).equals(new String("/"))) ||
                 (mascara.substring(i, i+1).equals(new String("("))) ||
                 (mascara.substring(i, i+1).equals(new String(")")))) {
                listaSeparadores.add(new Separador(new Integer(i), mascara.substring(i, i+1)));
            }
        }

        return listaSeparadores;
    }

    public static String padString(String numero, int tamanhoMaximo, int tamanhoMascara, ArrayList<Separador> listaSeparadores) {
        String stringMascarada = "";
        boolean isPad = true;
        int indicePadString = 0;
        int indiceNumero = 0;
        int tamanhoNumero = numero.length();

        while (isPad) {
            for (Separador separador : listaSeparadores) {
                if (separador.getPosicao().intValue() == indicePadString) {
                    stringMascarada += separador.getCaracter();
                    indicePadString += 1;
                    break;
                }
            }

            if ((indiceNumero < tamanhoMaximo) && (indiceNumero < tamanhoNumero) ) {
                stringMascarada += numero.substring(indiceNumero, indiceNumero+1);
            } 

            indicePadString += 1;
            indiceNumero += 1;

            if (indiceNumero >= tamanhoNumero)
                isPad = false;
        }

        return stringMascarada;
    }
}
package com.mcavalini.lib;

import android.text.InputType;
import android.text.method.NumberKeyListener;

public class KeyListenerNumber extends NumberKeyListener {

    public int getInputType() {
        return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
    }

    @Override
    protected char[] getAcceptedChars() {
        return new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    }

}
package com.mcavalini.core;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;

import com.mcavalini.lib.KeyListenerNumber;
import com.mcavalini.lib.Utils;

public class MaskEditText extends EditText {
    private String mascara;
    private int tamanhoMascara;
    private int tamanhoMaximo;

    private boolean isUpdating;

    private KeyListenerNumber keyListenerNumber;

    public static String CPF_MASK = "   .   .   -  ";
    public static String CEP_MASK = "     -   ";
    public static String TELEFONE_8DIGITOS_MASK = "(  )    -    ";

    public MaskEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }   

    public int getTamanhoMascara() {
        return tamanhoMascara;
    }

    public void init(String mascara) {
        this.mascara = mascara;
        this.tamanhoMascara = mascara.length();
        this.tamanhoMaximo = Utils.tamanhoMaximo(mascara);

        keyListenerNumber = new KeyListenerNumber();
        this.setKeyListener(keyListenerNumber);
        this.setText("");

        this.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            public void afterTextChanged(Editable s) {
                String stringAtual = s.toString();

                if (isUpdating) {
                    isUpdating = false;
                    return;
                }

                String numero = stringAtual.replaceAll("[^0-9]*", "");

                if (numero.length() > tamanhoMaximo)
                    numero = numero.substring(0, tamanhoMaximo);

                String campo = Utils.padString(numero, tamanhoMaximo, tamanhoMascara, Utils.posicaoSeparadoresTexto(MaskEditText.this.mascara));

                isUpdating = true;

                MaskEditText.this.setText(campo);
                MaskEditText.this.setSelection(campo.length());
            }
        });
    }
}
    <com.mcavalini.core.MaskEditText
        android:id="@+id/txtCpf"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <requestFocus />   
    </com.mcavalini.core.MaskEditText>
    
asked by anonymous 27.02.2015 / 14:02

1 answer

0

As already stated in the comments, the Separador class is missing. Let's create this class:

public class Separador {
    private final Integer posicao;
    private final String caracter;

    public Separador(Integer posicao, String caracter) {
        this.posicao = posicao;
        this.caracter = caracter;
    }

    public Integer getPosicao() {
        return posicao;
    }

    public String getCaracter() {
        return caracter;
    }
}

And with that, your code should compile and work.

However, the code still has several other problems:

  • It does not use autoboxing and autounboxing properly, using new Integer(i) instead of just i and then using getPosicao().intValue() rather than simply getPosicao() .

  • The Separador class I put above was made to be compatible with the code, but could be better if posicao was int and not Integer and caracter was char and not% String , but for this you would have to change the Utils class.

  • Using new String(",") is unnecessary, just "," .

  • The presence of a class called ArrayList<Separador> full of static methods appears to be some deeper anti-pattern.

  • There must be other problems as well.

27.02.2015 / 19:54