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

com.qiniu.service.line.SplitLineParser Maven / Gradle / Ivy

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

import com.qiniu.service.interfaces.ILineParser;

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

public class SplitLineParser implements ILineParser {

    private String separator;
    private Map indexMap;

    public SplitLineParser(String separator, Map indexMap) throws IOException {
        if (indexMap == null || indexMap.size() == 0) throw new IOException("there are no indexMap be set.");
        this.separator = separator;
        this.indexMap = indexMap;
    }

    public Map getItemMap(String line) throws IOException {
        String[] items = line.split(separator);
        Map itemMap = new HashMap<>();
        String mapKey;
        for (int i = 0; i < items.length; i++) {
            mapKey = indexMap.get(String.valueOf(i));
            if (mapKey != null) {
                if (items[i] == null) itemMap.put(mapKey, null);
                else itemMap.put(mapKey, items[i]);
            }
        }
        if (itemMap.size() < indexMap.size())
            throw new IOException("no enough indexes in line. The parameter indexes may have incorrect order or name.");
        return itemMap;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy