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

com.sghd.logging.csv.CsvUtils Maven / Gradle / Ivy

The newest version!
package com.sghd.logging.csv;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * CSV记录索引
 */
public class CsvUtils {
	private static final String SPLIT = "\t";

	private static ConcurrentHashMap, List> cache = new ConcurrentHashMap<>();
	private static ConcurrentHashMap, Map> index_cache = new ConcurrentHashMap<>();

	// //////////////////公开业务///////////////////

	/**
	 * 对象序列化为CSV文本
	 * @param obj 可对象值或class类
	 */
	public static String object2String(Object obj) {
		StringBuilder sbd = new StringBuilder();
		final List infos = getLogMethodInfo(obj);
		final int len = infos.size();

		for (int i = 0; i < len; i++) {
			CsvInfo info = infos.get(i);
			Object value = info.getValue(obj);
			sbd.append(value);
			if (i == len - 1) {
				continue;
			}
			sbd.append(SPLIT);
		}

		return sbd.toString();
	}

	/**
	 * CSV文本反序列化对象
	 * @param csv CSV文本
	 * @param clz 对象类型
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static  T string2Object(String csv, Class clz) {
		final String[] values = csv.split(SPLIT);
		final int len = values.length;
		final List infos = getLogMethodInfo(clz);
		T obj = null;
		try {
			Constructor constructor = clz.getDeclaredConstructor();
			constructor.setAccessible(true);
			obj = (T) constructor.newInstance();
			for (int i = 0; i < infos.size() && i < len; i++) {
				String value = values[i];
				if (value == null) {
					continue;
				}
				if (value.equals("null")) {
					continue;
				}

				CsvInfo info = infos.get(i);
				info.injectValue(obj, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

	/**
	 * 按索引顺序生成类字段
	 */
	public static List getFieldNames(Object obj) {
		final List infos = getLogMethodInfo(obj);
		List names = new ArrayList<>(infos.size());
		for (CsvInfo info : infos) {
			names.add(info.getField().getName());
		}
		return names;
	}

	/**
	 * 顺序生成类字段
	 */
	public static Map getFieldIndexs(Object obj) {
		Class key = null;
		if (obj instanceof Class) {
			key = (Class) obj;
		} else {
			key = obj.getClass();
		}

		Map result = index_cache.get(key);
		if (result != null) {
			return result;
		}
		result = new ConcurrentHashMap<>();

		final List infos = getLogMethodInfo(obj);
		int i = 0;
		for (CsvInfo info : infos) {
			result.put(info.getField().getName(), i);
			i++;
		}
		Map now = index_cache.putIfAbsent(key, result);
		return now == null ? result : now;

	}

	// ///////////////////// 内部方法////////////////////////////

	private static List getLogMethodInfo(Object obj) {
		Class key = null;
		if (obj instanceof Class) {
			key = (Class) obj;
		} else {
			key = obj.getClass();
		}

		List result = cache.get(key);
		if (result != null) {
			return result;
		}
		result = new ArrayList<>();

		if (result.isEmpty()) {
			for (Class c = key; c != null; c = c.getSuperclass()) {
				for (Field field : c.getDeclaredFields()) {
					field.setAccessible(true);
					if (Modifier.isStatic(field.getModifiers())) {
						continue;
					}
					CsvIndex anno = field.getAnnotation(CsvIndex.class);
					if (anno == null) {
						continue;
					}
					JsonIgnore ignore = field.getAnnotation(JsonIgnore.class);
					if (ignore != null) {
						continue;
					}
					int i = anno.value();
					result.add(CsvInfo.valueOf(i, null, field));
				}
			}

		}

		Collections.sort(result, CsvInfo.ASC);
		List now = cache.putIfAbsent(key, result);
		return now == null ? result : now;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy