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

org.sfm.poi.impl.JoinSheetMapper Maven / Gradle / Ivy

package org.sfm.poi.impl;


import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.sfm.map.*;
import org.sfm.map.context.MappingContextFactory;
import org.sfm.map.mapper.JoinMapperEnumarable;
import org.sfm.poi.RowMapper;
import org.sfm.utils.Enumarable;
import org.sfm.utils.EnumarableIterator;
import org.sfm.utils.RowHandler;

import java.util.Iterator;

//IFJAVA8_START
import org.sfm.utils.EnumarableSpliterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
//IFJAVA8_END

public class JoinSheetMapper implements RowMapper {

    private final Mapper mapper;
    private final int startRow = 0;

    private final RowHandlerErrorHandler rowHandlerErrorHandler;
    private final MappingContextFactory mappingContextFactory;

    public JoinSheetMapper(Mapper mapper, RowHandlerErrorHandler rowHandlerErrorHandler, MappingContextFactory mappingContextFactory) {
        this.mapper = mapper;
        this.rowHandlerErrorHandler = rowHandlerErrorHandler;
        this.mappingContextFactory = mappingContextFactory;
    }

    @Override
    public Iterator iterator(Sheet sheet) {
        return iterator(startRow, sheet);
    }

    @Override
    public Iterator iterator(int startRow, Sheet sheet) {
        return new EnumarableIterator(enumerable(startRow, sheet, newMappingContext()));
    }

    private Enumarable enumerable(int startRow, Sheet sheet, MappingContext mappingContext) {
        return new JoinMapperEnumarable(mapper, mappingContext, new RowEnumarable(startRow, sheet));
    }

    @Override
    public > RH forEach(Sheet sheet, RH rowHandler) {
        return forEach(startRow, sheet, rowHandler);
    }

    @Override
    public > RH forEach(int startRow, Sheet sheet, RH rowHandler) {
        MappingContext mappingContext = newMappingContext();

        Enumarable enumarable = enumerable(startRow, sheet, mappingContext);

        while(enumarable.next()) {
            try {
                rowHandler.handle(enumarable.currentValue());
            } catch(Exception e) {
                rowHandlerErrorHandler.handlerError(e, enumarable.currentValue());
            }
        }

        return rowHandler;
    }

    //IFJAVA8_START
    @Override
    public Stream stream(Sheet sheet) {
        return stream(startRow, sheet);
    }

    @Override
    public Stream stream(int startRow, Sheet sheet) {
        return StreamSupport.stream(new EnumarableSpliterator(enumerable(startRow, sheet, newMappingContext())), false);
    }
    //IFJAVA8_END


    @Override
    public T map(Row source) throws MappingException {
        return mapper.map(source);
    }

    @Override
    public T map(Row source, MappingContext context) throws MappingException {
        return mapper.map(source, context);
    }

    @Override
    public void mapTo(Row source, T target, MappingContext context) throws Exception {
        mapper.mapTo(source, target, context);
    }

    private MappingContext newMappingContext() {
        return mappingContextFactory.newContext();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy