I have a CSV file which looks like this:
ID | Name | Date "1","Foo1","2013-08-03 21:00:08" "1","Foo2","2013-08-03 21:00:08" "2","Foo3","2013-08-03 21:00:08" ...
I need to write a method which returns a list of the lines with same ID and date, for reading the CSV file I am using OpenCSV. My question is whether or not this method can be rewritten in more elegant way.
private String[] currentWorkerLine;
private String relationIdentifier;
private String currentStartDate;
private int rowCount;
private List<String[]> getWorkers(CSVReader reader) throws Exception{
List<String[]> workers = new ArrayList<>();
if (currentWorkerLine == null && rowCount == 0){
currentWorkerLine = reader.readNext();
relationIdentifier = currentWorkerLine[0];
currentStartDate = currentWorkerLine[2];
workers.add(currentWorkerLine);
rowCount++;
}
while ((currentWorkerline = reader.readNext()) != null) {
if (!isRelatedLine(currentWorkerline[0]) || !isCurrentDateLine(currentWorkerline[2])){
lastLine = currentWorkerline;
return workers;
}
relationIdentifier = currentWorkerline[0];
currentStartDate = currentWorkerline[2];
workers.add(currentWorkerline);
lastLine = currentWorkerline;
rowCount++;
}
if (currentWorkerline == null && lastLine != null) {
workers.add(lastLine);
rowCount++;
lastLine = null;
}
return workers;
}
private boolean isRelatedLine(String rel) {
return rel.equals(relationIdentifier);
}
private boolean isCurrentDateLine(String date) {
return date.equals(currentStartDate);
}