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

com.holmos.cache.size.layout.sizegetter.SizeGetter Maven / Gradle / Ivy

There is a newer version: 1.0.2u10
Show newest version
package com.holmos.cache.size.layout.sizegetter;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;

import com.holmos.cache.size.layout.MemoryLayoutSpecification;

/**
 * 获取对象占用缓存内存大小,对于重复的予以记录,不予计算
 * 
 * @author 吴银龙([email protected])
 * 
 * */
public abstract class SizeGetter {
	
	protected MemoryLayoutSpecification layoutSpecification;
	public SizeGetter(MemoryLayoutSpecification layoutSpecification){
		this.layoutSpecification=layoutSpecification;
	}
	private static ArrayListgetters=new ArrayList();
	
	protected ArrayList getStaticElements(Object value){
		ArrayList elements=new ArrayList();
		for(Field field : value.getClass().getDeclaredFields()){
			if((field.getModifiers() & Modifier.STATIC) == 0)
				elements.add(field);
		}
		return elements;
	}
	protected ArrayList getCommonElements(Object value){
		ArrayList elements=new ArrayList();
		for(Field field : value.getClass().getDeclaredFields()){
			if((field.getModifiers() & Modifier.STATIC) ==0)
				elements.add(field);
		}
		return elements;
	}
	static{
		getters.add(SizeGetterFactory.ARRAY_SIZE_GETTER);
		getters.add(SizeGetterFactory.OBJECT_SIZE_GETTER);
	}
	public final static ReflectSizeGetter sizeGetter=new ReflectSizeGetter(getters);
	/**
	 * 得到对象value占用内存的大小
	 * 
  • value中的值如果出现过,那么不予计算,在对象字典里面予以记录
  • *
  • value中没有出现过的对象,予以计算
  • * * @param value 待计算占用内存大小的对象 * @return 对象占用内存大小 * */ public abstract int getsize(Object value); /** * 检查value两个地方 * *
  • 是否是null
  • *
  • 是否和Getter的类型匹配
  • * * @param value 待校验的对象 * @return 是否可以获取该对象的大小 * */ public abstract boolean canGet(Object value); }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy