javaopencsv

OpenCsv .parse() seems to have parsed correctly but returns empty List


When I debug the following code, in the last step .parse() I can see my expected value, the arrayList has my expected entry:

the ArrayList has one entry

but then in the next step, the returned List is empty:

enter image description here

What I expect is that the List is getting filled with my MyWindloadDTOs that I see in the .parse() step. How can it get lost in the return statement?

    final String responseString = "Result 1,Result 2,Zone,Latitude,Longitude,Elevation,Street,ZIP,City,Standard,Annex,Note(s),Legal notice
22.0 m/s,0.30 kN/m<sup>2</sup>,22,59.9257848833493,10.7516773524414,27.54182624816895,Maridalsveien 33E,0175,Oslo,EN 1991-1-4,NS-EN 1991-1-4,Bezogen auf den Ort Oslo<br>Höhenfaktor c<sub>alt</sub> nach NA.4.2(2)P(901.1),Alle Angaben ohne Gewähr"
             
                final CSVReader reader = new CSVReader(new StringReader(responseString));
                final List<MyWindLoadDTO> windLoadDTOs = new CsvToBeanBuilder<MyWindLoadDTO>(reader)
                        .withType(MyWindLoadDTO.class)
                        .build()
                        .parse();

with this MyWindLoadDTO.class:

import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.CsvCustomBindByName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;


@AllArgsConstructor
@NoArgsConstructor
@Data
@EqualsAndHashCode
public class MyWindLoadDTO {

    @CsvCustomBindByName(column = "Result 1", converter = MyCsvStringToDoubleConverter.class)
    private Double vb;
    @CsvCustomBindByName(column = "Result 2", converter = MyCsvStringToDoubleConverter.class)
    private Double qb;
    @CsvBindByName(column = "Zone")
    private String windZone;
    @CsvBindByName(column = "Latitude")
    private double latitude;
    @CsvBindByName(column = "Longitude")
    private double longitude;
    @CsvBindByName(column = "Elevation")
    private double elevation;
    @CsvBindByName(column = "Street")
    private String street;
    @CsvBindByName(column = "ZIP")
    private String zip;
    @CsvBindByName(column = "City")
    private String city;
    @CsvBindByName(column = "Standard")
    private String standard;
    @CsvBindByName(column = "Annex")
    private String annex;
    @CsvBindByName(column = "Note(s)")
    private String notes;
    @CsvBindByName(column = "Legal notice")
    private String legalNotice;

}

and this MyCsvStringToDoubleConverter.class:

import com.opencsv.bean.AbstractBeanField;
import com.opencsv.exceptions.CsvConstraintViolationException;
import com.opencsv.exceptions.CsvDataTypeMismatchException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class MyCsvStringToDoubleConverter extends AbstractBeanField {

    final Pattern takeAllUntilFirstSpacePattern = Pattern.compile("(^\\d+(\\.\\d+)?)");
    @Override
    protected Object convert(String value) throws CsvDataTypeMismatchException, CsvConstraintViolationException {
        if (value == null || value.equals("?") || value.equalsIgnoreCase("N/A") || value.equals("-")) {
            return null;
        }
        final Matcher matcherQb = takeAllUntilFirstSpacePattern.matcher(value);
        // if we find a match, get the first matching String
        if (matcherQb.find()) {
            return Double.parseDouble(matcherQb.group(1));
        }
        return null;
    }
}

Solution

  • ok, the final killed me... So this works:

    List<MyWindLoadDTO> windLoadDTOs = new CsvToBeanBuilder<MyWindLoadDTO>(reader)
                            .withType(MyWindLoadDTO.class)
                            .build()
                            .parse();
    

    I know this may get downvoted, but I'll not delete it, maybe someone else is helped by it as an example for using @CsvCustomBindByName`` with a custom converter.