io.github.xinshepherd.excel.core.ExcelContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easy-excel Show documentation
Show all versions of easy-excel Show documentation
Easy Usage of the Apache POI
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 extends FontStyle> clazz) {
return fontStyleMap.computeIfAbsent(clazz.getName(), key -> {
try {
Constructor extends FontStyle> 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);
}
});
}
}