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

com.holmos.cache.size.DataDictionary Maven / Gradle / Ivy

The newest version!
package com.holmos.cache.size;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * 缓存的数据字典,里面存储两个字典:
 * 
 * 
  • 缓存中包含对象的类型所占用内存大小,类似C++中的sizeof计算
  • *
  • 缓存中的对象使用情况,保存对象和被引用次数的key-value对
  • * * @author 吴银龙([email protected]) * */ public class DataDictionary { private static volatile DataDictionary dataDictionary; public static DataDictionary getInstance(){ synchronized(DataDictionary.class){ if(null==dataDictionary) dataDictionary=new DataDictionary(); return dataDictionary; } } /**缓存中包含对象的类型所占用内存大小的字典*/ private HashMap, Integer> typeSizeDic; /**缓存中的对象使用情况字典*/ private HashMap objectUseDic; /**缓存中的创建顺序表*/ private LinkedList eleCreatedOrderDic; private DataDictionary(){ typeSizeDic=new HashMap, Integer>(); objectUseDic=new HashMap(); eleCreatedOrderDic=new LinkedList(); } public HashMap, Integer> getTypeSizeDic() { return typeSizeDic; } public HashMap getObjectUseDic() { return objectUseDic; } public Integer getTypeSize(Class type){ Integer size=typeSizeDic.get(type); if(null == size) return 0; return size; } public Integer getUserNum(Object key){ Integer num= objectUseDic.get(key); if(null == num) return 0; return num; } public void putTypeSize(Class type,int size){ typeSizeDic.put(type, size); } public void addObjectUse(Object value){ if(objectUseDic.get(value)==null) objectUseDic.put(value, 1); else objectUseDic.put(value, objectUseDic.get(value)+1); } public void createNewEle(Object element){ eleCreatedOrderDic.add(element); } public void removeEleBet(int start,int end){ for(int i=start;i