Alternative to create a new method on a String object?

4

I have the following situation: I have a txt file of defined positions (type CSV, CNAB, etc.) to process and extract values. For purposes of understanding, follow the code I've made and it's working perfectly:

public class Extract {

    final int[] CPF = new int[]{0, 10};
    final int[] NOME = new int[]{15, 45};

    public static void main(String args[]) {

      FileInputStream fis = new FileInputStream("C:\cnab.txt");        
      String line;

      while ((line = fis.readLine()) != null) {
          System.out.println(getValue(line, CPF));
          System.out.println(getValue(line, NOME)); 
      }
    }

    private String getValue(String line, int[] slice) {
        return line.substring(slice[0], slice[1]);
    }
 }

So long ok, but I would like something more readable, elegant, such as:

System.out.println(line.getValue(CPF));

But we all know that String is end . Anyone have any suggestions?

    
asked by anonymous 17.03.2015 / 16:18

2 answers

8

Why you should not do this

Changing a fundamental language class (whatever it is) to implement a simple requirement like this is a big mistake.

The Java language purposely prevents for security reasons that the basic types like String and Integer are modified or extended with inheritance. These classes are declared with the final modifier.

One of the great perils of this is in relation to organization. Programmers would start making several language modifications, including libraries that would make other modifications, and the code would end up in a big mess.

Another problem is in performance. Large systems would end up with too much overhead of additional methods and subclasses where it simply is not needed.

Ever wondered if each programmer tried to modify the String class to meet their immediate needs?

Make OO right

Inheriting or changing existing classes is the best solution from an object-oriented design perspective for situations like this.

In this case, you can simply encapsulate the line in a new object containing the method you need.

Example:

public class LinhaArquivo {
    private String linha;
    public LinhaArquivo(String linha) { 
        this.linha = linha;
    }
    public String getValue(int[] slice) {
        return linha.substring(slice[0], slice[1]);
    }
}

Then you could change your code as follows:

public class Extract {

    final int[] CPF = new int[]{0, 10};
    final int[] NOME = new int[]{15, 45};

    public static void main(String args[]) {

      FileInputStream fis = new FileInputStream("C:\cnab.txt");        
      String line;

      while ((line = br.readLine()) != null) {
          LinhaArquivo linha = new LinhaArquivo(line);
          System.out.println(linha.getValue(CPF));
          System.out.println(linha.getValue(NOME)); 
      }
    }

}
    
17.03.2015 / 17:58
6

If the positions are static, create an enum

public enum ArchiveEnum {

NOME(0, 5),
CPF(6, 20);
private Integer minAge;
private Integer maxAge;

private ArchiveEnum(Integer minAge, Integer maxAge) {
    this.minAge = minAge;
    this.maxAge = maxAge;
}

public Integer getMinAge() {
    return minAge;
}

public void setMinAge(Integer minAge) {
    this.minAge = minAge;
}

public Integer getMaxAge() {
    return maxAge;
}

public void setMaxAge(Integer maxAge) {
    this.maxAge = maxAge;
}
}

And in your method, pass the ENUM type

public String getCampo(ArchiveEnum enumValue) {
    String archive = "TIAGO,111.111.111-11"; // Suposição de linha do arquivo
    return archive.subString(enumValue.getMinAge(), enumValue.getMaxAge()).toString();
}
    
17.03.2015 / 17:50