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

io.github.xinshepherd.excel.core.ExcelContext Maven / Gradle / Ivy

The newest version!
package io.github.xinshepherd.excel.core;

import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Fuxin
 * @since 1.1.0
 */

public class ExcelContext {

    /**
     * 根据全类名获取字体类型bean
     */
    private final Map fontStyleMap;

    /** Cache of singleton objects created by FactoryBeans: FactoryBean name to object. */
    private final Map factoryBeanObjectCache = new ConcurrentHashMap<>(16);

    public ExcelContext() {
        fontStyleMap = new ConcurrentHashMap<>();
    }

    public FontStyle getFontStyle(Class clazz) {
        return fontStyleMap.computeIfAbsent(clazz.getName(), key -> {
            try {
                Constructor constructor = clazz.getConstructor();
                constructor.setAccessible(true);
                return constructor.newInstance();
            } catch (Exception e) {
                throw new ExcelException(e);
            }
        });
    }

    @SuppressWarnings("unchecked")
    public  T getBean(Class clazz) {
        return (T) factoryBeanObjectCache.computeIfAbsent(clazz.getName(), key -> {
            try {
                Constructor constructor = clazz.getConstructor();
                constructor.setAccessible(true);
                return constructor.newInstance();
            } catch (Exception e) {
                throw new ExcelException(e);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy