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

org.csveed.api.CsvClientImpl Maven / Gradle / Ivy

Go to download

Java toolkit for mapping CSV-to-Bean mapping and vice versa. Redirection to Introduction in 5 seconds!

There is a newer version: 0.8.2
Show newest version
package org.csveed.api;

import org.csveed.bean.*;
import org.csveed.bean.conversion.Converter;
import org.csveed.report.CsvException;
import org.csveed.report.GeneralError;
import org.csveed.row.*;

import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

public class CsvClientImpl implements CsvClient {

    private BeanReader beanReader;

    private BeanWriter beanWriter;

    private RowReader rowReader;

    private RowWriter rowWriter;
    
    private final RowInstructions rowInstructions;

    private BeanInstructions beanInstructions;

    public CsvClientImpl(Writer writer) {
        this.rowWriter = new RowWriterImpl(writer);
        this.rowInstructions = getRowWriter().getRowInstructions();
    }

    public CsvClientImpl(Writer writer, Class beanClass) {
        this(writer, new BeanParser().getBeanInstructions(beanClass));
    }

    public CsvClientImpl(Writer writer, BeanInstructions beanInstructions) {
        this.beanWriter= new BeanWriterImpl(writer, beanInstructions);
        this.rowWriter = getBeanWriter().getRowWriter();
        this.rowInstructions = getRowWriter().getRowInstructions();
        this.beanInstructions = beanInstructions;
    }

    public CsvClientImpl(Reader reader) {
        this.rowReader = new RowReaderImpl(reader);
        this.rowInstructions = getRowReader().getRowInstructions();
    }
    
    public CsvClientImpl(Reader reader, Class beanClass) {
        this(reader, new BeanParser().getBeanInstructions(beanClass));
    }

    public CsvClientImpl(Reader reader, BeanInstructions beanInstructions) {
        this.beanReader = new BeanReaderImpl(reader, beanInstructions);
        this.rowReader = getBeanReader().getRowReader();
        this.rowInstructions = getRowReader().getRowInstructions();
        this.beanInstructions = beanInstructions;
    }

    @Override
    public void writeBeans(Collection beans) {
        getBeanWriter().writeBeans(beans);
    }

    @Override
    public void writeBean(T bean) {
        getBeanWriter().writeBean(bean);
    }

    @Override
    public void writeRow(Row row) {
        getRowWriter().writeRow(row);
    }

    @Override
    public Row writeRow(String[] row) {
        return getRowWriter().writeRow(row);
    }

    @Override
    public void writeRows(Collection rows) {
        getRowWriter().writeRows(rows);
    }

    @Override
    public void writeRows(String[][] rows) {
        getRowWriter().writeRows(rows);
    }

    public Header writeHeader(String[] header) {
        return getRowWriter().writeHeader(header);
    }

    public void writeHeader(Header header) {
        getRowWriter().writeHeader(header);
    }

    @Override
    public List readBeans() {
        return getBeanReader().readBeans();
    }

    @Override
    public T readBean() {
        return getBeanReader().readBean();
    }

    @Override
    public List readRows() {
        return getRowReader().readRows();
    }

    @Override
    public Row readRow() {
        return getRowReader().readRow();
    }

    @Override
    public Header readHeader() {
        return getRowReader().readHeader();
    }

    @Override
    public int getCurrentLine() {
        return getRowReader().getCurrentLine();
    }

    @Override
    public boolean isFinished() {
        return getRowReader().isFinished();
    }

    @Override
    public CsvClient setUseHeader(boolean useHeader) {
        rowInstructions.setUseHeader(useHeader);
        return this;
    }

    @Override
    public CsvClient setStartRow(int startRow) {
        rowInstructions.setStartRow(startRow);
        return this;
    }

    @Override
    public CsvClient setEscape(char symbol) {
        rowInstructions.setEscape(symbol);
        return this;
    }

    @Override
    public CsvClient setQuote(char symbol) {
        rowInstructions.setQuote(symbol);
        return this;
    }

    @Override
    public CsvClient setSeparator(char symbol) {
        rowInstructions.setSeparator(symbol);
        return this;
    }

    @Override
    public CsvClient setComment(char symbol) {
        rowInstructions.setComment(symbol);
        return this;
    }

    @Override
    public CsvClient setEndOfLine(char[] symbols) {
        rowInstructions.setEndOfLine(symbols);
        return this;
    }

    @Override
    public CsvClient skipEmptyLines(boolean skip) {
        rowInstructions.skipEmptyLines(skip);
        return this;
    }

    @Override
    public CsvClient skipCommentLines(boolean skip) {
        rowInstructions.skipCommentLines(skip);
        return this;
    }

    @Override
    public CsvClient setMapper(Class mapper) {
        getBeanInstructions().setMapper(mapper);
        return this;
    }

    @Override
    public CsvClient setDate(String propertyName, String dateFormat) {
        getBeanInstructions().setDate(propertyName, dateFormat);
        return this;
    }

    @Override
    public CsvClient setLocalizedNumber(String propertyName, Locale locale) {
        getBeanInstructions().setLocalizedNumber(propertyName, locale);
        return this;
    }

    @Override
    public CsvClient setRequired(String propertyName, boolean required) {
        getBeanInstructions().setRequired(propertyName, required);
        return this;
    }

    @Override
    public CsvClient setConverter(String propertyName, Converter converter) {
        getBeanInstructions().setConverter(propertyName, converter);
        return this;
    }

    @Override
    public CsvClient ignoreProperty(String propertyName) {
        getBeanInstructions().ignoreProperty(propertyName);
        return this;
    }

    @Override
    public CsvClient mapColumnIndexToProperty(int columnIndex, String propertyName) {
        getBeanInstructions().mapColumnIndexToProperty(columnIndex, propertyName);
        return this;
    }

    @Override
    public CsvClient mapColumnNameToProperty(String columnName, String propertyName) {
        getBeanInstructions().mapColumnNameToProperty(columnName, propertyName);
        return this;
    }

    @Override
    public CsvClient setStartIndexDynamicColumns(int startIndex) {
        getBeanInstructions().setStartIndexDynamicColumns(startIndex);
        return this;
    }

    @Override
    public CsvClient setHeaderNameToProperty(String propertyName) {
        getBeanInstructions().setHeaderNameToProperty(propertyName);
        return this;
    }

    @Override
    public CsvClient setHeaderValueToProperty(String propertyName) {
        getBeanInstructions().setHeaderValueToProperty(propertyName);
        return this;
    }
    
    private BeanInstructions getBeanInstructions() {
        if (this.beanInstructions == null) {
            throw new CsvException(new GeneralError(
                    "BeanInstructions have not been initialized. Make sure to pass BeanInstructions or the bean class" +
                    " to CsvClient."
            ));
        }
        return this.beanInstructions;
    }

    private BeanReader getBeanReader() {
        if (this.beanReader == null) {
            throw new CsvException(new GeneralError(
                    "BeanReader has not been initialized. Make sure to pass BeanInstructions or the bean class to CsvClient."
            ));
        }
        return this.beanReader;
    }

    private BeanWriter getBeanWriter() {
        if (this.beanWriter == null) {
            throw new CsvException(new GeneralError(
                    "BeanWriter has not been initialized. Make sure to pass BeanInstructions or the bean class to CsvClient."
            ));
        }
        return this.beanWriter;
    }

    private RowReader getRowReader() {
        if (this.rowReader == null) {
            throw new CsvException(new GeneralError(
                    "RowReader has not been initialized. Make sure to pass a Reader to the constructor."));
        }
        return this.rowReader;
    }

    public RowWriter getRowWriter() {
        if (this.rowWriter == null) {
            throw new CsvException(new GeneralError(
                    "RowWriter has not been initialized. Make sure to pass a Writer to the constructor."));
        }
        return rowWriter;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy