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

com.qiniu.convert.LineToMap Maven / Gradle / Ivy

There is a newer version: 8.4.8
Show newest version
package com.qiniu.convert;

import com.qiniu.interfaces.ILineParser;
import com.qiniu.interfaces.ITypeConvert;
import com.qiniu.util.LineUtils;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

public class LineToMap implements ITypeConvert> {

    private ILineParser lineParser;
    private List errorList = new ArrayList<>();

    public LineToMap(String parseType, String separator, Map indexMap) throws IOException {
        if ("json".equals(parseType)) {
            this.lineParser = line -> LineUtils.getItemMap(line, indexMap, false);
        } else if ("csv".equals(parseType)) {
            this.lineParser = line -> LineUtils.getItemMap(line, ",", indexMap, false);
        } else if ("tab".equals(parseType)) {
            this.lineParser = line -> LineUtils.getItemMap(line, separator, indexMap, false);
        } else {
            throw new IOException("please check your format for line to map.");
        }
    }

    public List> convertToVList(List lineList) {
        if (lineList == null || lineList.size() == 0) return new ArrayList<>();
        return lineList.stream()
                .map(line -> {
                    try {
                        return lineParser.getItemMap(line);
                    } catch (Exception e) {
                        errorList.add(line + "\t" + e.getMessage());
                        return null;
                    }
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }

    public List getErrorList() {
        return errorList;
    }

    public List consumeErrorList() {
        List errors = new ArrayList<>();
        Collections.addAll(errors, new String[errorList.size()]);
        Collections.copy(errors, errorList);
        errorList.clear();
        return errors;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy