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

org.sfm.csv.impl.CsvMapperCellConsumer Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.csv.impl;

import org.sfm.csv.parser.CellConsumer;
import org.sfm.map.RowHandlerErrorHandler;
import org.sfm.utils.RowHandler;

import java.util.Collection;

public final class CsvMapperCellConsumer implements CellConsumer {


    private final CsvMapperCellHandler mapperSetters;



    protected final RowHandlerErrorHandler rowHandlerErrorHandlers;

    protected final BreakDetector breakDetector;

    protected final RowHandler handler;

    protected final CsvMapperCellConsumer[] children;

    protected int cellIndex = 0;

    @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
    public CsvMapperCellConsumer(
            CsvMapperCellHandler csvMapperCellHandler,
            RowHandlerErrorHandler rowHandlerErrorHandlers,
            RowHandler handler,
            BreakDetector breakDetector, Collection> children) {
        super();
        this.mapperSetters = csvMapperCellHandler;
        this.rowHandlerErrorHandlers = rowHandlerErrorHandlers;
        this.handler = handler;
        this.breakDetector = breakDetector;
        this.children = children.toArray(new CsvMapperCellConsumer[0]);
    }

    @Override
    public final void endOfRow() {
        composeInstance();
        resetConsumer();
    }

    public final T getCurrentInstance() {
        return mapperSetters.getCurrentInstance();
    }


    protected final boolean hasData() {
        return cellIndex > 0;
    }


    protected final void callHandler() {
        if (handler == null) return;
        try {
            handler.handle(getCurrentInstance());
        } catch (Exception e) {
            rowHandlerErrorHandlers.handlerError(e, getCurrentInstance());
        }
    }


    @Override
    public final void end() {
        endOfRow();
        afterEnd();
    }


    @Override
    public final void newCell(char[] chars, int offset, int length) {
        int index = cellIndex;
        newCell(chars, offset, length, index);
        afterNewCell(index);
    }

    public final void newCell(char[] chars, int offset, int length, int cellIndex) {
        if (mapperSetters.isDelayedSetter(cellIndex)) {
            mapperSetters.delayedCellValue(chars, offset, length, cellIndex);
        } else if (isNotNull()) {
            mapperSetters.cellValue(chars, offset, length, cellIndex);
        }
        this.cellIndex = cellIndex + 1;
    }

    private boolean isNotNull() {
        if (breakDetector == null) {
            mapperSetters.createInstanceIfNull();
            return true;
        } else {
            return breakDetector.isNotNull();
        }
    }

    public final BreakDetector getBreakDetector() {
        return breakDetector;
    }


    private void resetConsumer() {
        for (CsvMapperCellConsumer child : children) {
            child.resetConsumer();
        }
        if (breakDetector != null) {
            breakDetector.reset();
        } else {
            mapperSetters.resetCurrentInstance();
        }
        cellIndex = 0;
    }

    private void composeInstance() {
        if (hasData()) {
            if (breakDetector == null || breakDetector.isNotNull()) {
                for (CsvMapperCellConsumer child : children) {
                    child.composeInstance();
                }

                mapperSetters.createInstanceIfNull();

                mapperSetters.applyDelayedSetters();

                if (breakDetector == null) {
                    callHandler();
                }
            }
        }
    }


    protected void afterEnd() {
        if (breakDetector != null && mapperSetters.hasInstance()) {
            callHandler();
        }
    }

    protected void afterNewCell(int index) {
        if (breakDetector == null) return;
        updateBreakStatus(index);
    }


    private void updateBreakStatus(int cellIndex) {

        if (breakDetector.updateStatus(mapperSetters, cellIndex)) {
            if (breakDetector.broken()) {
                if (mapperSetters.hasInstance()) {
                    callHandler();
                    mapperSetters.resetCurrentInstance();
                }

                updateChildrenStatus(cellIndex);

                if (breakDetector.isNotNull()) {
                    mapperSetters.createInstance();
                }
                return;
            }
        }

        updateChildrenStatus(cellIndex);
    }

    private void updateChildrenStatus(int cellIndex) {
        for (CsvMapperCellConsumer consumer : children) {
            consumer.updateBreakStatus(cellIndex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy