nyla.solutions.global.patterns.reflection.ObjectClassSizer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nyla.solutions.global Show documentation
Show all versions of nyla.solutions.global Show documentation
Nyla Solutions Global Java API provides support for basic application
utilities (application configuration, data encryption, debugger and text
processing).
The newest version!
package nyla.solutions.global.patterns.reflection;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import nyla.solutions.global.operations.ClassPath;
import nyla.solutions.global.patterns.search.ReLookup;
import nyla.solutions.global.util.Config;
/**
*
* @author Gregory Green
*
*/
public class ObjectClassSizer
{
/**
* Construct object with a default object
* class size map.
*
* this.defaultTypeSizes = new HashMap, Long>();
this.defaultTypeSizes.put(String.class, Long.valueOf(defaultStringSizeBytes)); //100 KB
this.defaultTypeSizes.put(boolean.class,Long.valueOf(1));
this.defaultTypeSizes.put(Boolean.class,Long.valueOf(2));
this.defaultTypeSizes.put(byte.class,Long.valueOf(1));
this.defaultTypeSizes.put(Byte.class,Long.valueOf(2));
this.defaultTypeSizes.put(char.class, Long.valueOf(2));
this.defaultTypeSizes.put(Character.class, Long.valueOf(4));
this.defaultTypeSizes.put( short.class,Long.valueOf(2));
this.defaultTypeSizes.put( Short.class,Long.valueOf(4));
this.defaultTypeSizes.put( int.class,Long.valueOf(4));
this.defaultTypeSizes.put( Integer.class,Long.valueOf(8));
this.defaultTypeSizes.put(long.class, Long.valueOf(8));
this.defaultTypeSizes.put(Long.class, Long.valueOf(18));
this.defaultTypeSizes.put(float.class, Long.valueOf(4));
this.defaultTypeSizes.put(Float.class, Long.valueOf(8));
this.defaultTypeSizes.put(double.class, Long.valueOf(8));
this.defaultTypeSizes.put(Double.class, Long.valueOf(16));
this.defaultTypeSizes.put(Void.class, Long.valueOf(0));
this.defaultTypeSizes.put(BigDecimal.class, Long.valueOf(32));
this.defaultTypeSizes.put(BigInteger.class, Long.valueOf(56));
*/
public ObjectClassSizer()
{
this.defaultTypeSizes = new HashMap, Long>();
//this.defaultTypeSizes.put(String.class, Long.valueOf(defaultStringSizeBytes)); //100 KB
this.defaultTypeSizes.put(boolean.class,Long.valueOf(1));
this.defaultTypeSizes.put(Boolean.class,Long.valueOf(2));
this.defaultTypeSizes.put(byte.class,Long.valueOf(1));
this.defaultTypeSizes.put(Byte.class,Long.valueOf(2));
this.defaultTypeSizes.put(char.class, Long.valueOf(2));
this.defaultTypeSizes.put(Character.class, Long.valueOf(4));
this.defaultTypeSizes.put( short.class,Long.valueOf(2));
this.defaultTypeSizes.put( Short.class,Long.valueOf(4));
this.defaultTypeSizes.put( int.class,Long.valueOf(4));
this.defaultTypeSizes.put( Integer.class,Long.valueOf(8));
this.defaultTypeSizes.put(long.class, Long.valueOf(8));
this.defaultTypeSizes.put(Long.class, Long.valueOf(18));
this.defaultTypeSizes.put(float.class, Long.valueOf(4));
this.defaultTypeSizes.put(Float.class, Long.valueOf(8));
this.defaultTypeSizes.put(double.class, Long.valueOf(8));
this.defaultTypeSizes.put(Double.class, Long.valueOf(16));
this.defaultTypeSizes.put(Date.class, Long.valueOf(32));
this.defaultTypeSizes.put(Void.class, Long.valueOf(0));
this.defaultTypeSizes.put(BigDecimal.class, Long.valueOf(32));
this.defaultTypeSizes.put(BigInteger.class, Long.valueOf(56));
}// --------------------------------------------------------
/**
*
* @param aClass the class to estimate
* @return
*/
public long sizeInBytes(Class> aClass)
{
if(!String.class.equals(aClass) && ClassPath.isPrimitive(aClass) )
{
Long sizeLong = this.defaultTypeSizes.get(aClass);
return sizeLong.longValue();
}
//test if is enum
if(aClass.isEnum())
{
Object[] enumConstants = aClass.getEnumConstants();
long enumEntyrSize = defaultTypeSizes.get(int.class).longValue();
if(enumConstants == null || enumConstants.length == 0)
return enumEntyrSize;
else
return enumEntyrSize * enumConstants.length + 1;
}
//check if in default type size
Long defaultTypeSize = defaultTypeSizes.get(aClass);
if(defaultTypeSize != null)
return defaultTypeSize.longValue();
Field fields[] = aClass.getDeclaredFields();
Field arr[] = fields;
int len = arr.length;
long totalSize = 0;
for(int i = 0; i < len; i++)
{
Field field = arr[i];
//check if field name has a default size
String fieldName = field.getName();
//check if field name has a size
Long fieldNameLengthBytes = fieldDefaultSizes.get(fieldName);
Class> fieldClass = field.getType();
if(fieldNameLengthBytes != null)
{
//Total by field name
totalSize += fieldNameLengthBytes.longValue();
}
else if(String.class.equals(fieldClass) || char[].class.equals(fieldClass))
{
totalSize += defaultStringSizeBytes;
}
else if(ClassPath.isPrimitive(fieldClass) )
{
Long sizeLong = this.defaultTypeSizes.get(fieldClass);
totalSize += sizeLong.longValue();
}
else
{
//nested call
totalSize += sizeInBytes(field.getType());
}
}
return totalSize;
}// --------------------------------------------------------
/**
* @return the defaultStringSizeBytes
*/
public int getDefaultStringSizeBytes() {
return defaultStringSizeBytes;
}
/**
* @param defaultStringSizeBytes the defaultStringSizeBytes to set
*/
public void setDefaultStringSizeBytes(int defaultStringSizeBytes) {
this.defaultStringSizeBytes = defaultStringSizeBytes;
}
/**
* @return the fieldDefaultSizes
*/
public ReLookup getFieldDefaultSizes() {
return fieldDefaultSizes;
}
/**
* @param fieldDefaultSizes the fieldDefaultSizes to set
*/
public void setFieldDefaultSizes(ReLookup fieldDefaultSizes) {
this.fieldDefaultSizes = fieldDefaultSizes;
}
/**
* @return the defaultTypeSizes
*/
public Map, Long> getDefaultTypeSizes() {
return defaultTypeSizes;
}
/**
* @param defaultTypeSizes the defaultTypeSizes to set
*/
public void setDefaultTypeSizes(Map, Long> defaultTypeSizes) {
this.defaultTypeSizes = defaultTypeSizes;
}
private int defaultStringSizeBytes = Config.getPropertyInteger(ObjectClassSizer.class,"defaultStringSizeBytes", 1000).intValue();
private ReLookup fieldDefaultSizes = new ReLookup();
private Map,Long> defaultTypeSizes;
}