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

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

package com.qiniu.convert;

import com.qiniu.interfaces.IStringFormat;
import com.qiniu.util.ConvertingUtils;

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

public class MapToString extends Converter, String> {

    private IStringFormat> stringFormatter;

    public MapToString(String format, String separator, List fields) throws IOException {
        if (separator == null) throw new IOException("separator can not be null.");
        if ("json".equals(format)) {
            stringFormatter = line -> ConvertingUtils.toPair(line, fields, new JsonObjectPair()).toString();
        } else if ("csv".equals(format)) {
            stringFormatter = line -> ConvertingUtils.toPair(line, fields, new StringBuilderPair(","));
        } else if ("tab".equals(format)) {
            stringFormatter = line -> ConvertingUtils.toPair(line, fields, new StringBuilderPair(separator));
        } else if ("yaml".equals(format)) {
            stringFormatter = line -> ConvertingUtils.toStringWithIndent(line, fields);
        } else {
            throw new IOException("please check your format for map to string.");
        }
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy