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

com.qiniu.util.LineUtils Maven / Gradle / Ivy

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

import com.aliyun.oss.model.OSSObjectSummary;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qcloud.cos.model.COSObjectSummary;
import com.qiniu.storage.model.FileInfo;

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

public final class LineUtils {

    // 为了保证字段按照设置的顺序来读取,故使用 ArrayList
    final static private List hashFields = new ArrayList(){{
        add("hash");
        add("etag");
    }};

    final static private List timeFields = new ArrayList(){{
        add("datetime");
        add("timestamp");
        add("putTime");
    }};

    final static private List sizeFields = new ArrayList(){{
        add("size");
        add("fsize");
    }};

    final static private List mimeFields = new ArrayList(){{
        add("mime");
        add("mimeType");
    }};

    final static private List ownerFields = new ArrayList(){{
        add("owner");
        add("endUser");
    }};

    final static private List intFields = new ArrayList(){{
        add("status");
    }};

    final static private List longFields = new ArrayList(){{
        addAll(sizeFields);
        add("timestamp");
        add("putTime");
    }};

    final static public List fileInfoFields = new ArrayList(){{
        add("key");
        addAll(hashFields);
        addAll(sizeFields);
        addAll(timeFields);
        addAll(mimeFields);
        add("type");
        addAll(intFields);
        add("md5");
        addAll(ownerFields);
    }};

    public static Map getItemMap(FileInfo fileInfo, Map indexMap) throws IOException {
        if (fileInfo == null || fileInfo.key == null) throw new IOException("empty file or key.");
        Map itemMap = new HashMap<>();
        for (String index : indexMap.keySet()) {
            if (!fileInfoFields.contains(index)) {
                throw new IOException("the index: " + index + " can't be found.");
            }
            switch (index) {
                case "key": itemMap.put(indexMap.get(index), fileInfo.key); break;
                case "hash":
                case "etag":
                    itemMap.put(indexMap.get(index), fileInfo.hash); break;
                case "size":
                case "fsize":
                    itemMap.put(indexMap.get(index), String.valueOf(fileInfo.fsize)); break;
                case "datetime":
                    itemMap.put(indexMap.get(index), DatetimeUtils.stringOf(fileInfo.putTime, 10000000)); break;
                case "timestamp":
                case "putTime":
                    itemMap.put(indexMap.get(index), String.valueOf(fileInfo.putTime)); break;
                case "mime":
                case "mimeType":
                    itemMap.put(indexMap.get(index), fileInfo.mimeType); break;
                case "type": itemMap.put(indexMap.get(index), String.valueOf(fileInfo.type)); break;
                case "status": itemMap.put(indexMap.get(index), String.valueOf(fileInfo.status)); break;
                case "owner":
                case "endUser":
                    itemMap.put(indexMap.get(index), fileInfo.endUser); break;
            }
        }
        return itemMap;
    }

    public static Map getItemMap(COSObjectSummary cosObject, Map indexMap)
            throws IOException {
        if (cosObject == null || cosObject.getKey() == null) throw new IOException("empty cosObjectSummary or key.");
        Map itemMap = new HashMap<>();
        for (String index : indexMap.keySet()) {
            if (!fileInfoFields.contains(index)) {
                throw new IOException("the index: " + index + " can't be found.");
            }
            switch (index) {
                case "key": itemMap.put(indexMap.get(index), cosObject.getKey()); break;
                case "hash":
                case "etag":
                    itemMap.put(indexMap.get(index), cosObject.getETag()); break;
                case "size":
                case "fsize":
                    itemMap.put(indexMap.get(index), String.valueOf(cosObject.getSize())); break;
                case "datetime":
                    itemMap.put(indexMap.get(index), DatetimeUtils.stringOf(cosObject.getLastModified())); break;
                case "timestamp":
                case "putTime":
                    itemMap.put(indexMap.get(index), String.valueOf(cosObject.getLastModified().getTime())); break;
                case "type": itemMap.put(indexMap.get(index), cosObject.getStorageClass()); break;
                case "owner":
                case "endUser":
                    itemMap.put(indexMap.get(index), cosObject.getOwner().getDisplayName()); break;
            }
        }
        return itemMap;
    }

    public static Map getItemMap(OSSObjectSummary ossObject, Map indexMap)
            throws IOException {
        if (ossObject == null || ossObject.getKey() == null) throw new IOException("empty cosObjectSummary or key.");
        Map itemMap = new HashMap<>();
        for (String index : indexMap.keySet()) {
            if (!fileInfoFields.contains(index)) {
                throw new IOException("the index: " + index + " can't be found.");
            }
            switch (index) {
                case "key": itemMap.put(indexMap.get(index), ossObject.getKey()); break;
                case "hash":
                case "etag":
                    itemMap.put(indexMap.get(index), ossObject.getETag()); break;
                case "size":
                case "fsize":
                    itemMap.put(indexMap.get(index), String.valueOf(ossObject.getSize())); break;
                case "datetime":
                    itemMap.put(indexMap.get(index), DatetimeUtils.stringOf(ossObject.getLastModified())); break;
                case "timestamp":
                case "putTime":
                    itemMap.put(indexMap.get(index), String.valueOf(ossObject.getLastModified().getTime())); break;
                case "type": itemMap.put(indexMap.get(index), ossObject.getStorageClass()); break;
                case "owner":
                case "endUser":
                    itemMap.put(indexMap.get(index), ossObject.getOwner().getDisplayName()); break;
            }
        }
        return itemMap;
    }

    public static Map getItemMap(JsonObject json, Map indexMap) throws IOException {
        if (json == null) throw new IOException("empty JsonObject.");
        Map itemMap = new HashMap<>();
        for (String index : indexMap.keySet()) {
            if (json.has(index)) itemMap.put(indexMap.get(index), JsonUtils.toString(json.get(index)));
            else throw new IOException("the index: " + index + " can't be found.");
        }
        return itemMap;
    }

    public static Map getItemMap(String line, Map indexMap) throws IOException {
        if (line == null) throw new IOException("empty json line.");
        JsonObject parsed = new JsonParser().parse(line).getAsJsonObject();
        return getItemMap(parsed, indexMap);
    }

    public static Map getItemMap(String line, String separator, Map indexMap) throws IOException {
        if (line == null) throw new IOException("empty string line.");
        String[] items = line.split(separator);
        Map itemMap = new HashMap<>();
        int position;
        for (String index : indexMap.keySet()) {
            position = Integer.valueOf(index);
            if (items.length > position) itemMap.put(indexMap.get(index), items[position]);
            else throw new IOException("the index: " + index + " can't be found.");
        }
        return itemMap;
    }

    public static String toFormatString(FileInfo fileInfo, Set rmFields) throws IOException {
        if (fileInfo == null || fileInfo.key == null) throw new IOException("empty file or key.");
        JsonObject converted = new JsonObject();
        if (rmFields == null || !rmFields.contains("key")) converted.addProperty("key", fileInfo.key);
        if (rmFields == null || hashFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("hash", fileInfo.hash);
        if (rmFields == null || sizeFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("size", fileInfo.fsize);
        if (rmFields == null || timeFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("datetime", DatetimeUtils.stringOf(fileInfo.putTime, 10000000));
        if (rmFields == null || mimeFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("mime", fileInfo.mimeType);
        if (rmFields == null || !rmFields.contains("type")) converted.addProperty("type", fileInfo.type);
        if (rmFields == null || !rmFields.contains("status")) converted.addProperty("status", fileInfo.status);
//        if (rmFields == null || !rmFields.contains("md5")) converted.addProperty("md5", fileInfo.md5);
        if ((rmFields == null || ownerFields.stream().noneMatch(rmFields::contains)) && fileInfo.endUser != null)
            converted.addProperty("owner", fileInfo.endUser);
        if (converted.size() == 0) throw new IOException("empty result.");
        return converted.toString();
    }

    public static String toFormatString(FileInfo fileInfo, String separator, Set rmFields) throws IOException {
        if (fileInfo == null || fileInfo.key == null) throw new IOException("empty file or key.");
        StringBuilder converted = new StringBuilder();
        if (rmFields == null || !rmFields.contains("key")) converted.append(fileInfo.key).append(separator);
        if (rmFields == null || hashFields.stream().noneMatch(rmFields::contains))
            converted.append(fileInfo.hash).append(separator);
        if (rmFields == null || sizeFields.stream().noneMatch(rmFields::contains))
            converted.append(fileInfo.fsize).append(separator);
        if (rmFields == null || timeFields.stream().noneMatch(rmFields::contains))
            converted.append(DatetimeUtils.stringOf(fileInfo.putTime, 10000000)).append(separator);
        if (rmFields == null || mimeFields.stream().noneMatch(rmFields::contains))
            converted.append(fileInfo.mimeType).append(separator);
        if (rmFields == null || !rmFields.contains("type")) converted.append(fileInfo.type).append(separator);
        if (rmFields == null || !rmFields.contains("status")) converted.append(fileInfo.status).append(separator);
//        if (rmFields == null || !rmFields.contains("md5")) converted.append(fileInfo.md5).append(separator);
        if ((rmFields == null || ownerFields.stream().noneMatch(rmFields::contains)) && fileInfo.endUser != null)
            converted.append(fileInfo.endUser).append(separator);
        if (converted.length() <= separator.length()) throw new IOException("empty result.");
        return converted.deleteCharAt(converted.length() - separator.length()).toString();
    }

    public static String toFormatString(COSObjectSummary cosObject, Set rmFields) throws IOException {
        if (cosObject == null || cosObject.getKey() == null) throw new IOException("empty cosObjectSummary or key.");
        JsonObject converted = new JsonObject();
        if (rmFields == null || !rmFields.contains("key")) converted.addProperty("key", cosObject.getKey());
        if (rmFields == null || hashFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("hash", cosObject.getETag());
        if (rmFields == null || sizeFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("size", cosObject.getSize());
        if (rmFields == null || timeFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("datetime", DatetimeUtils.stringOf(cosObject.getLastModified()));
        if (rmFields == null || !rmFields.contains("type")) converted.addProperty("type", cosObject.getStorageClass());
        if ((rmFields == null || ownerFields.stream().noneMatch(rmFields::contains)) && cosObject.getOwner() != null)
            converted.addProperty("owner", cosObject.getOwner().getDisplayName());
        if (converted.size() == 0) throw new IOException("empty result.");
        return converted.toString();
    }

    public static String toFormatString(COSObjectSummary cosObject, String separator, Set rmFields)
            throws IOException {
        if (cosObject == null || cosObject.getKey() == null) throw new IOException("empty cosObjectSummary or key.");
        StringBuilder converted = new StringBuilder();
        if (rmFields == null || !rmFields.contains("key")) converted.append(cosObject.getKey()).append(separator);
        if (rmFields == null || hashFields.stream().noneMatch(rmFields::contains))
            converted.append(cosObject.getETag()).append(separator);
        if (rmFields == null || sizeFields.stream().noneMatch(rmFields::contains))
            converted.append(cosObject.getSize()).append(separator);
        if (rmFields == null || timeFields.stream().noneMatch(rmFields::contains))
            converted.append(DatetimeUtils.stringOf(cosObject.getLastModified())).append(separator);
        if (rmFields == null || !rmFields.contains("type")) converted.append(cosObject.getStorageClass()).append(separator);
        if ((rmFields == null || ownerFields.stream().noneMatch(rmFields::contains)) && cosObject.getOwner() != null)
            converted.append(cosObject.getOwner().getDisplayName()).append(separator);
        if (converted.length() <= separator.length()) throw new IOException("empty result.");
        return converted.deleteCharAt(converted.length() - separator.length()).toString();
    }

    public static String toFormatString(OSSObjectSummary ossObject, Set rmFields) throws IOException {
        if (ossObject == null || ossObject.getKey() == null) throw new IOException("empty cosObjectSummary or key.");
        JsonObject converted = new JsonObject();
        if (rmFields == null || !rmFields.contains("key")) converted.addProperty("key", ossObject.getKey());
        if (rmFields == null || hashFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("hash", ossObject.getETag());
        if (rmFields == null || sizeFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("size", ossObject.getSize());
        if (rmFields == null || timeFields.stream().noneMatch(rmFields::contains))
            converted.addProperty("datetime", DatetimeUtils.stringOf(ossObject.getLastModified()));
        if (rmFields == null || !rmFields.contains("type")) converted.addProperty("type", ossObject.getStorageClass());
        if ((rmFields == null || ownerFields.stream().noneMatch(rmFields::contains)) && ossObject.getOwner() != null)
            converted.addProperty("owner", ossObject.getOwner().getDisplayName());
        if (converted.size() == 0) throw new IOException("empty result.");
        return converted.toString();
    }

    public static String toFormatString(OSSObjectSummary ossObject, String separator, Set rmFields)
            throws IOException {
        if (ossObject == null || ossObject.getKey() == null) throw new IOException("empty cosObjectSummary or key.");
        StringBuilder converted = new StringBuilder();
        if (rmFields == null || !rmFields.contains("key")) converted.append(ossObject.getKey()).append(separator);
        if (rmFields == null || hashFields.stream().noneMatch(rmFields::contains))
            converted.append(ossObject.getETag()).append(separator);
        if (rmFields == null || sizeFields.stream().noneMatch(rmFields::contains))
            converted.append(ossObject.getSize()).append(separator);
        if (rmFields == null || timeFields.stream().noneMatch(rmFields::contains))
            converted.append(DatetimeUtils.stringOf(ossObject.getLastModified())).append(separator);
        if (rmFields == null || !rmFields.contains("type")) converted.append(ossObject.getStorageClass()).append(separator);
        if ((rmFields == null || ownerFields.stream().noneMatch(rmFields::contains)) && ossObject.getOwner() != null)
            converted.append(ossObject.getOwner().getDisplayName()).append(separator);
        if (converted.length() <= separator.length()) throw new IOException("empty result.");
        return converted.deleteCharAt(converted.length() - separator.length()).toString();
    }

    // 以下的 format 方法中先通过 fileInfoFields.foreach 来匹配是由于需要保证输出结果按照字段顺序排列

    public static String toFormatString(Map line, Set rmFields) throws IOException {
        if (line == null) throw new IOException("empty string map.");
        JsonObject converted = new JsonObject();
        Set keySet = line.keySet();
        if (rmFields != null) keySet.removeAll(rmFields);
        fileInfoFields.forEach(key -> {
            if (keySet.contains(key) && line.get(key) != null) {
                if (longFields.contains(key)) converted.addProperty(key, Long.valueOf(line.get(key)));
                else if (intFields.contains(key)) converted.addProperty(key, Integer.valueOf(line.get(key)));
                else converted.addProperty(key, line.get(key));
            }
            keySet.remove(key);
        });
        for (String key : keySet) converted.addProperty(key, line.get(key));
        if (converted.size() == 0) throw new IOException("empty result.");
        return converted.toString();
    }

    public static String toFormatString(Map line, String separator, Set rmFields) throws IOException {
        if (line == null) throw new IOException("empty string map.");
        StringBuilder converted = new StringBuilder();
        Set keySet = line.keySet();
        if (rmFields != null) keySet.removeAll(rmFields);
        fileInfoFields.forEach(key -> {
            if (keySet.contains(key) && line.get(key) != null) {
                if (longFields.contains(key)) converted.append(Long.valueOf(line.get(key))).append(separator);
                else if (intFields.contains(key)) converted.append(Integer.valueOf(line.get(key))).append(separator);
                else converted.append(line.get(key)).append(separator);
            }
            keySet.remove(key);
        });
        for (String key : keySet) converted.append(line.get(key)).append(separator);
        if (converted.length() <= separator.length()) throw new IOException("empty result.");
        return converted.deleteCharAt(converted.length() - separator.length()).toString();
    }

    public static String toFormatString(JsonObject json, String separator, Set rmFields) throws IOException {
        if (json == null) throw new IOException("empty JsonObject.");
        StringBuilder converted = new StringBuilder();
        Set keySet = json.keySet();
        if (rmFields != null) keySet.removeAll(rmFields);
        fileInfoFields.forEach(key -> {
            if (keySet.contains(key) && !(json.get(key) instanceof JsonNull)) {
                if (longFields.contains(key)) converted.append(json.get(key).getAsLong()).append(separator);
                else if (intFields.contains(key)) converted.append(json.get(key).getAsInt()).append(separator);
                else converted.append(json.get(key).getAsString()).append(separator);
            }
            keySet.remove(key);
        });
        for (String key : keySet) converted.append(JsonUtils.toString(json.get(key))).append(separator);
        if (converted.length() <= separator.length()) throw new IOException("empty result.");
        return converted.deleteCharAt(converted.length() - separator.length()).toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy