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.util.FileUtils;
import com.qiniu.util.ConvertingUtils;

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

public class LineToMap extends Converter> {

    private ILineParser lineParser;

    public LineToMap(String parseType, String separator, String addKeyPrefix, String rmKeyPrefix, Map indexMap)
            throws IOException {
        if (separator == null) throw new IOException("separator can not be null.");
        if ("json".equals(parseType)) {
            this.lineParser = line -> process(addKeyPrefix, rmKeyPrefix, ConvertingUtils.toPair(line, indexMap, new StringMapPair()));
        } else if ("csv".equals(parseType)) {
            this.lineParser = line -> process(addKeyPrefix, rmKeyPrefix, ConvertingUtils.toPair(line, ",", indexMap, new StringMapPair()));
        } else if ("tab".equals(parseType)) {
            this.lineParser = line -> process(addKeyPrefix, rmKeyPrefix, ConvertingUtils.toPair(line, separator, indexMap, new StringMapPair()));
        } else {
            throw new IOException("please check your format for line to map.");
        }
    }

    private Map process(String addKeyPrefix, String rmKeyPrefix, Map itemMap) throws IOException {
        String key = itemMap.get("key");
        if (key != null) {
            if (addKeyPrefix == null) addKeyPrefix = "";
            itemMap.put("key", addKeyPrefix + FileUtils.rmPrefix(rmKeyPrefix, key));
        }
        return itemMap;
    }

    @Override
    public Map convertToV(String line) throws IOException {
        return lineParser.getItemMap(line);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy