com.github.timo_reymann.csv_parser.meta.CsvMetaDataReader Maven / Gradle / Ivy
package com.github.timo_reymann.csv_parser.meta;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* Csv meta data reader, used for reading meta information from bean class
*
* @author Timo Reymann
* @since 20.12.17
*/
public class CsvMetaDataReader {
/**
* Cache for mapping of csv columns to fields
*/
private static HashMap> CACHE = new HashMap<>();
/**
* Class object of entity
*/
private Class clazz;
/**
* Mapping of csv columns to fields
*/
private HashMap fields;
/**
* Create new meta data reader
*
* @param clazz Class object for bean to map from and to csv
*/
public CsvMetaDataReader(Class clazz) {
this.clazz = clazz;
}
/**
* Get field csv column mapping
*
* @return Map with fields
*/
private HashMap getFieldsToMap() {
HashMap csvColumnFieldHashMap = CACHE.get(clazz);
if (csvColumnFieldHashMap != null) {
return csvColumnFieldHashMap;
}
fields = new HashMap<>();
process(clazz.getDeclaredFields());
process(clazz.getSuperclass().getDeclaredFields());
CACHE.put(clazz, fields);
return fields;
}
public CsvColumn getCsvColumnForField(Field field) {
return getFieldsToMap().get(field);
}
/**
* Get effective value for column mapping
*
* @return HashMap with fields indexed by Effective index (headerName/index)
*/
public HashMap