Search code examples
javacsvapache-commons-csv

why are classes declared as final in apache commons csv library


Apache commons CSV Parser class throws an exception

"EOF reached before encapsulated token finished"

for the pseudo code

CSVParser csvParser = CSVParser.parse(fileChunkString, CSVFormat.DEFAULT);

The reason for the error is my CSV File is parsed in chunks i.e. I am not passing the entire file content at once, but, a chunk of it, and the error makes sense as the closing tokens are not guaranteed to be encountered

String for an example can be a1,b1,c1,d1,e1,f1\na,b,"csdsds, as you see the closing quotes are missing here in the 2nd row.

I want to modify the library source code to memorise the last row and merge it with the next chunk of file content e.g.: next chunk could be dfdfd",a,c so my content becomes a,b,"csdsds,dfdfd",a,c

Going through the source code the first step I see is that I need to extend Lexer.java and override the method as this class is throwing the above EOF exception, and also extend CSVParser.java class

But, the problem, is these classes are declared as final, and I can't extend them. I want to understand why are these classes declared as final?


Solution

  • But, the problem, is these classes are declared as final, and I can't extend them. I want to understand why are these classes declared as final?

    The main reason classes are final or fields are restricted visibility is that this way Apache can make internal changes to the classes without worrying about users extending or accessing those internal parts. As soon as a class is not final you have to consider all protected fields as exposed API.

    In your case I would bring up the problem on commons-developer mailing list and see what can be done about it. Commons CSV is under rapid development at the moment.