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

cn.hutool.core.bean.BeanInfoCache Maven / Gradle / Ivy

There is a newer version: 5.8.33
Show newest version
package cn.hutool.core.bean;

import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.map.ReferenceConcurrentMap;
import cn.hutool.core.map.WeakConcurrentMap;

import java.beans.PropertyDescriptor;
import java.util.Map;

/**
 * Bean属性缓存
* 缓存用于防止多次反射造成的性能问题 * * @author Looly */ public enum BeanInfoCache { INSTANCE; private final WeakConcurrentMap, Map> pdCache = new WeakConcurrentMap<>(); private final WeakConcurrentMap, Map> ignoreCasePdCache = new WeakConcurrentMap<>(); /** * 获得属性名和{@link PropertyDescriptor}Map映射 * * @param beanClass Bean的类 * @param ignoreCase 是否忽略大小写 * @return 属性名和{@link PropertyDescriptor}Map映射 */ public Map getPropertyDescriptorMap(Class beanClass, boolean ignoreCase) { return getCache(ignoreCase).get(beanClass); } /** * 获得属性名和{@link PropertyDescriptor}Map映射 * * @param beanClass Bean的类 * @param ignoreCase 是否忽略大小写 * @param supplier 缓存对象产生函数 * @return 属性名和{@link PropertyDescriptor}Map映射 * @since 5.4.1 */ public Map getPropertyDescriptorMap( Class beanClass, boolean ignoreCase, Func0> supplier) { return getCache(ignoreCase).computeIfAbsent(beanClass, (key)->supplier.callWithRuntimeException()); } /** * 加入缓存 * * @param beanClass Bean的类 * @param fieldNamePropertyDescriptorMap 属性名和{@link PropertyDescriptor}Map映射 * @param ignoreCase 是否忽略大小写 */ public void putPropertyDescriptorMap(Class beanClass, Map fieldNamePropertyDescriptorMap, boolean ignoreCase) { getCache(ignoreCase).put(beanClass, fieldNamePropertyDescriptorMap); } /** * 清空缓存 * * @since 5.7.21 */ public void clear() { this.pdCache.clear(); this.ignoreCasePdCache.clear(); } /** * 根据是否忽略字段名的大小写,返回不用Cache对象 * * @param ignoreCase 是否忽略大小写 * @return {@link ReferenceConcurrentMap} * @since 5.4.1 */ private ReferenceConcurrentMap, Map> getCache(boolean ignoreCase) { return ignoreCase ? ignoreCasePdCache : pdCache; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy