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

com.github.kunalk16.excel.objectmapper.ExcelObjectFromSheetMapper Maven / Gradle / Ivy

The newest version!
package com.github.kunalk16.excel.objectmapper;

import com.github.kunalk16.excel.model.user.Row;
import com.github.kunalk16.excel.objectmapper.annotations.ExcelColumn;
import com.github.kunalk16.excel.objectmapper.extractor.ColumnByHeaderRowValueExtractor;
import com.github.kunalk16.excel.objectmapper.extractor.FieldsFromClassExtractor;
import com.github.kunalk16.excel.objectmapper.extractor.ObjectFromRowExtractor;

import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;

public class ExcelObjectFromSheetMapper {
    static  List createObjectFromSheet(Class modelClass, Iterator rowIterator) {
        if (rowIterator.hasNext()) {
            Map fieldsFromClass = new FieldsFromClassExtractor<>(ExcelColumn.class).apply(modelClass);
            if (fieldsFromClass.isEmpty()) {
                return Collections.emptyList();
            }

            Map columnByHeaderRowValue = new ColumnByHeaderRowValueExtractor().apply(rowIterator.next());

            if (columnByHeaderRowValue.isEmpty()) {
                return Collections.emptyList();
            }

            if (!rowIterator.hasNext()) {
                return Collections.emptyList();
            }

            List modelRows = new ArrayList<>();

            Function objectFromRowExtractor = new ObjectFromRowExtractor<>(modelClass,
                    fieldsFromClass, columnByHeaderRowValue);

            while (rowIterator.hasNext()) {
                Optional.ofNullable(rowIterator.next())
                        .map(objectFromRowExtractor)
                        .ifPresent(modelRows::add);
            }

            return Collections.unmodifiableList(modelRows);
        }

        return Collections.emptyList();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy