All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.sskorol.data.CsvReader Maven / Gradle / Ivy

package io.github.sskorol.data;

import io.github.sskorol.utils.ReflectionUtils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.val;
import one.util.streamex.StreamEx;
import org.apache.commons.csv.CSVFormat;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import static java.lang.String.format;
import static org.apache.commons.csv.CSVParser.parse;
import static org.joor.Reflect.on;

@Getter
@RequiredArgsConstructor
public class CsvReader implements DataReader {

    private final Class entityClass;
    private final String path;

    public CsvReader(final Class entityClass) {
        this(entityClass, "");
    }

    @Override
    public StreamEx read() {
        try (val csvParser = parse(getUrl(), StandardCharsets.UTF_8,
                CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase().withTrim())) {
            val entityFields = StreamEx.of(entityClass.getDeclaredFields()).map(ReflectionUtils::getFieldName).toList();
            return StreamEx.of(csvParser.getRecords())
                           .map(record -> StreamEx.of(entityFields).map(record::get).toArray())
                           .map(args -> on(entityClass).create(args).get());
        } catch (IOException ex) {
            throw new IllegalArgumentException(
                    format("Unable to read JSON data to %s. Check provided path.", entityClass), ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy