com.holmos.cache.size.layout.sizegetter.ReflectSizeGetter Maven / Gradle / Ivy
package com.holmos.cache.size.layout.sizegetter;
import java.util.ArrayList;
import com.holmos.cache.size.layout.ClassSizeInfoParser;
/**
* 通过反射来层层解析获取对象的占用内存的大小
*
* @author 吴银龙([email protected])
* */
public class ReflectSizeGetter {
/**对象大小获取器,如果在此获取器集合里面找不到合适的,则抛出异常,用了链模式和访问者模式*/
protected ArrayList sizeGetters;
public ReflectSizeGetter(ArrayList sizeGetters){
this.sizeGetters=sizeGetters;
}
public int getDataSize(Object value){
if(value.getClass().isPrimitive())
return 1;
int size=0;
for(SizeGetter getter:sizeGetters){
if(getter.canGet(value)){
size+=getter.getsize(value);
}
}
return size;
}
public int getDataSize(long value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(long.class);
}
public int getDataSize(double value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(double.class);
}
public int getDataSize(int value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(int.class);
}
public int getDataSize(float value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(float.class);
}
public int getDataSize(char value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(char.class);
}
public int getDataSize(short value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(short.class);
}
public int getDataSize(byte value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(byte.class);
}
public int getDataSize(boolean value){
return ClassSizeInfoParser.getLayoutSpecification().getPrimitiveSize(boolean.class);
}
}