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

com.orion.office.csv.CsvExt Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package com.orion.office.csv;

import com.orion.lang.able.SafeCloseable;
import com.orion.lang.define.collect.MutableMap;
import com.orion.lang.utils.io.Files1;
import com.orion.office.csv.core.CsvReader;
import com.orion.office.csv.option.CsvReaderOption;
import com.orion.office.csv.reader.*;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * csv 提取器
 * 也支持其他格式的文本文件
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2020/3/27 11:07
 */
public class CsvExt implements SafeCloseable {

    private final CsvReader reader;

    public CsvExt(File file) {
        this(Files1.openInputStreamSafe(file), null);
    }

    public CsvExt(File file, CsvReaderOption option) {
        this(Files1.openInputStreamSafe(file), option);
    }

    public CsvExt(String file) {
        this(Files1.openInputStreamSafe(file), null);
    }

    public CsvExt(String file, CsvReaderOption option) {
        this(Files1.openInputStreamSafe(file), option);
    }

    public CsvExt(InputStream in) {
        this(in, null);
    }

    public CsvExt(InputStream in, CsvReaderOption option) {
        this(new CsvReader(in, option));
    }

    public CsvExt(Reader reader) {
        this(reader, null);
    }

    public CsvExt(Reader reader, CsvReaderOption option) {
        this(new CsvReader(reader, option));
    }

    public CsvExt(CsvReader reader) {
        this.reader = reader;
    }

    /**
     * 将文本转化为 CsvExt
     *
     * @param s s
     * @return CsvExt
     */
    public static CsvExt parse(String s) {
        return new CsvExt(CsvReader.parse(s));
    }

    /**
     * 将文本转化为 CsvExt
     *
     * @param s      s
     * @param option 配置项
     * @return CsvExt
     */
    public static CsvExt parse(String s, CsvReaderOption option) {
        return new CsvExt(CsvReader.parse(s, option));
    }

    // -------------------- array reader --------------------

    public CsvArrayReader arrayReader() {
        return new CsvArrayReader(reader);
    }

    public CsvArrayReader arrayReader(Collection rows) {
        return new CsvArrayReader(reader, rows);
    }

    /**
     * 获取 array 读取器
     *
     * @param consumer consumer
     * @return CsvArrayReader
     */
    public CsvArrayReader arrayReader(Consumer consumer) {
        return new CsvArrayReader(reader, consumer);
    }

    // -------------------- map reader --------------------

    public  CsvMapReader mapReader() {
        return new CsvMapReader<>(reader);
    }

    public  CsvMapReader mapReader(Collection> rows) {
        return new CsvMapReader<>(reader, rows);
    }

    /**
     * 获取 map 读取器
     *
     * @param consumer consumer
     * @param       K
     * @param       V
     * @return CsvMapReader
     */
    public  CsvMapReader mapReader(Consumer> consumer) {
        return new CsvMapReader<>(reader, consumer);
    }

    // -------------------- bean reader --------------------

    public  CsvBeanReader mapReader(Class targetClass) {
        return new CsvBeanReader<>(reader, targetClass);
    }

    public  CsvBeanReader mapReader(Class targetClass, List rows) {
        return new CsvBeanReader<>(reader, targetClass, rows);
    }

    /**
     * 获取 bean 读取器
     *
     * @param consumer consumer
     * @param       T
     * @return CsvBeanReader
     */
    public  CsvBeanReader mapReader(Class targetClass, Consumer consumer) {
        return new CsvBeanReader<>(reader, targetClass, consumer);
    }

    // -------------------- lambda reader --------------------

    public  CsvLambdaReader lambdaReader(Supplier supplier) {
        return new CsvLambdaReader<>(reader, supplier);
    }

    public  CsvLambdaReader lambdaReader(Collection rows, Supplier supplier) {
        return new CsvLambdaReader<>(reader, rows, supplier);
    }

    /**
     * 获取 lambda 读取器
     *
     * @param consumer consumer
     * @param       T
     * @return CsvLambdaReader
     */
    public  CsvLambdaReader lambdaReader(Consumer consumer, Supplier supplier) {
        return new CsvLambdaReader<>(reader, consumer, supplier);
    }

    // -------------------- raw reader --------------------

    public CsvRawReader rawReader() {
        return new CsvRawReader(reader);
    }

    public CsvRawReader rawReader(Collection rows) {
        return new CsvRawReader(reader, rows);
    }

    /**
     * 获取 raw 读取器
     *
     * @param consumer consumer
     * @return CsvRawReader
     */
    public CsvRawReader rawReader(Consumer consumer) {
        return new CsvRawReader(reader, consumer);
    }

    public CsvReader getReader() {
        return reader;
    }

    /**
     * 关闭读取流
     */
    @Override
    public void close() {
        reader.close();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy