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

net.sixpointsix.carpo.common.extractor.StreamingDataSetExtractor Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package net.sixpointsix.carpo.common.extractor;

import net.sixpointsix.carpo.common.extractor.method.DataElement;
import net.sixpointsix.carpo.common.extractor.method.ReadOnlyExtractionMethodList;
import net.sixpointsix.carpo.common.model.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Extract data using java streams
 *
 * @author Andrew Tarry
 * @since 0.2.0
 */
public class StreamingDataSetExtractor implements DataSetExtractor {

    private final PropertyExtractor propertyExtractor;

    public StreamingDataSetExtractor(PropertyExtractor propertyExtractor) {
        this.propertyExtractor = propertyExtractor;
    }

    public DataSet getData(List data, ReadOnlyExtractionMethodList extractionMethodList) {
        if(data == null || extractionMethodList == null || data.isEmpty() || extractionMethodList.isEmpty()) {
            return DataSet.empty();
        }

        List rows = data
                .parallelStream()
                .map(e -> {
                    return extractionMethodList
                            .stream()
                            .map(m -> extractToDatapoint(e, m))
                            .collect(Collectors.toList());
                })
                .map(DataPointRow::new)
                .collect(Collectors.toList());

        return new ListDataSet(rows);
    }

    private DataPoint extractToDatapoint(PropertyHoldingEntity entity, DataElement element) {
        String value = element.extract(entity, propertyExtractor);

        return new DataPoint(element.getRowHeader(), value);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy